jpfun 0.5.0 → 0.7.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 (79) hide show
  1. package/dist/converter/midi/convert.d.ts +3 -1
  2. package/dist/converter/midi/convert.js +78 -66
  3. package/dist/converter/musicxml/features.d.ts +19 -5
  4. package/dist/converter/musicxml/features.js +46 -8
  5. package/dist/converter/musicxml/index.js +142 -23
  6. package/dist/converter/musicxml/model.d.ts +15 -0
  7. package/dist/diagnostic.js +1 -1
  8. package/dist/functions/ASTtypes.d.ts +4 -0
  9. package/dist/functions/ASTtypes.js +25 -14
  10. package/dist/functions/adjust/index.js +6 -2
  11. package/dist/functions/arpeggio/index.js +21 -17
  12. package/dist/functions/arpeggio/shape.d.ts +1 -1
  13. package/dist/functions/arpeggio/shape.js +27 -21
  14. package/dist/functions/bar/index.js +37 -36
  15. package/dist/functions/beam/auto.js +18 -24
  16. package/dist/functions/beam/layout.js +15 -23
  17. package/dist/functions/box/index.js +52 -1
  18. package/dist/functions/br/index.js +3 -4
  19. package/dist/functions/default.js +10 -5
  20. package/dist/functions/div/index.js +2 -3
  21. package/dist/functions/gliss/index.d.ts +2 -0
  22. package/dist/functions/gliss/index.js +180 -0
  23. package/dist/functions/grace/index.d.ts +4 -2
  24. package/dist/functions/grace/index.js +19 -11
  25. package/dist/functions/head/index.js +50 -62
  26. package/dist/functions/meter/index.js +45 -24
  27. package/dist/functions/meter/shape.d.ts +3 -0
  28. package/dist/functions/meter/shape.js +43 -0
  29. package/dist/functions/note/accidentals.js +10 -16
  30. package/dist/functions/note/index.js +38 -48
  31. package/dist/functions/ottava/index.d.ts +2 -0
  32. package/dist/functions/ottava/index.js +217 -0
  33. package/dist/functions/page/index.js +3 -3
  34. package/dist/functions/pagebreak/index.d.ts +2 -0
  35. package/dist/functions/pagebreak/index.js +36 -0
  36. package/dist/functions/rest/index.d.ts +2 -0
  37. package/dist/functions/rest/index.js +231 -0
  38. package/dist/functions/slide/index.d.ts +2 -0
  39. package/dist/functions/slide/index.js +132 -0
  40. package/dist/functions/stack/index.js +8 -15
  41. package/dist/functions/symbol/index.d.ts +9 -0
  42. package/dist/functions/symbol/index.js +41 -12
  43. package/dist/functions/symbol/ornament.js +15 -20
  44. package/dist/functions/symbol/shape.js +4 -1
  45. package/dist/functions/symbol/symbols/fine.js +1 -1
  46. package/dist/functions/symbol/symbols/tr.js +46 -1
  47. package/dist/functions/temporal.d.ts +36 -12
  48. package/dist/functions/temporal.js +29 -9
  49. package/dist/functions/tie/index.js +46 -32
  50. package/dist/functions/tuplet/index.js +1 -1
  51. package/dist/functions/up/index.js +51 -33
  52. package/dist/functions/voice/connections.d.ts +19 -0
  53. package/dist/functions/voice/connections.js +90 -0
  54. package/dist/functions/voice/index.js +245 -99
  55. package/dist/functions/volta/index.js +41 -21
  56. package/dist/jpfun.core.min.js +48 -55
  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 +57 -64
  60. package/dist/layout/engine.d.ts +4 -1
  61. package/dist/layout/engine.js +232 -75
  62. package/dist/layout/model.js +22 -66
  63. package/dist/layout/page.d.ts +3 -2
  64. package/dist/layout/page.js +7 -6
  65. package/dist/layout/types.d.ts +34 -45
  66. package/dist/lowering/loweringContext.d.ts +1 -1
  67. package/dist/lowering/loweringContext.js +41 -171
  68. package/dist/parser/parserContext.d.ts +2 -0
  69. package/dist/parser/parserContext.js +31 -14
  70. package/dist/parser/preprocess.js +4 -6
  71. package/dist/playback/compile.js +12 -3
  72. package/dist/playback/types.d.ts +4 -2
  73. package/dist/render/paint.js +1 -1
  74. package/dist/render/svg.js +1 -0
  75. package/dist/render/text.d.ts +1 -1
  76. package/dist/render/text.js +3 -2
  77. package/dist/render/types.d.ts +1 -0
  78. package/grammar.md +221 -257
  79. package/package.json +1 -1
