jpfun 0.5.0 → 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 (41) hide show
  1. package/dist/functions/ASTtypes.d.ts +4 -0
  2. package/dist/functions/ASTtypes.js +25 -14
  3. package/dist/functions/adjust/index.js +6 -2
  4. package/dist/functions/arpeggio/index.js +19 -12
  5. package/dist/functions/beam/auto.js +2 -11
  6. package/dist/functions/box/index.js +52 -1
  7. package/dist/functions/default.js +2 -1
  8. package/dist/functions/div/index.js +2 -3
  9. package/dist/functions/grace/index.d.ts +4 -2
  10. package/dist/functions/grace/index.js +16 -5
  11. package/dist/functions/head/index.js +11 -4
  12. package/dist/functions/meter/index.js +26 -6
  13. package/dist/functions/meter/shape.d.ts +3 -0
  14. package/dist/functions/meter/shape.js +43 -0
  15. package/dist/functions/note/index.js +25 -45
  16. package/dist/functions/rest/index.d.ts +2 -0
  17. package/dist/functions/rest/index.js +233 -0
  18. package/dist/functions/symbol/ornament.js +1 -1
  19. package/dist/functions/temporal.d.ts +21 -7
  20. package/dist/functions/temporal.js +14 -4
  21. package/dist/functions/tie/index.js +14 -10
  22. package/dist/functions/tuplet/index.js +1 -1
  23. package/dist/functions/up/index.js +47 -27
  24. package/dist/functions/voice/connections.d.ts +19 -0
  25. package/dist/functions/voice/connections.js +88 -0
  26. package/dist/functions/voice/index.js +244 -99
  27. package/dist/jpfun.core.min.js +56 -39
  28. package/dist/jpfun.min.js +65 -48
  29. package/dist/layout/engine.d.ts +3 -1
  30. package/dist/layout/engine.js +190 -64
  31. package/dist/layout/model.js +22 -66
  32. package/dist/layout/page.js +1 -1
  33. package/dist/layout/types.d.ts +30 -45
  34. package/dist/lowering/loweringContext.d.ts +1 -1
  35. package/dist/lowering/loweringContext.js +34 -4
  36. package/dist/parser/parserContext.js +4 -8
  37. package/dist/parser/preprocess.js +4 -6
  38. package/dist/playback/compile.js +1 -1
  39. package/dist/render/paint.js +1 -1
  40. package/grammar.md +200 -257
  41. package/package.json +1 -1
