jpfun 0.4.0 → 0.5.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 (83) 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 +4 -4
  14. package/dist/functions/ASTtypes.js +5 -9
  15. package/dist/functions/adjust/index.js +16 -53
  16. package/dist/functions/arpeggio/index.js +27 -15
  17. package/dist/functions/bar/index.js +7 -8
  18. package/dist/functions/beam/index.js +6 -1
  19. package/dist/functions/beam/layout.d.ts +1 -1
  20. package/dist/functions/beam/layout.js +5 -3
  21. package/dist/functions/box/index.js +88 -82
  22. package/dist/functions/br/index.js +9 -5
  23. package/dist/functions/dash/index.js +24 -13
  24. package/dist/functions/div/index.d.ts +5 -2
  25. package/dist/functions/div/index.js +15 -14
  26. package/dist/functions/dot/index.js +13 -11
  27. package/dist/functions/dyn/index.js +35 -39
  28. package/dist/functions/grace/index.d.ts +19 -6
  29. package/dist/functions/grace/index.js +136 -121
  30. package/dist/functions/head/index.js +19 -14
  31. package/dist/functions/key/index.js +8 -4
  32. package/dist/functions/meter/index.js +22 -2
  33. package/dist/functions/note/index.d.ts +6 -1
  34. package/dist/functions/note/index.js +48 -30
  35. package/dist/functions/note/source-transform.d.ts +21 -0
  36. package/dist/functions/note/source-transform.js +164 -0
  37. package/dist/functions/page/index.d.ts +8 -1
  38. package/dist/functions/page/index.js +23 -13
  39. package/dist/functions/program/index.js +11 -2
  40. package/dist/functions/set/index.js +14 -8
  41. package/dist/functions/stack/index.js +7 -5
  42. package/dist/functions/symbol/index.d.ts +2 -0
  43. package/dist/functions/symbol/index.js +11 -7
  44. package/dist/functions/symbol/ornament.d.ts +3 -3
  45. package/dist/functions/symbol/ornament.js +21 -35
  46. package/dist/functions/symbol/symbols/accent.js +5 -5
  47. package/dist/functions/symbol/symbols/fermata.js +5 -4
  48. package/dist/functions/tempo/index.js +7 -1
  49. package/dist/functions/text/index.js +11 -3
  50. package/dist/functions/tie/index.js +28 -32
  51. package/dist/functions/tuplet/index.js +26 -24
  52. package/dist/functions/up/index.js +24 -21
  53. package/dist/functions/voice/index.js +36 -26
  54. package/dist/functions/volta/index.js +16 -17
  55. package/dist/jpfun.core.min.js +98 -129
  56. package/dist/jpfun.from-midi.min.js +5 -5
  57. package/dist/jpfun.from-musicxml.min.js +8 -8
  58. package/dist/jpfun.min.js +107 -138
  59. package/dist/layout/engine.d.ts +11 -1
  60. package/dist/layout/engine.js +299 -96
  61. package/dist/layout/page.js +2 -0
  62. package/dist/layout/range.d.ts +26 -0
  63. package/dist/layout/range.js +75 -0
  64. package/dist/layout/types.d.ts +33 -4
  65. package/dist/lowering/types.d.ts +5 -0
  66. package/dist/parser/parse-utils/length-utils.js +2 -0
  67. package/dist/parser/parse-utils/note-utils.d.ts +2 -0
  68. package/dist/parser/parse-utils/note-utils.js +6 -2
  69. package/dist/parser/parserContext.js +1 -1
  70. package/dist/playback/compile.d.ts +2 -9
  71. package/dist/playback/compile.js +297 -186
  72. package/dist/playback/event.d.ts +8 -16
  73. package/dist/playback/event.js +13 -3
  74. package/dist/playback/types.d.ts +76 -18
  75. package/dist/playback/types.js +1 -0
  76. package/dist/render/index.d.ts +1 -1
  77. package/dist/render/index.js +1 -1
  78. package/dist/render/recording.d.ts +1 -0
  79. package/dist/render/recording.js +21 -0
  80. package/dist/render/translate.d.ts +14 -0
  81. package/dist/render/translate.js +48 -0
  82. package/grammar.md +103 -78
  83. package/package.json +4 -4