@@ -12,7 +12,7 @@ import type { Painter } from "../render/types.js";
12
12
  * 沿记谱顺序流动的时间状态
13
13
  *
14
14
  * 速度、力度、调性、音色是系统级字段:有确定的类型和初值,任何时刻都能直接读,不用写兼容分支。
15
- * 其余键由具体函数自行约定,核心不认识。
15
+ * 拍号仅在显式设置后有值;其余自定义键由具体函数约定,核心不认识。
16
16
  *
17
17
  * velocity 和 program 按音轨各自流动,新音轨继承分叉处父轨的值;其余键整篇共享。
18
18
  */
@@ -21,17 +21,29 @@ export interface TimeState {
21
21
  velocity: number;
22
22
  keySignature: string;
23
23
  program: number;
24
+ /** 当前小节时长(QN);尚无显式拍号或散板时为 undefined */
25
+ meter?: Fraction;
26
+ /** 当前执行的全局事件;复合体内部回调共用该位置 */
27
+ currentEvent?: TemporalNodeBase;
24
28
  [key: string]: any;
25
29
  }
26
30
  export declare const DEFAULT_BPM = 120;
27
31
  export declare const DEFAULT_VELOCITY = 80;
28
32
  export declare const DEFAULT_TONALITY = "C4";
29
33
  export declare const DEFAULT_PROGRAM = 0;
34
+ /** 每个事件的可选语义声明;核心字段与函数私有的 @键分开 */
35
+ export interface TemporalAddon {
36
+ /** 请求在当前时间列之前换几行,缺省为 0 */
37
+ breakBefore?: number;
38
+ /** 在所属行之前强制换页;配合 breakBefore 使用,布尔值控制上一页的余白分配 */
39
+ pageBreakBefore?: boolean;
40
+ [key: `@${string}`]: unknown;
41
+ }
30
42
  /**
31
43
  * 事件的合并组:同一时刻 mergeKey 相等的事件归并成一列
32
44
  *
33
45
  * 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
34
- * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 br,-2 声部名
46
+ * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 换行/换页,-2 声部名
35
47
  */
36
48
  /** 时间对齐点(小节线);必须是最小值,最先出队 */
37
49
  export declare const ANCHOR_KEY: number;