@@ -2,12 +2,13 @@ import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTLabelNode, ASTBraceNode }
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { ANCHOR_KEY, DEFAULT_KEY, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
4
4
  import { Fraction } from "../../fraction.js";
5
- import { layoutLocalSequence } from "../../layout/engine.js";
5
+ import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
6
6
  /**
7
7
  * `@up` / `@down` 共用的折叠容器,不导出
8
8
  *
9
9
  * `contents[0]` 是宿主,其余向上;`belows` 全部向下。两个方向共用一个容器,
10
10
  * 所以混写的 `^` / `_` 会全部绑到同一个宿主,不需要括号分组。
11
+ * 每个参数只能产生一个 VisualTemporalNode,外层统一承担对齐、时值和合列
11
12
  */
12
13
  class FoldFunction extends ASTFunctionNode {
13
14
  contents = [];
@@ -57,9 +58,9 @@ class FoldFunction extends ASTFunctionNode {
57
58
  // 成员不是外层分组的成员,折叠体才是;否则 voice 的歌词会按下标错位
58
59
  ctx.isolateFromLoweringGroups(() => {
59
60
  for (const content of this.children) {
60
- // 摊平所有时间列取全部事件,每个成员要求恰好一个
61
- const [member, ...rest] = ctx.trackedEvents(content, new Fraction(), track).flat();
62
- if (!member || rest.length > 0 || !isVisualTemporalNode(member)) {
61
+ const columns = ctx.trackedEvents(content, new Fraction(), track);
62
+ const member = columns.length === 1 && columns[0].length === 1 ? columns[0][0] : undefined;
63
+ if (!member || !isVisualTemporalNode(member)) {
63
64
  throw new ErrorDiagnostic("E_UP_INVALID_CHILD", "@up 的每个参数必须恰好产生一个可见 Temporal,且不能包含多声部结构", content.sourceSpan);
64
65
  }
65
66
  members.push(member);
@@ -240,21 +241,23 @@ class FoldTemporal extends TemporalNodeBase {
240
241
  this.members = members;
241
242
  // members 按 ast.children 构建,而 children 是 [...contents, ...belows]
242
243
  this.aboveCount = ast.contents.length;
243
- // 节奏由第一个有时值的成员决定;否则 `$p ^ 1` 这类写法会把整个折叠体压成零时长
244
- const lead = members.find(member => !member.T.isZero());
245
- if (lead)
246
- this.T.copyFrom(lead.T);
247
- this.mergeKey = DEFAULT_KEY;
244
+ const host = members[0];
245
+ this.mergeKey = host && host.mergeKey !== host.order ? host.mergeKey : DEFAULT_KEY;
246
+ // 普通组保留 `$p ^ 1` 的时值回退,专属组不借附属音符提前推进游标
247
+ const fallback = this.mergeKey === DEFAULT_KEY || this.mergeKey === ANCHOR_KEY;
248
+ if (!fallback && host)
249
+ this.T.copyFrom(host.T);
248
250
  this.initLayoutBox();
249
251
  // 宿主决定折叠体的时值,它的修饰语义也随之成为整体的修饰,
250
252
  // 自动连梁等语义处理才能看到这个折叠体的节奏;
251
253
  // 随后外层 LoweringGroup 会在同一 addon 上继续累加
252
- const leadAddon = members[0]?.addon;
253
- if (leadAddon)
254
- this.addon = { ...leadAddon };
254
+ const addon = host?.addon;
255
+ if (addon)
256
+ this.addon = { ...addon };
255
257
  for (const member of members) {
256
- // 只有锚点需要传上来:`| ^ @text(A)` 得保持小节线语义;
257
- // 成员不进全局 columns,它们自己的合并组对外没有意义
258
+ if (fallback && this.T.isZero())
259
+ this.T.copyFrom(member.T);
260
+ // 锚点仍优先:`| ^ @text(A)` 得保持小节线语义
258
261
  if (member.mergeKey === ANCHOR_KEY)
259
262
  this.mergeKey = ANCHOR_KEY;
260
263
  // 时间同步在 onTimeState 里做,避免成员的时间被提前固化(后续时间可能会变)
@@ -270,18 +273,33 @@ class FoldTemporal extends TemporalNodeBase {
270
273
  * 成员共享全局时间状态,从写在最后的成员开始固化
271
274
  */
272
275
  onTimeState(state) {
276
+ const duration = this.T.clone();
277
+ let offset = undefined;
278
+ let durationChanged = false;
273
279
  // 宿主写在最前面,标记写在它后面;标记先写入状态,宿主才读得到
274
280
  for (let i = this.members.length - 1; i >= 0; i--) {
275
281
  const member = this.members[i];
276
282
  // 堆叠在一起的成员共享同一个时值,由第一个成员决定;
277
283
  // 本来就没有时长的成员(标注、小节线等)保持 0,不会被拉长
278
- if (!member.T.isZero())
284
+ if (!member.T.isZero() && !this.T.isZero())
279
285
  member.T.copyFrom(this.T);
280
286
  member.t.copyFrom(this.t);
281
287
  member.track = this.track;
282
288
  member.layoutLine = this.layoutLine;
283
- member.onTimeState?.(state);
289
+ offset = member.onTimeState?.(state);
290
+ durationChanged ||= !member.T.isZero() && !member.T.equals(duration);
284
291
  }
292
+ const host = this.members[0];
293
+ if (host && !host.T.isZero()) {
294
+ this.T.copyFrom(host.T);
295
+ if (durationChanged) {
296
+ for (const member of this.members) {
297
+ if (!member.T.isZero())
298
+ member.T.copyFrom(this.T);
299
+ }
300
+ }
301
+ }
302
+ return offset;
285
303
  }
286
304
  emitPlayback(emitter) {
287
305
  // 倒序发布,宿主(members[0])最后:附属成员注册的局部变换因此作用到宿主
@@ -295,7 +313,7 @@ class FoldTemporal extends TemporalNodeBase {
295
313
  * 宿主留在轨道基线上,其余成员按书写顺序向上或向下叠放
296
314
  *
297
315
  * 成员不进入全局 columns,因此它们的准备、定位和绘制都由本节点负责;
298
- * 每个成员独立执行局部横排,宿主继承含框约束的占位,其余成员仍沿宿主锚点悬挂。
316
+ * 每个成员独立执行局部横排,宿主继承含框约束的占位,其余成员仍沿宿主锚点悬挂。
299
317
  */
300
318
  prepareLayout(context) {
301
319
  // lowering 期间修饰挂在折叠体上(augmenter 要看到整体节奏),渲染时交给宿主:
@@ -319,18 +337,20 @@ class FoldTemporal extends TemporalNodeBase {
319
337
  const anchor = first.box.x + first.box.anchor;
320
338
  const gap = this.ast.size * 0.12;
321
339
  this.verticalOffsets.length = this.members.length;
322
- this.verticalOffsets.fill(0);
340
+ this.verticalOffsets[0] = 0;
323
341
  // 以宿主盒顶为 0,向上得到负坐标、向下得到正坐标,最后整体下移
324
- let top = 0;
342
+ const firstBounds = getLayoutBounds(first);
343
+ let top = firstBounds.y;
325
344
  for (let i = 1; i < this.aboveCount; i++) {
326
- top -= gap + this.members[i].box.h;
327
- this.verticalOffsets[i] = top;
345
+ const bounds = getLayoutBounds(this.members[i]);
346
+ this.verticalOffsets[i] = top - gap - bounds.y - bounds.h;
347
+ top -= gap + bounds.h;
328
348
  }
329
- let bottom = first.box.h;
349
+ let bottom = firstBounds.y + firstBounds.h;
330
350
  for (let i = this.aboveCount; i < this.members.length; i++) {
331
- bottom += gap;
332
- this.verticalOffsets[i] = bottom;
333
- bottom += this.members[i].box.h;
351
+ const bounds = getLayoutBounds(this.members[i]);
352
+ this.verticalOffsets[i] = bottom + gap - bounds.y;
353
+ bottom += gap + bounds.h;
334
354
  }
335
355
  for (let i = 0; i < this.verticalOffsets.length; i++)
336
356
  this.verticalOffsets[i] -= top;
@@ -353,7 +373,7 @@ class FoldTemporal extends TemporalNodeBase {
353
373
  const topOffset = this.verticalOffsets[topIndex];
354
374
  this.ports["tie.top"] = {
355
375
  x: anchor,
356
- y: topOffset + (this.members[topIndex].ports["tie.top"]?.y ?? 0),
376
+ y: topOffset + (this.members[topIndex].ports["tie.top"]?.y ?? getLayoutBounds(this.members[topIndex], true).y),
357
377
  };
358
378
  }
359
379
  /** 引擎每次改变本节点坐标后,按当前 anchor 与准备阶段的纵向偏移重算成员绝对坐标 */
@@ -369,7 +389,7 @@ class FoldTemporal extends TemporalNodeBase {
369
389
  for (const member of this.members) {
370
390
  member.paint(painter);
371
391
  for (const decoration of member.decorations)
372
- decoration.paint(painter);
392
+ decoration.paint?.(painter);
373
393
  }
374
394
  }
375
395
  }
@@ -0,0 +1,19 @@
1
+ import type { SourceSpan } from "../ASTtypes.js";
2
+ import type { PathCommand } from "../../render/types.js";
3
+ export declare const CURVED_BRACE_COMMANDS: readonly PathCommand[];
4
+ export declare const CURVED_BRACE_BOUNDS: import("../../core.js").Rect;
5
+ /** 方括线端钩,以粗竖线宽度为单位,绘制时上下镜像 */
6
+ export declare const BRACKET_HOOK_REACH = 2.33;
7
+ export declare const BRACKET_HOOK_DROP = 1.17;
8
+ export declare const BRACKET_HOOK_COMMANDS: readonly PathCommand[];
9
+ export type ConnectionKind = "brace" | "bracket";
10
+ export interface ConnectionSpec {
11
+ kind: ConnectionKind;
12
+ from?: number;
13
+ to?: number;
14
+ }
15
+ /** 开放端点在声部收集完毕后补全 */
16
+ export type VoiceConnection = Required<ConnectionSpec>;
17
+ export declare function parseConnections(value: string, span: SourceSpan): ConnectionSpec[];
18
+ export declare function resolveConnections(specs: readonly ConnectionSpec[], count: number, span: SourceSpan): VoiceConnection[];
19
+ export declare function serializeConnections(specs: readonly ConnectionSpec[]): string;
@@ -0,0 +1,88 @@
1
+ import { ErrorDiagnostic } from "../../diagnostic.js";
2
+ import { pathBounds } from "../../layout/path.js";
3
+ export const CURVED_BRACE_COMMANDS = [
4
+ { op: "M", x: 2.98438, y: -165.5 },
5
+ { op: "C", cx1: 8.9375, cy1: -171.781, cx2: 20.5156, cy2: -184.688, x: 20.5156, y: -208.531 },
6
+ { op: "C", cx1: 20.5156, cy1: -238.641, cx2: 7.60938, cy2: -269.094, x: 7.60938, y: -287.969 },
7
+ { op: "C", cx1: 7.60938, cy1: -308.156, cx2: 19.2031, cy2: -327.016, x: 19.8594, y: -327.688 },
8
+ { op: "C", cx1: 19.8594, cy1: -328.016, cx2: 19.8594, cy2: -328.016, x: 19.8594, y: -328.344 },
9
+ { op: "C", cx1: 19.8594, cy1: -328.672, cx2: 19.8594, cy2: -329, x: 19.5313, y: -329.344 },
10
+ { op: "C", cx1: 19.2031, cy1: -329.344, cx2: 18.5313, cy2: -329.344, x: 18.2031, y: -328.672 },
11
+ { op: "C", cx1: 17.875, cy1: -328.344, cx2: 0, cy2: -310.469, x: 0, y: -283.328 },
12
+ { op: "C", cx1: 0, cy1: -254.203, cx2: 11.5781, cy2: -233.688, x: 11.5781, y: -195.625 },
13
+ { op: "C", cx1: 11.5781, cy1: -188.328, cx2: 8.60938, cy2: -176.75, x: 0.328125, y: -166.484 },
14
+ { op: "C", cx1: 0, cy1: -166.484, cx2: 0, cy2: -165.828, x: 0, y: -165.5 },
15
+ { op: "C", cx1: -0.328125, cy1: -165.172, cx2: 0, cy2: -164.828, x: 0.328125, y: -164.5 },
16
+ { op: "C", cx1: 8.60938, cy1: -154.25, cx2: 11.5781, cy2: -142.656, x: 11.5781, y: -135.375 },
17
+ { op: "C", cx1: 11.5781, cy1: -97.3125, cx2: 0, cy2: -76.7969, x: 0, y: -47.6563 },
18
+ { op: "C", cx1: 0, cy1: -20.8594, cx2: 17.875, cy2: -2.64063, x: 18.2031, y: -2.3125 },
19
+ { op: "C", cx1: 18.5313, cy1: -1.65625, cx2: 19.2031, cy2: -1.32813, x: 19.5313, y: -1.65625 },
20
+ { op: "C", cx1: 19.8594, cy1: -1.98438, cx2: 20.1875, cy2: -2.3125, x: 19.8594, y: -2.64063 },
21
+ { op: "C", cx1: 19.8594, cy1: -2.64063, cx2: 19.8594, cy2: -2.98438, x: 19.8594, y: -3.3125 },
22
+ { op: "C", cx1: 19.2031, cy1: -3.96875, cx2: 7.60938, cy2: -22.8438, x: 7.60938, y: -43.0313 },
23
+ { op: "C", cx1: 7.60938, cy1: -61.8906, cx2: 20.5156, cy2: -92.3438, x: 20.5156, y: -122.469 },
24
+ { op: "C", cx1: 20.5156, cy1: -146.625, cx2: 8.9375, cy2: -159.203, x: 2.98438, y: -165.5 },
25
+ { op: "Z" },
26
+ ];
27
+ export const CURVED_BRACE_BOUNDS = pathBounds(CURVED_BRACE_COMMANDS);
28
+ /** 方括线端钩,以粗竖线宽度为单位,绘制时上下镜像 */
29
+ export const BRACKET_HOOK_REACH = 2.33;
30
+ export const BRACKET_HOOK_DROP = 1.17;
31
+ const BRACKET_HOOK_BASE = 0.34;
32
+ export const BRACKET_HOOK_COMMANDS = [
33
+ { op: "M", x: 0, y: 0 },
34
+ { op: "L", x: 0, y: -BRACKET_HOOK_BASE },
35
+ {
36
+ op: "C",
37
+ cx1: BRACKET_HOOK_REACH * 0.421, cy1: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP * 0.077,
38
+ cx2: BRACKET_HOOK_REACH * 0.733, cy2: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP * 0.346,
39
+ x: BRACKET_HOOK_REACH, y: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP,
40
+ },
41
+ { op: "L", x: BRACKET_HOOK_REACH * 0.929, y: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP },
42
+ {
43
+ op: "C",
44
+ cx1: BRACKET_HOOK_REACH * 0.696, cy1: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP * 0.535,
45
+ cx2: BRACKET_HOOK_REACH * 0.328, cy2: -BRACKET_HOOK_BASE + BRACKET_HOOK_DROP * 0.352,
46
+ x: 0, y: 0,
47
+ },
48
+ { op: "Z" },
49
+ ];
50
+ function invalidConnection(span) {
51
+ return new ErrorDiagnostic("E_VOICES_CONNECT", '@voices 的 connect 必须是 [起点-终点] 或 {起点-终点} 的组合;端点可省略,否则须为有效声部编号。空字符串表示只有连谱线。', span);
52
+ }
53
+ export function parseConnections(value, span) {
54
+ const source = value.trim();
55
+ // 粘连匹配必须消费整串,不能跳过非法字符
56
+ // 非空数字与其后空白一起可选,避免省略端点时重复回溯
57
+ const pattern = /\s*([\[{])\s*(?:(\d+)\s*)?-\s*(?:(\d+)\s*)?([\]}])\s*/y;
58
+ const result = [];
59
+ while (pattern.lastIndex < source.length) {
60
+ const match = pattern.exec(source);
61
+ if (!match || match[4] !== (match[1] === "[" ? "]" : "}"))
62
+ throw invalidConnection(span);
63
+ const from = match[2] ? Number(match[2]) : undefined;
64
+ const to = match[3] ? Number(match[3]) : undefined;
65
+ for (const endpoint of [from, to]) {
66
+ if (endpoint !== undefined && (!Number.isSafeInteger(endpoint) || endpoint < 1)) {
67
+ throw invalidConnection(span);
68
+ }
69
+ }
70
+ if (from !== undefined && to !== undefined && from > to)
71
+ throw invalidConnection(span);
72
+ result.push({ kind: match[1] === "[" ? "bracket" : "brace", from, to });
73
+ }
74
+ return result;
75
+ }
76
+ export function resolveConnections(specs, count, span) {
77
+ return specs.map(({ kind, from = 1, to = count }) => {
78
+ if (from > to || to > count)
79
+ throw invalidConnection(span);
80
+ return { kind, from, to };
81
+ });
82
+ }
83
+ export function serializeConnections(specs) {
84
+ return specs.map(({ kind, from, to }) => {
85
+ const range = `${from ?? ""}-${to ?? ""}`;
86
+ return kind === "brace" ? `{${range}}` : `[${range}]`;
87
+ }).join("");
88
+ }