@@ -1,6 +1,7 @@
1
1
  import type { 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 { Track } from "../../lowering/track.js";
4
5
  import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../temporal.js";
5
6
  import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
6
7
  import { ParserContext } from "../../parser/parserContext.js";
@@ -31,7 +32,7 @@ declare class GraceFunction extends ASTFunctionNode {
31
32
  constructor(span: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null, scale?: number);
32
33
  setGrace(node: ASTNodeBase, factor?: number): void;
33
34
  private adopt;
34
- /** 倚音与宿主各自收敛成可见 Temporal,然后整体折叠进一个复合节点 */
35
+ /** 宿主收敛成一个可见 Temporal,倚音展开成局部时间列,然后整体折叠进一个复合节点 */
35
36
  loweringEnter(ctx: LoweringContext, track: Track): GraceTemporal[];
36
37
  toString(source: string): string;
37
38
  }
@@ -41,29 +42,41 @@ export declare class GraceTemporal extends TemporalNodeBase {
41
42
  ast: GraceFunction;
42
43
  box: LayoutBox;
43
44
  readonly host: VisualTemporalNode;
45
+ /** 可见倚音按局部时间成列,同列成员分属不同声部 */
46
+ readonly graceColumns: readonly (readonly VisualTemporalNode[])[];
47
+ /** graceColumns 按列展开 */
44
48
  readonly graces: readonly VisualTemporalNode[];
45
- /** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
49
+ /** 承担节奏的倚音成员,按声部与零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
46
50
  readonly graceRuns: readonly (readonly VisualTemporalNode[])[];
47
51
  readonly side: GraceSide;
52
+ /** 倚音子域的根轨:由宿主轨分出,不挂进它的分组,子域之外不可见 */
53
+ private readonly root;
54
+ /** 倚音内容里声明的附件,随子域排版 */
55
+ private readonly attachments;
56
+ /** 倚音内容的全部事件,含不可见的状态事件,按局部时间列展开 */
57
+ private readonly events;
58
+ /** 与 events 一一对应的局部起点,已随时值减半 */
59
+ private readonly starts;
48
60
  /** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
49
61
  private hostOffset;
50
62
  private graceOffsets;
51
63
  /** 倚音线起点,同样是局部偏移 */
52
64
  private hookOrigin;
53
- constructor(ast: GraceFunction, host: VisualTemporalNode, graces: readonly VisualTemporalNode[], side: GraceSide);
65
+ constructor(ast: GraceFunction, host: VisualTemporalNode, columns: readonly (readonly TemporalNodeBase[])[], root: Track, attachments: readonly LoweringAttachment[], side: GraceSide);
54
66
  /** 成员共享全局时间状态,按发声顺序固化 */
55
67
  onTimeState(state: TimeState): void;
56
68
  /**
57
69
  * 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
58
70
  *
59
- * 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
71
+ * 比例 = 倚音块字面时长 / 四分音符,而四分音符 T=1,所以直接取各声部最晚的局部终点。
60
72
  * 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
73
+ * 局部时间按同一比例映射进借走的时值,各声部因此同时起奏。
61
74
  */
62
75
  emitPlayback(emitter: PlaybackEmitter): void;
63
76
  /**
64
77
  * 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
65
78
  *
66
- * 倚音成员之间自己紧排,不参与横向弹簧;整块的宽度计入复合盒,
79
+ * 倚音内容作为子域先排成一个刚性块,块内的附件同时就位;含约束留白的宽度计入复合盒,
67
80
  * 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
68
81
  */
69
82
  prepareLayout(context: LayoutPrepareContext): void;
@@ -1,6 +1,7 @@
1
1
  import { ErrorDiagnostic } from "../../diagnostic.js";
2
- import { prepareLayoutHost } from "../../layout/engine.js";
2
+ import { 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
  /**
@@ -31,27 +32,36 @@ class GraceFunction extends ASTFunctionNode {
31
32
  static def = {
32
33
  name: ["grace"],
33
34
  description: "倚音",
34
- example: `前倚音: @grace(宿主, 倚音, side=pre) 后倚音: @grace(宿主, 倚音, side=post)
35
- 语法糖:'>' 与 '<',箭头永远指向宿主
36
- 2>1 前倚音:2 是 1 的倚音,画在 1 的左上角
37
- 1<2 后倚音:2 是 1 的倚音,画在 1 的右上角
38
- {3 2}>1 多个倚音用大括号括起来
39
- 1>2>3 允许嵌套:1 是 2 的倚音,2 是 3 的倚音
40
- 倚音默认就是八分音符(自带一条减时线),再写 '/' 变成十六分音符`,
41
- allowExtraArgs: true,
35
+ details: `\
36
+ ~~~jpfun
37
+ @grace(1, {3 2}, side=pre)
38
+ ~~~
39
+ **简写**:箭头始终指向宿主
40
+ - \`2>1\`:\`2\` 是 \`1\` 的前倚音,位于左上方
41
+ - \`1<2\`:\`2\` 是 \`1\` 的后倚音,位于右上方
42
+ - \`{3 2}>1\`:一组前倚音,等价于上例
43
+ - \`1>2>3\`:允许嵌套,\`1\` 修饰 \`2\`,\`2\` 修饰 \`3\`
44
+
45
+ 倚音默认带一条减时线,按八分音符书写;再加 \`/\` 变为十六分音符
46
+
47
+ 倚音内容可以是换行以外的任意结构,例如 \`{1 & 3}>2\` 的两个声部同时奏出`,
48
+ allowExtraArgs: false,
42
49
  args: [
43
50
  {
44
51
  name: "host",
52
+ description: "被倚音修饰的宿主内容",
45
53
  type: "content",
46
54
  default: null,
47
55
  },
48
56
  {
49
57
  name: "grace",
58
+ description: "倚音内容,多个音符可用大括号组合,也可以含多个声部",
50
59
  type: "content",
51
60
  default: null,
52
61
  },
53
62
  {
54
63
  name: "side",
64
+ description: "`pre` 为前倚音,`post` 为后倚音",
55
65
  type: "string",
56
66
  default: "pre",
57
67
  }
@@ -198,45 +208,45 @@ class GraceFunction extends ASTFunctionNode {
198
208
  this.sourceSpan.start = Math.min(this.sourceSpan.start, node.sourceSpan.start);
199
209
  this.sourceSpan.end = Math.max(this.sourceSpan.end, node.sourceSpan.end);
200
210
  }
201
- /** 倚音与宿主各自收敛成可见 Temporal,然后整体折叠进一个复合节点 */
211
+ /** 宿主收敛成一个可见 Temporal,倚音展开成局部时间列,然后整体折叠进一个复合节点 */
202
212
  loweringEnter(ctx, track) {
203
- if (!this.host || !this.grace) {
213
+ const host = this.host;
214
+ const grace = this.grace;
215
+ if (!host || !grace) {
204
216
  throw new ErrorDiagnostic("E_GRACE_MISSING_ARGS", "@grace 需要同时给出宿主和倚音", this.sourceSpan);
205
217
  }
206
- let graces = [];
207
- 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);
208
224
  // 成员不是外层分组的成员,复合节点才是;否则 voice 的歌词会按下标错位
225
+ // 按发声顺序展开,事件序号随之有序
209
226
  ctx.isolateFromLoweringGroups(() => {
210
- const order = this.side === "pre"
211
- ? [this.grace, this.host]
212
- : [this.host, this.grace];
213
- for (const content of order) {
214
- const events = ctx.trackedEvents(content, new Fraction(), track).flat();
215
- const visible = events.filter(isVisualTemporalNode);
216
- // 并行分支的纵向关系由引擎解 Track 树,而折叠成员进不了引擎
217
- if (events.some(event => event.track !== track))
218
- throw new ErrorDiagnostic("E_GRACE_PARALLEL_CONTENT", "@grace 的内容不能包含多声部结构(& / @voices);要在倚音里叠音请用 ^", content.sourceSpan);
219
- if (content === this.host) {
220
- if (visible.length !== 1 || visible.length !== events.length) {
221
- throw new ErrorDiagnostic("E_GRACE_INVALID_HOST", "@grace 的宿主必须恰好产生一个可见 Temporal", content.sourceSpan);
222
- }
223
- host = visible[0];
224
- }
225
- else {
226
- if (visible.length === 0 || visible.length !== events.length) {
227
- throw new ErrorDiagnostic("E_GRACE_INVALID_CONTENT", "@grace 的倚音必须只产生可见 Temporal,且至少一个", content.sourceSpan);
228
- }
229
- graces = visible;
230
- }
231
- }
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();
232
234
  });
233
- 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);
234
244
  // 倚音同段无条件全部相连,只被零时长标记切断,所以 grace 一定认识 beam,允许耦合
235
245
  // 折叠成员不进全局时间列,autobeam 看不到它们,要手动调用且强制连接
236
246
  for (const run of composite.graceRuns) {
237
247
  if (run.length < 2)
238
248
  continue;
239
- ctx.addAttachment(createBeamLayoutAttachment([...run], false, this.grace.sourceSpan));
249
+ ctx.addAttachment(createBeamLayoutAttachment([...run], false, grace.sourceSpan));
240
250
  }
241
251
  return [composite];
242
252
  }
@@ -255,27 +265,39 @@ const HOOK_COMMANDS = [
255
265
  { op: "C", cx1: -0.01, cy1: 0.26, cx2: 0.14, cy2: 0.42, x: 0.29, y: 0.36 },
256
266
  ];
257
267
  const HOOK_WIDTH = 0.07;
258
- /** 倚音块底边到宿主顶边的视觉间隙;倚音线会探进这段空间指向宿主 */
268
+ /** 倚音块底边到宿主肩线的视觉间隙;倚音线会探进这段空间指向宿主 */
259
269
  const GRACE_RISE = 0.18;
260
- /** 倪音向宿主借时值的上限,防止宿主被偷光;只读 */
270
+ /** 倚音向宿主借时值的上限,防止宿主被偷光;只读 */
261
271
  const MAX_STEAL_RATIO = new Fraction(3, 4);
262
272
  /** 倚音复合体:宿主与倚音都折叠在这一个盒子里,对外只是一个可见事件 */
263
273
  export class GraceTemporal extends TemporalNodeBase {
264
274
  host;
275
+ /** 可见倚音按局部时间成列,同列成员分属不同声部 */
276
+ graceColumns;
277
+ /** graceColumns 按列展开 */
265
278
  graces;
266
- /** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
279
+ /** 承担节奏的倚音成员,按声部与零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
267
280
  graceRuns;
268
281
  side;
282
+ /** 倚音子域的根轨:由宿主轨分出,不挂进它的分组,子域之外不可见 */
283
+ root;
284
+ /** 倚音内容里声明的附件,随子域排版 */
285
+ attachments;
286
+ /** 倚音内容的全部事件,含不可见的状态事件,按局部时间列展开 */
287
+ events;
288
+ /** 与 events 一一对应的局部起点,已随时值减半 */
289
+ starts;
269
290
  /** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
270
291
  hostOffset = { x: 0, y: 0 };
271
292
  graceOffsets = [];
272
293
  /** 倚音线起点,同样是局部偏移 */
273
294
  hookOrigin = null;
274
- constructor(ast, host, graces, side) {
295
+ constructor(ast, host, columns, root, attachments, side) {
275
296
  super();
276
297
  this.ast = ast;
277
298
  this.host = host;
278
- this.graces = graces;
299
+ this.root = root;
300
+ this.attachments = attachments;
279
301
  this.side = side;
280
302
  this.T.copyFrom(host.T);
281
303
  this.mergeKey = host.mergeKey;
@@ -286,37 +308,54 @@ export class GraceTemporal extends TemporalNodeBase {
286
308
  host.addon = void 0;
287
309
  // 成员不进入全局 columns,对外由复合体代表:写在成员上的标签仍可做关系端点
288
310
  host.foldedInto = this;
289
- const runs = [];
290
- let run = [];
291
- for (const grace of graces) {
292
- grace.foldedInto = this;
293
- // 调号、速度这类零时长标记不承担节奏,还会在视觉上把倚音串切断
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
+ // 调号、速度这类零时长标记不承担节奏,还会在视觉上把本声部的倚音串切断
294
336
  if (grace.T.isZero()) {
295
- if (run.length > 0)
296
- runs.push(run);
297
- run = [];
337
+ runs.delete(grace.track);
298
338
  continue;
299
339
  }
340
+ let run = runs.get(grace.track);
341
+ if (!run) {
342
+ runs.set(grace.track, run = []);
343
+ graceRuns.push(run);
344
+ }
300
345
  run.push(grace);
301
- // 倚音默认就是八分音符:补一条减时线,书面时值随之减半
346
+ // 补一条减时线,书面时值随之减半
302
347
  const addon = grace.addon = { ...grace.addon };
303
348
  addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + 1;
304
349
  grace.T.divPow2();
305
350
  }
306
- if (run.length > 0)
307
- runs.push(run);
308
- this.graceRuns = runs;
351
+ this.graceRuns = graceRuns;
309
352
  }
310
353
  /** 成员共享全局时间状态,按发声顺序固化 */
311
354
  onTimeState(state) {
312
355
  // 同步时间的修改。比如tuplet修改的是GraceTemporal.T,这里要把它传给成员
313
356
  this.host.T.copyFrom(this.T);
314
- const members = this.side === "pre"
315
- ? [...this.graces, this.host]
316
- : [this.host, ...this.graces];
317
- for (const member of members) {
357
+ for (const member of this.side === "pre" ? [...this.events, this.host] : [this.host, ...this.events]) {
318
358
  member.t.copyFrom(this.t);
319
- member.track = this.track;
320
359
  member.layoutLine = this.layoutLine;
321
360
  member.onTimeState?.(state);
322
361
  }
@@ -324,43 +363,37 @@ export class GraceTemporal extends TemporalNodeBase {
324
363
  /**
325
364
  * 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
326
365
  *
327
- * 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
366
+ * 比例 = 倚音块字面时长 / 四分音符,而四分音符 T=1,所以直接取各声部最晚的局部终点。
328
367
  * 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
368
+ * 局部时间按同一比例映射进借走的时值,各声部因此同时起奏。
329
369
  */
330
370
  emitPlayback(emitter) {
331
371
  const written = new Fraction();
332
- for (const grace of this.graces)
333
- written.add(grace.T);
372
+ const end = new Fraction();
373
+ this.events.forEach((event, index) => {
374
+ end.copyFrom(this.starts[index]).add(event.T);
375
+ if (end.compare(written) > 0)
376
+ written.copyFrom(end);
377
+ });
334
378
  const ratio = written.compare(MAX_STEAL_RATIO) > 0 ? MAX_STEAL_RATIO : written;
335
379
  const total = emitter.end.clone().sub(emitter.start);
336
380
  const steal = total.clone().mul(ratio);
337
381
  const hostDuration = total.sub(steal);
338
- const start = emitter.start.clone();
339
- const playGraces = (cursor) => {
340
- for (const grace of this.graces) {
341
- if (grace.T.isZero() || written.isZero()) {
342
- emitter.play(grace, cursor, new Fraction());
343
- continue;
344
- }
345
- const duration = steal.clone().mul(grace.T).div(written);
346
- emitter.play(grace, cursor, duration);
347
- cursor.add(duration);
348
- }
349
- };
350
- if (this.side === "pre") {
351
- const cursor = start.clone();
352
- playGraces(cursor);
353
- emitter.play(this.host, cursor, hostDuration);
354
- }
355
- else {
356
- emitter.play(this.host, start, hostDuration);
357
- playGraces(start.clone().add(hostDuration));
358
- }
382
+ // 全是零时长标记时块长为 0,成员都落在借时起点
383
+ const scale = written.isZero() ? new Fraction() : steal.clone().div(written);
384
+ const base = this.side === "pre" ? emitter.start.clone() : emitter.start.clone().add(hostDuration);
385
+ if (this.side === "post")
386
+ emitter.play(this.host, emitter.start.clone(), hostDuration);
387
+ this.events.forEach((event, index) => {
388
+ emitter.play(event, this.starts[index].clone().mul(scale).add(base), event.T.clone().mul(scale));
389
+ });
390
+ if (this.side === "pre")
391
+ emitter.play(this.host, base.add(steal), hostDuration);
359
392
  }
360
393
  /**
361
394
  * 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
362
395
  *
363
- * 倚音成员之间自己紧排,不参与横向弹簧;整块的宽度计入复合盒,
396
+ * 倚音内容作为子域先排成一个刚性块,块内的附件同时就位;含约束留白的宽度计入复合盒,
364
397
  * 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
365
398
  */
366
399
  prepareLayout(context) {
@@ -370,44 +403,26 @@ export class GraceTemporal extends TemporalNodeBase {
370
403
  this.host.addon = this.addon;
371
404
  this.addon = void 0;
372
405
  }
373
- for (const grace of this.graces)
374
- prepareLayoutHost(grace, context);
375
- prepareLayoutHost(this.host, context);
376
- const em = this.ast.size;
377
- const graceEm = em * GRACE_SCALE;
378
- const memberGap = graceEm * 0.16;
406
+ const block = context.layoutSubdomain(this, this.graceColumns, this.root, this.attachments);
407
+ const hostWidth = layoutLocalSequence([this.host], context);
408
+ const graceEm = this.ast.size * GRACE_SCALE;
379
409
  const sideGap = graceEm * 0.2;
380
410
  const rise = graceEm * GRACE_RISE;
381
- let graceWidth = 0;
382
- let graceAxis = 0;
383
- let graceHeight = 0;
384
- // 倚音之间按视觉轴对齐,否则带下八度点的成员会把数字顶得比旁边高
385
- for (const grace of this.graces)
386
- graceAxis = Math.max(graceAxis, grace.box.visualAxis);
387
- this.graceOffsets = this.graces.map(grace => {
388
- const x = graceWidth;
389
- graceWidth += grace.box.w + memberGap;
390
- const y = graceAxis - grace.box.visualAxis;
391
- graceHeight = Math.max(graceHeight, y + grace.box.h);
392
- return { x, y };
393
- });
394
- if (this.graces.length > 0)
395
- graceWidth -= memberGap;
396
411
  // 倚音贴着宿主的肩线而不是宿主盒顶:宿主本身也是复合体时,
397
412
  // 肩线由它转发上来,所以两侧倚音会落在同一高度而不是层层叠高
398
413
  const shoulder = this.host.ports[SHOULDER_PORT]?.y ?? 0;
399
- const graceTop = shoulder - rise - graceHeight;
414
+ const graceTop = shoulder - rise - block.height;
400
415
  const lift = Math.max(0, -graceTop);
401
- const graceX = this.side === "pre" ? 0 : this.host.box.w + sideGap;
416
+ const graceX = this.side === "pre" ? 0 : hostWidth + sideGap;
402
417
  this.hostOffset = {
403
- x: this.side === "pre" ? graceWidth + sideGap : 0,
418
+ x: this.host.box.x + (this.side === "pre" ? block.width + sideGap : 0),
404
419
  y: lift,
405
420
  };
406
- for (const offset of this.graceOffsets) {
407
- offset.x += graceX;
408
- offset.y += graceTop + lift;
409
- }
410
- this.box.w = this.host.box.w + sideGap + graceWidth;
421
+ this.graceOffsets = block.positions.map(position => ({
422
+ x: graceX + position.x,
423
+ y: graceTop + lift + position.y,
424
+ }));
425
+ this.box.w = hostWidth + sideGap + block.width;
411
426
  this.box.h = lift + this.host.box.h;
412
427
  this.box.anchor = this.hostOffset.x + this.host.box.anchor;
413
428
  this.box.visualAxis = lift + this.host.box.visualAxis;
@@ -415,19 +430,19 @@ export class GraceTemporal extends TemporalNodeBase {
415
430
  for (const name in this.host.ports) {
416
431
  const port = this.host.ports[name];
417
432
  this.ports[name] = {
433
+ ...port,
418
434
  x: this.hostOffset.x + port.x,
419
435
  y: this.hostOffset.y + port.y,
420
436
  };
421
437
  }
422
438
  // 宿主没有肩线时它自己的盒顶就是肩线;有则上面那轮转发已经带偏移抬好了
423
439
  this.ports[SHOULDER_PORT] ??= { x: this.box.anchor, y: this.hostOffset.y };
424
- // 钩形曲线挂在靠近宿主的那个倚音成员下方,零时长标记不承接它
425
- const near = this.side === "pre"
426
- ? this.graceRuns.at(-1)?.at(-1)
427
- : this.graceRuns[0]?.[0];
440
+ // 钩形曲线挂在靠近宿主的那个倚音成员下方,零时长标记不承接它;高度取成员的最低下沿,不含块内附件
441
+ const rhythmic = this.graces.filter(grace => !grace.T.isZero());
442
+ const near = this.side === "pre" ? rhythmic.at(-1) : rhythmic[0];
428
443
  this.hookOrigin = near ? {
429
444
  x: this.graceOffsets[this.graces.indexOf(near)].x + near.box.anchor,
430
- y: graceTop + lift + graceHeight,
445
+ y: Math.max(...this.graces.map((grace, index) => this.graceOffsets[index].y + grace.box.h)),
431
446
  } : null;
432
447
  }
433
448
  /** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
@@ -296,22 +296,27 @@ class HeadSlotNode extends ASTNodeBase {
296
296
  class HeadFunction extends ASTFunctionNode {
297
297
  static def = {
298
298
  name: ["head"],
299
- description: "提供左、中、右三块布局,常用于曲谱头部信息展示;也可用连续的 H.*: 行声明构造",
300
- example: `@head(left={{@key(C) @meter(4,4)}}, center={@text(标题)}, right={@text(作者)})
301
- 语法糖:相邻的 H.*: 声明合并为一个 head,每条声明生成对应槽的一行;空行或普通内容结束组合
302
- H.title: 标题 center 槽,预设大字号居中 Text
303
- H.subtitle: 副标题 center 槽,预设小字号居中 Text
304
- H.author: 作者 right 槽,预设小字号右对齐 Text
305
- H.signature: 1=C 4/4 left 槽,同一行生成 Key 和 Meter
306
- H.tempo: 94 left 槽,生成 Tempo
307
- H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
308
- 以 @ 或 { 开头时按 DSL 解析,否则按该字段的裸文本规则生成 Text`,
299
+ description: "左、中、右三区布局",
300
+ details: `常用于谱头:
301
+ ~~~jpfun
302
+ @head(left={{@key(C) @meter(4,4)}},
303
+ center={@text(标题)}, right={@text(作者)})
304
+ ~~~
305
+ **简写**:相邻的 \`H.*:\` 行合并为同一个谱头,每条声明生成所在区的一行;空行或普通内容结束组合
306
+ ~~~jpfun
307
+ H.title: 标题
308
+ H.subtitle: 副标题
309
+ H.author: 作者
310
+ H.signature: 1=C 4/4
311
+ H.tempo: 94
312
+ ~~~
313
+ \`title\` / \`subtitle\` 居中,分别预设大、小字号;\`author\` 小字号右对齐;\`signature\` 和 \`tempo\` 放在左区。\`H.left:\`、\`H.center:\`、\`H.right:\` 可直接写对应区的零时长内容。以 \`@\` 或 \`{\` 开头的值按 jpFun 解析,其余按字段的文本规则处理。`,
309
314
  allowExtraArgs: false,
310
315
  args: [
311
- { name: "left", type: "content", default: "" },
312
- { name: "center", type: "content", default: "" },
313
- { name: "right", type: "content", default: "" },
314
- { name: "gap", type: "length", default: { value: DEFAULT_GAP_PX, unit: "px" } },
316
+ { name: "left", description: "左区零时长内容,如调性、拍号与速度记号", type: "content", default: "" },
317
+ { name: "center", description: "中区零时长内容,如标题与副标题", type: "content", default: "" },
318
+ { name: "right", description: "右区零时长内容,如作者与署名", type: "content", default: "" },
319
+ { name: "gap", description: "每个区内相邻行的垂直间距", type: "length", default: { value: DEFAULT_GAP_PX, unit: "px" } },
315
320
  ],
316
321
  };
317
322
  slots;
@@ -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)
@@ -33,18 +33,22 @@ class KeyFunction extends ASTFunctionNode {
33
33
  static def = {
34
34
  name: ["key", "1"],
35
35
  description: "设置时间线上的 1= 调性基准",
36
- example: `@1(C4) 或 @key(F#3)
37
- 它不会修改 parser 的局部变量,而是在时间固化阶段影响其后的数字音名解释
38
- 画出来的记号只含音名和升降号,不含八度;通常叠在音符上方书写:1 ^ @1(F#)`,
36
+ details: `\
37
+ ~~~jpfun
38
+ 1 ^ @key(F#3)
39
+ ~~~
40
+ 调性沿时间线影响后续数字音名。谱面记号显示音名与升降号,省略八度;通常用 \`^\` 叠在音符上方。`,
39
41
  allowExtraArgs: false,
40
42
  args: [
41
43
  {
42
44
  name: "tonality",
45
+ description: "`1=` 的音高基准,如 `C4`、`F#3`",
43
46
  type: "string",
44
47
  default: null,
45
48
  },
46
49
  {
47
50
  name: "size",
51
+ description: "调性记号字号",
48
52
  type: "length",
49
53
  default: {
50
54
  value: 1,
@@ -6,21 +6,28 @@ class MeterFunction extends ASTFunctionNode {
6
6
  static def = {
7
7
  name: ["meter"],
8
8
  description: "设置拍号",
9
- example: `@meter(6, 8) 设置 6/8 拍号`,
9
+ details: `\
10
+ ~~~jpfun
11
+ @meter(6, 8)
12
+ ~~~
13
+ 设置 \`6/8\` 拍号:每小节含六个八分音符的时值`,
10
14
  allowExtraArgs: false,
11
15
  args: [
12
16
  {
13
17
  name: "num",
18
+ description: "拍号分子,每小节包含的单位拍数",
14
19
  type: "number",
15
20
  default: null
16
21
  },
17
22
  {
18
23
  name: "den",
24
+ description: "拍号分母,以几分音符为单位拍",
19
25
  type: "number",
20
26
  default: null
21
27
  },
22
28
  {
23
29
  name: "size",
30
+ description: "拍号字号",
24
31
  type: "length",
25
32
  default: { value: 0.7, unit: "em" },
26
33
  },
@@ -80,9 +87,22 @@ class MeterFunction extends ASTFunctionNode {
80
87
  }
81
88
  measureStart.copyFrom(end);
82
89
  };
90
+ const metersByHost = new Map();
91
+ for (const nodes of result.astToTemporal.values()) {
92
+ for (const node of nodes) {
93
+ if (!(node instanceof MeterTemporal))
94
+ continue;
95
+ let host = node;
96
+ while (host.foldedInto)
97
+ host = host.foldedInto;
98
+ if (!metersByHost.has(host))
99
+ metersByHost.set(host, node);
100
+ }
101
+ }
83
102
  for (const column of result.columns) {
84
103
  const time = column[0].t;
85
- const meter = column.find(node => node instanceof MeterTemporal);
104
+ const host = column.find(node => metersByHost.has(node));
105
+ const meter = host && metersByHost.get(host);
86
106
  if (meter) {
87
107
  closeMeasure(time, meter.ast.sourceSpan.start);
88
108
  active = meter.ast;
@@ -1,2 +1,7 @@
1
- import { ASTFunctionClass } from "../ASTtypes.js";
1
+ import { SourceSpan, ASTFunctionClass } from "../ASTtypes.js";
2
+ import type { LoweringResult } from "../../lowering/types.js";
3
+ import { type NoteTransform, type SourceEdit } from "./source-transform.js";
4
+ export type { NoteTransform, SourceEdit } from "./source-transform.js";
5
+ /** 根据同一源码的 lowering 生成局部修改;传入 ranges 时只处理完整落入任一选区的音符 */
6
+ export declare function transformNotes(source: string, lowering: LoweringResult, operation: NoteTransform, ranges?: readonly SourceSpan[]): SourceEdit[];
2
7
  export declare const NoteNode: ASTFunctionClass;