@@ -48,12 +60,12 @@ export declare class TemporalNodeBase implements TimeLineEvent {
48
60
  track: Track;
49
61
  ast: ASTNodeBase;
50
62
  order: number;
51
- addon?: Record<string, any>;
63
+ addon?: TemporalAddon;
52
64
  /**
53
65
  * 时间列合并组:同一时刻 mergeKey 相等的事件归并成一列
54
66
  *
55
67
  * 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
56
- * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 br,-2 声部名
68
+ * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 换行/换页,-2 声部名
57
69
  */
58
70
  mergeKey: number;
59
71
  /**
@@ -63,20 +75,30 @@ export declare class TemporalNodeBase implements TimeLineEvent {
63
75
  * 按全局事件工作的消费者可以沿链上溯到宿主。
64
76
  */
65
77
  foldedInto?: TemporalNodeBase;
66
- /** 请求在自己所在时间列之前换几行,默认 0 */
67
- breakBefore: number;
68
78
  /** 由 LoweringContext 在列归并后固化的谱面行号 */
69
79
  layoutLine: number;
70
80
  /** 不可见状态事件保持 undefined */
71
81
  box?: LayoutBox;
82
+ /** 显式微调时才创建后置平移,折叠成员还会继承外层对象的平移 */
83
+ placementOffset?: LayoutPoint;
72
84
  /**
73
85
  * 横向弹簧布局参数
74
86
  * 大部分对象使用空对象即可采用布局器默认值
75
87
  */
76
88
  springConfig: HorizontalSpringConfig;
77
89
  /**
78
- * 关系函数可使用的命名局部坐标
79
- * prepareLayout 每次执行时由具体节点和装饰函数重新生成
90
+ * 装饰与关系函数使用的命名局部坐标,相对 box 左上角,每轮布局重建。
91
+ *
92
+ * 当前内置端口:
93
+ * - body.left / body.right:主体核心左右边界,供 div、tuplet、volta、ottava 使用;缺省为盒左右边界。
94
+ * - decoration.below:下方装饰排列起点(只读 y);缺省为正文 box.h。
95
+ * - shoulder:倚音的纵向定位基准(只读 y);音符提供数字顶边,缺省为盒顶。
96
+ * - tie.top:连音线端点;缺省为 anchor 与内容上沿,up 提供最上层成员的端点。
97
+ * - div.{level}.left / div.{level}.right:从 0 编号的减时线端点,由 div.place 发布,供 beam 接管。
98
+ * - dot:附点起点;缺省为盒右边界与 visualAxis,音符覆盖为字形附近的位置。
99
+ * - lyric:歌词对齐点(voice 只读 x);未提供则不作为歌词目标,note 在 finalizeLayout 发布。
100
+ *
101
+ * up、grace、arpeggio 转发宿主端口并换算局部坐标;端口不表达装饰是否存在。
80
102
  */
81
103
  ports: Record<string, LayoutPoint>;
82
104
  /**
@@ -89,8 +111,8 @@ export declare class TemporalNodeBase implements TimeLineEvent {
89
111
  * 1. layout 的 prepareLayoutHost 开始时创建或清空数组;
90
112
  * 2. Temporal.prepareLayout 可先加入节点自身的装饰,例如下八度点;
91
113
  * 3. 引擎再通过 addon 对应的 layoutDecorationHandler 加入函数装饰;
92
- * 4. arrangeBelowDecorations 按 below.order 分配主体下方空间并调用 below.place;
93
- * 5. 最终 paintLayout 在主体 paint 后依次调用 decoration.paint。
114
+ * 4. 引擎分别按 above/below.order 分配空间并调用 place
115
+ * 5. 最终 paintLayout 在主体 paint 后调用可选的 decoration.paint
94
116
  *
95
117
  * 数组必须保留到绘制结束,因为 decoration 通常以闭包保存本次测量得到的几何。它只属于当前 layout pass,不能跨 pass 复用。
96
118
  * prepare 阶段生成、place 后冻结、paint 后失效
@@ -103,9 +125,9 @@ export declare class TemporalNodeBase implements TimeLineEvent {
103
125
  protected initLayoutBox(): LayoutBox;
104
126
  /**
105
127
  * 时间状态 修改&冻结 入口
106
- * 调用时机在时间位置已经确定之后,处理“调性、速度、拍号”等时间信息的固化
128
+ * 当前列定位后固化状态;偏移仅支持非负值,整列取最大值累加到后续列
107
129
  */
108
- onTimeState?(state: TimeState): void;
130
+ onTimeState?(state: TimeState): Fraction | void;
109
131
  /** 把已固化的音乐语义声明为播放事件;不执行实时播放 */
110
132
  emitPlayback?(emitter: PlaybackEmitter): void;
111
133
  /** 给自己所在的时间列贴播放标记,供控制流游标查询 */
@@ -133,6 +155,8 @@ export declare class TemporalNodeBase implements TimeLineEvent {
133
155
  /** 只执行绘制,不允许在这里重新测量或改变 LayoutBox */
134
156
  paint(_painter: Painter): void;
135
157
  }
158
+ /** 复合体下放函数语义时,核心声明仍留在全局事件上 */
159
+ export declare function transferFunctionAddons(source: TemporalNodeBase, target: TemporalNodeBase): void;
136
160
  /** 可以进入视觉布局的 Temporal,同时满足 decoration 所需的 LayoutHost 协议 */
137
161
  export type VisualTemporalNode = TemporalNodeBase & LayoutHost;
138
162
  export declare function isVisualTemporalNode(node: TemporalNodeBase): node is VisualTemporalNode;
@@ -7,7 +7,7 @@ export const DEFAULT_PROGRAM = 0;
7
7
  * 事件的合并组:同一时刻 mergeKey 相等的事件归并成一列
8
8
  *
9
9
  * 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
10
- * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 br,-2 声部名
10
+ * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 换行/换页,-2 声部名
11
11
  */
12
12
  /** 时间对齐点(小节线);必须是最小值,最先出队 */
13
13
  export const ANCHOR_KEY = -Infinity;
@@ -26,12 +26,12 @@ export class TemporalNodeBase {
26
26
  track; // 所在的纵向音轨;同轨判断使用引用相等
27
27
  ast; // 对应的 AST 节点
28
28
  order; // 事件创建序号,作为id。可用于区分同时发生的父子、排序同一时刻发生的事件
29
- addon; // 其他任意字段,仅在存在已固化的函数语义时创建,比如存储 div 和 dot 的数目
29
+ addon; // 仅在存在语义声明时创建,比如换行或 div 和 dot 的数目
30
30
  /**
31
31
  * 时间列合并组:同一时刻 mergeKey 相等的事件归并成一列
32
32
  *
33
33
  * 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
34
- * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 br,-2 声部名
34
+ * 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 换行/换页,-2 声部名
35
35
  */
36
36
  mergeKey;
37
37
  /**
@@ -41,8 +41,6 @@ export class TemporalNodeBase {
41
41
  * 按全局事件工作的消费者可以沿链上溯到宿主。
42
42
  */
43
43
  foldedInto;
44
- /** 请求在自己所在时间列之前换几行,默认 0 */
45
- breakBefore = 0;
46
44
  /** 由 LoweringContext 在列归并后固化的谱面行号 */
47
45
  layoutLine = 0;
48
46
  /** 不可见状态事件保持 undefined */
@@ -53,8 +51,18 @@ export class TemporalNodeBase {
53
51
  */
54
52
  springConfig;
55
53
  /**
56
- * 关系函数可使用的命名局部坐标
57
- * prepareLayout 每次执行时由具体节点和装饰函数重新生成
54
+ * 装饰与关系函数使用的命名局部坐标,相对 box 左上角,每轮布局重建。
55
+ *
56
+ * 当前内置端口:
57
+ * - body.left / body.right:主体核心左右边界,供 div、tuplet、volta、ottava 使用;缺省为盒左右边界。
58
+ * - decoration.below:下方装饰排列起点(只读 y);缺省为正文 box.h。
59
+ * - shoulder:倚音的纵向定位基准(只读 y);音符提供数字顶边,缺省为盒顶。
60
+ * - tie.top:连音线端点;缺省为 anchor 与内容上沿,up 提供最上层成员的端点。
61
+ * - div.{level}.left / div.{level}.right:从 0 编号的减时线端点,由 div.place 发布,供 beam 接管。
62
+ * - dot:附点起点;缺省为盒右边界与 visualAxis,音符覆盖为字形附近的位置。
63
+ * - lyric:歌词对齐点(voice 只读 x);未提供则不作为歌词目标,note 在 finalizeLayout 发布。
64
+ *
65
+ * up、grace、arpeggio 转发宿主端口并换算局部坐标;端口不表达装饰是否存在。
58
66
  */
59
67
  ports;
60
68
  /**
@@ -67,8 +75,8 @@ export class TemporalNodeBase {
67
75
  * 1. layout 的 prepareLayoutHost 开始时创建或清空数组;
68
76
  * 2. Temporal.prepareLayout 可先加入节点自身的装饰,例如下八度点;
69
77
  * 3. 引擎再通过 addon 对应的 layoutDecorationHandler 加入函数装饰;
70
- * 4. arrangeBelowDecorations 按 below.order 分配主体下方空间并调用 below.place;
71
- * 5. 最终 paintLayout 在主体 paint 后依次调用 decoration.paint。
78
+ * 4. 引擎分别按 above/below.order 分配空间并调用 place
79
+ * 5. 最终 paintLayout 在主体 paint 后调用可选的 decoration.paint
72
80
  *
73
81
  * 数组必须保留到绘制结束,因为 decoration 通常以闭包保存本次测量得到的几何。它只属于当前 layout pass,不能跨 pass 复用。
74
82
  * prepare 阶段生成、place 后冻结、paint 后失效
@@ -96,6 +104,18 @@ export class TemporalNodeBase {
96
104
  /** 只执行绘制,不允许在这里重新测量或改变 LayoutBox */
97
105
  paint(_painter) { }
98
106
  }
107
+ /** 复合体下放函数语义时,核心声明仍留在全局事件上 */
108
+ export function transferFunctionAddons(source, target) {
109
+ const addon = source.addon;
110
+ if (!addon)
111
+ return;
112
+ for (const key of Object.keys(addon).filter((key) => key.startsWith("@"))) {
113
+ (target.addon ??= {})[key] = addon[key];
114
+ delete addon[key];
115
+ }
116
+ if (!Object.keys(addon).length)
117
+ source.addon = undefined;
118
+ }
99
119
  export function isVisualTemporalNode(node) {
100
120
  return node.box !== undefined;
101
121
  }
@@ -2,10 +2,15 @@ import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { isVisualTemporalNode, } from "../temporal.js";
4
4
  import { pathBounds } from "../../layout/path.js";
5
+ import { getLayoutBounds } from "../../layout/engine.js";
5
6
  /** 三次贝塞尔控制点的抬高系数:两个控制点同高时,实际弧高恰好是抬高的 3/4 */
6
7
  const CUBIC_LIFT = 4 / 3;
7
8
  /** 弧带中部最大厚度与字号之比 */
8
9
  const THICKNESS_RATIO = 0.09;
10
+ /** 参考弧线的跨度约为弧高的 5 倍;在 2.5em 处平滑达到 0.5em 上限 */
11
+ const AUTO_SPAN_RATIO = 2.5;
12
+ const AUTO_HEIGHT_RATIO = 0.5;
13
+ const CROSS_LINE_HEIGHT_RATIO = 0.45;
9
14
  class TieFunction extends ASTFunctionNode {
10
15
  static def = {
11
16
  name: ["tie"],
@@ -15,25 +20,29 @@ class TieFunction extends ASTFunctionNode {
15
20
  1@a 2 3@b @tie(a, b, height=0.5em)
16
21
  ~~~
17
22
  - **位置参数**:端点标签,可传多个,按顺序连接;省略时查找最近的可标记对象,包括增时线
18
- - **height**:弧线高度,默认 \`0.5em\`
23
+ - **height**:仅命名的弧线高度参数,默认 \`0\`(自动);非正数按水平跨度自动调整,在 \`2.5em\` 跨度处平滑达到 \`0.5em\` 上限,正数指定固定弧高。em 相对调用处字号;可用 \`@set(tie.height=0.8em)\` 设置局部默认值,显式传参优先
19
24
 
20
- 同行端点用一条弧线连接,跨行时自动拆成多段。同轨同音且时间连续的音段建立播放连接,各段保留自己的装饰,例如 \`1 ^ $tr 1 @tie()\` 的后一音段不继承颤音。涉及增时线的连接仅绘制,不合并播放音符。`,
25
+ 同行端点用一条弧线连接,自动高度按水平跨度计算;跨行时自动拆成多段,自动模式下各段统一使用 \`0.45em\` 高度。同轨同音且时间连续的音段建立播放连接,各段保留自己的装饰,例如 \`1 ^ $tr 1 @tie()\` 的后一音段不继承颤音。涉及增时线的连接仅绘制,不合并播放音符。`,
21
26
  allowExtraArgs: true,
22
27
  extraArgType: "label",
23
- args: []
28
+ args: [{
29
+ name: "height",
30
+ namedOnly: true,
31
+ description: "弧线高度,支持 px/em;em 相对调用处字号,非正数自动调整,最高 0.5em",
32
+ type: "length",
33
+ default: { value: 0, unit: "em" },
34
+ }]
24
35
  };
25
36
  endPoints = [];
26
37
  height;
38
+ fontSize;
27
39
  constructor(sourceSpan, args, ctx, parent = null) {
28
40
  super(sourceSpan, parent);
29
- let height = ctx.variables.fontsize * 0.5;
41
+ // 命名选项统一解析默认值,位置参数仍全部作为端点
42
+ const [height] = this.getArgValue(args, ctx);
30
43
  for (const [key, value] of args) {
31
- if (key === "height") {
32
- const length = ctx.parseArgWithType(value.valueSpan, "length", sourceSpan.start);
33
- if (length)
34
- height = Math.max(0, ctx.length2px(length));
44
+ if (key === "height")
35
45
  continue;
36
- }
37
46
  const v = value instanceof ASTFunctionNode
38
47
  ? value
39
48
  : ctx.parseArgWithType(value.valueSpan, "label", sourceSpan.start);
@@ -50,7 +59,8 @@ class TieFunction extends ASTFunctionNode {
50
59
  }
51
60
  if (this.endPoints.length < 2)
52
61
  throw new ErrorDiagnostic("E_NOT_ENOUGH_ARGS", "@tie 连音线需要至少两个端点", sourceSpan);
53
- this.height = height;
62
+ this.height = Math.max(0, ctx.length2px(height));
63
+ this.fontSize = ctx.variables.fontsize;
54
64
  }
55
65
  /**
56
66
  * tie 不推进时间
@@ -68,7 +78,7 @@ class TieFunction extends ASTFunctionNode {
68
78
  ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("tie", this.sourceSpan));
69
79
  return [];
70
80
  }
71
- ctx.addAttachment(new TieLayoutAttachment(endPoints, this.height, this.sourceSpan));
81
+ ctx.addAttachment(new TieLayoutAttachment(endPoints, this.height, this.fontSize, this.sourceSpan));
72
82
  return [];
73
83
  }
74
84
  toString(source) {
@@ -84,11 +94,13 @@ class TieLayoutAttachment {
84
94
  sourceSpan;
85
95
  endPoints;
86
96
  height;
97
+ fontSize;
87
98
  /** 弧带中部厚度;两端收尖到 0。端点字号不随布局变化,可以一次算定 */
88
99
  thickness;
89
- constructor(endPoints, height, sourceSpan) {
100
+ constructor(endPoints, height, fontSize, sourceSpan) {
90
101
  this.endPoints = endPoints;
91
102
  this.height = height;
103
+ this.fontSize = fontSize;
92
104
  this.sourceSpan = sourceSpan;
93
105
  const size = endPoints.reduce((current, endpoint) => Math.max(current, endpoint.ast.size), 0);
94
106
  this.thickness = Math.max(1.2, size * THICKNESS_RATIO);
@@ -157,7 +169,7 @@ class TieLayoutAttachment {
157
169
  }
158
170
  return {
159
171
  x: node.box.x + node.box.anchor,
160
- y: node.box.y,
172
+ y: getLayoutBounds(node, true).y,
161
173
  };
162
174
  }
163
175
  /** 根据端点固化后的行号选择同行弧线或跨行分段 */
@@ -172,24 +184,29 @@ class TieLayoutAttachment {
172
184
  const start = this.tiePort(startNode);
173
185
  const end = this.tiePort(endNode);
174
186
  if (startNode.layoutLine === endNode.layoutLine) {
175
- const startTop = context.getVisualAxis(startNode.layoutLine, startNode.track)
176
- + (context.getHostExtent(startNode.layoutLine, startNode.track)?.top ?? 0);
177
- const endTop = context.getVisualAxis(endNode.layoutLine, endNode.track)
178
- + (context.getHostExtent(endNode.layoutLine, endNode.track)?.top ?? 0);
187
+ const startTop = this.hostTop(context, startNode.layoutLine, startNode.track);
188
+ const endTop = this.hostTop(context, endNode.layoutLine, endNode.track);
179
189
  segments.push(this.arcSegment(start, end, startNode.layoutLine, startTop <= endTop ? startNode.track : endNode.track));
180
190
  continue;
181
191
  }
192
+ const height = this.height > 0 ? this.height : this.fontSize * CROSS_LINE_HEIGHT_RATIO;
182
193
  // 首行先起弧,再沿水平段延伸到当前谱面行最右侧
183
- segments.push(this.openingSegment(start, this.lineRightX(context, startNode.layoutLine), this.plateauY(context, startNode.layoutLine, startNode.track, start.y), startNode.layoutLine, startNode.track));
194
+ segments.push(this.openingSegment(start, this.lineRightX(context, startNode.layoutLine), height, startNode.layoutLine, startNode.track));
184
195
  // 中间行可能没有任何可见对象,仍然需要逐行补一段水平线
185
196
  for (let line = startNode.layoutLine + 1; line < endNode.layoutLine; line++) {
186
- segments.push(this.horizontalSegment(this.lineEntryX(context, line, startNode.track), this.lineRightX(context, line), this.plateauY(context, line, startNode.track), line, startNode.track));
197
+ segments.push(this.horizontalSegment(this.lineEntryX(context, line, startNode.track), this.lineRightX(context, line), this.hostTop(context, line, startNode.track) - height, line, startNode.track));
187
198
  }
188
199
  // 跳过声部名、大括号等位于端点列之前的系统前缀,再落弧到终点
189
- segments.push(this.closingSegment(this.lineEntryX(context, endNode.layoutLine, endNode.track), end, this.plateauY(context, endNode.layoutLine, endNode.track, end.y), endNode.layoutLine, endNode.track));
200
+ segments.push(this.closingSegment(this.lineEntryX(context, endNode.layoutLine, endNode.track), end, height, endNode.layoutLine, endNode.track));
190
201
  }
191
202
  return segments;
192
203
  }
204
+ resolveHeight(span) {
205
+ if (this.height > 0)
206
+ return this.height;
207
+ const ratio = Math.min(1, span / (this.fontSize * AUTO_SPAN_RATIO));
208
+ return this.fontSize * AUTO_HEIGHT_RATIO * ratio * (2 - ratio);
209
+ }
193
210
  lineEntryX(context, lineIndex, track) {
194
211
  const line = context.lines[lineIndex];
195
212
  const host = line.trackRuns.get(track)?.find(host => !host.T.isZero());
@@ -206,13 +223,8 @@ class TieLayoutAttachment {
206
223
  }
207
224
  return right;
208
225
  }
209
- /** 有端点时从端点固定抬高;中间段没有端点,才以当前 Track 的主体顶部为基准 */
210
- plateauY(context, line, track, endpointY = Infinity) {
211
- if (Number.isFinite(endpointY))
212
- return endpointY - this.height;
213
- const axis = context.getVisualAxis(line, track);
214
- const hostTop = context.getHostExtent(line, track)?.top ?? 0;
215
- return axis + hostTop - this.height;
226
+ hostTop(context, line, track) {
227
+ return context.getVisualAxis(line, track) + (context.getHostExtent(line, track)?.top ?? 0);
216
228
  }
217
229
  /** 一条三次贝塞尔的外缘配上反向内缘,得到两端收尖、中部最厚的乐谱弧线 */
218
230
  arcSegment(startInput, endInput, line, track) {
@@ -223,7 +235,7 @@ class TieLayoutAttachment {
223
235
  const run = (end.x - start.x) * 0.25;
224
236
  const leftX = start.x + run;
225
237
  const rightX = end.x - run;
226
- const lift = this.height * CUBIC_LIFT;
238
+ const lift = this.resolveHeight(end.x - start.x) * CUBIC_LIFT;
227
239
  const inner = Math.max(0, lift - this.thickness * CUBIC_LIFT);
228
240
  const commands = [
229
241
  { op: "M", x: start.x, y: start.y },
@@ -234,8 +246,9 @@ class TieLayoutAttachment {
234
246
  return makeSegment(commands, line, track);
235
247
  }
236
248
  /** 跨行首段:从端点起弧,随后水平延伸到右边界 */
237
- openingSegment(start, right, plateauY, line, track) {
238
- const run = this.height * 1.2;
249
+ openingSegment(start, right, height, line, track) {
250
+ const plateauY = start.y - height;
251
+ const run = height * 1.2;
239
252
  right = Math.max(right, start.x + run);
240
253
  const curveX = start.x + run;
241
254
  const controlX = start.x + run * 0.45;
@@ -251,8 +264,9 @@ class TieLayoutAttachment {
251
264
  ], line, track);
252
265
  }
253
266
  /** 跨行末段:从左边界水平进入,最后落弧到端点 */
254
- closingSegment(left, end, plateauY, line, track) {
255
- const run = this.height * 1.2;
267
+ closingSegment(left, end, height, line, track) {
268
+ const plateauY = end.y - height;
269
+ const run = height * 1.2;
256
270
  left = Math.min(left, end.x - run);
257
271
  const curveX = end.x - run;
258
272
  const controlX = end.x - run * 0.45;
@@ -77,7 +77,7 @@ class TupletFunction extends ASTFunctionNode {
77
77
  shortest.copyFrom(event.T);
78
78
  writtenTotal.add(event.T);
79
79
  }
80
- const units = writtenTotal.clone().div(shortest);
80
+ const units = writtenTotal.div(shortest);
81
81
  const actual = units.numerator;
82
82
  if (units.denominator !== 1 || actual < 2 || !Number.isSafeInteger(actual)) {
83
83
  throw new ErrorDiagnostic("E_TUPLET_NON_INTEGRAL_UNITS", "@tuplet 的总时值必须是最短正时值的整数倍,且至少包含2个单位", this.sourceSpan);
@@ -1,13 +1,14 @@
1
1
  import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTLabelNode, ASTBraceNode } from "../ASTtypes.js";
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
- import { ANCHOR_KEY, DEFAULT_KEY, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
3
+ import { ANCHOR_KEY, DEFAULT_KEY, transferFunctionAddons, 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,10 +58,10 @@ 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)) {
63
- throw new ErrorDiagnostic("E_UP_INVALID_CHILD", "@up 的每个参数必须恰好产生一个可见 Temporal,且不能包含多声部结构", content.sourceSpan);
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) || (member.addon?.breakBefore ?? 0) > 0) {
64
+ throw new ErrorDiagnostic("E_UP_INVALID_CHILD", "@up 的每个参数必须恰好产生一个可见 Temporal,且不能换行或包含多声部结构", content.sourceSpan);
64
65
  }
65
66
  members.push(member);
66
67
  }
@@ -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,15 +313,13 @@ class FoldTemporal extends TemporalNodeBase {
295
313
  * 宿主留在轨道基线上,其余成员按书写顺序向上或向下叠放
296
314
  *
297
315
  * 成员不进入全局 columns,因此它们的准备、定位和绘制都由本节点负责;
298
- * 每个成员独立执行局部横排,宿主继承含框约束的占位,其余成员仍沿宿主锚点悬挂。
316
+ * 每个成员独立执行局部横排,宿主继承含框约束的占位,其余成员仍沿宿主锚点悬挂。
299
317
  */
300
318
  prepareLayout(context) {
301
319
  // lowering 期间修饰挂在折叠体上(augmenter 要看到整体节奏),渲染时交给宿主:
302
320
  // 减时线要落在它的数字与下八度点之间,而不是压在整个盒子下面
303
- if (this.addon && this.members[0]) {
304
- this.members[0].addon = this.addon;
305
- this.addon = void 0;
306
- }
321
+ if (this.members[0])
322
+ transferFunctionAddons(this, this.members[0]);
307
323
  const first = this.members[0];
308
324
  if (!first) {
309
325
  this.box.w = this.box.h = 0;
@@ -319,18 +335,20 @@ class FoldTemporal extends TemporalNodeBase {
319
335
  const anchor = first.box.x + first.box.anchor;
320
336
  const gap = this.ast.size * 0.12;
321
337
  this.verticalOffsets.length = this.members.length;
322
- this.verticalOffsets.fill(0);
338
+ this.verticalOffsets[0] = 0;
323
339
  // 以宿主盒顶为 0,向上得到负坐标、向下得到正坐标,最后整体下移
324
- let top = 0;
340
+ const firstBounds = getLayoutBounds(first);
341
+ let top = firstBounds.y;
325
342
  for (let i = 1; i < this.aboveCount; i++) {
326
- top -= gap + this.members[i].box.h;
327
- this.verticalOffsets[i] = top;
343
+ const bounds = getLayoutBounds(this.members[i]);
344
+ this.verticalOffsets[i] = top - gap - bounds.y - bounds.h;
345
+ top -= gap + bounds.h;
328
346
  }
329
- let bottom = first.box.h;
347
+ let bottom = firstBounds.y + firstBounds.h;
330
348
  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;
349
+ const bounds = getLayoutBounds(this.members[i]);
350
+ this.verticalOffsets[i] = bottom + gap - bounds.y;
351
+ bottom += gap + bounds.h;
334
352
  }
335
353
  for (let i = 0; i < this.verticalOffsets.length; i++)
336
354
  this.verticalOffsets[i] -= top;
@@ -353,7 +371,7 @@ class FoldTemporal extends TemporalNodeBase {
353
371
  const topOffset = this.verticalOffsets[topIndex];
354
372
  this.ports["tie.top"] = {
355
373
  x: anchor,
356
- y: topOffset + (this.members[topIndex].ports["tie.top"]?.y ?? 0),
374
+ y: topOffset + (this.members[topIndex].ports["tie.top"]?.y ?? getLayoutBounds(this.members[topIndex], true).y),
357
375
  };
358
376
  }
359
377
  /** 引擎每次改变本节点坐标后,按当前 anchor 与准备阶段的纵向偏移重算成员绝对坐标 */
@@ -369,7 +387,7 @@ class FoldTemporal extends TemporalNodeBase {
369
387
  for (const member of this.members) {
370
388
  member.paint(painter);
371
389
  for (const decoration of member.decorations)
372
- decoration.paint(painter);
390
+ decoration.paint?.(painter);
373
391
  }
374
392
  }
375
393
  }
@@ -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;