jpfun 0.8.0 → 1.0.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 (140) hide show
  1. package/README.md +10 -15
  2. package/dist/diagnostic.d.ts +29 -2
  3. package/dist/diagnostic.js +5 -4
  4. package/dist/functions/ASTtypes.d.ts +1 -14
  5. package/dist/functions/ASTtypes.js +5 -36
  6. package/dist/functions/adjust/index.js +45 -33
  7. package/dist/functions/arpeggio/index.js +44 -48
  8. package/dist/functions/arpeggio/shape.d.ts +2 -3
  9. package/dist/functions/arpeggio/shape.js +20 -50
  10. package/dist/functions/bar/index.d.ts +3 -0
  11. package/dist/functions/bar/index.js +21 -11
  12. package/dist/functions/beam/auto.js +54 -123
  13. package/dist/functions/beam/index.js +4 -9
  14. package/dist/functions/beam/layout.js +3 -2
  15. package/dist/functions/box/index.js +1 -6
  16. package/dist/functions/dash/index.js +6 -4
  17. package/dist/functions/div/index.d.ts +3 -5
  18. package/dist/functions/div/index.js +14 -12
  19. package/dist/functions/dot/index.js +3 -9
  20. package/dist/functions/dyn/index.js +7 -26
  21. package/dist/functions/gliss/index.js +99 -91
  22. package/dist/functions/grace/index.d.ts +2 -5
  23. package/dist/functions/grace/index.js +47 -94
  24. package/dist/functions/head/index.js +32 -36
  25. package/dist/functions/key/index.js +9 -12
  26. package/dist/functions/meter/index.d.ts +1 -0
  27. package/dist/functions/meter/index.js +6 -3
  28. package/dist/functions/meter/shape.d.ts +1 -1
  29. package/dist/functions/note/accidentals.d.ts +3 -3
  30. package/dist/functions/note/accidentals.js +29 -16
  31. package/dist/functions/note/index.js +12 -14
  32. package/dist/functions/note/source-transform.d.ts +15 -0
  33. package/dist/functions/note/source-transform.js +75 -71
  34. package/dist/functions/ottava/index.js +12 -32
  35. package/dist/functions/page/index.js +15 -17
  36. package/dist/functions/rest/index.js +10 -10
  37. package/dist/functions/slide/index.js +1 -1
  38. package/dist/functions/stack/index.js +8 -57
  39. package/dist/functions/temporal.d.ts +11 -5
  40. package/dist/functions/temporal.js +22 -2
  41. package/dist/functions/text/index.js +24 -30
  42. package/dist/functions/tie/index.js +42 -41
  43. package/dist/functions/tremolo/index.js +1 -1
  44. package/dist/functions/tuplet/index.js +1 -6
  45. package/dist/functions/up/index.js +82 -88
  46. package/dist/functions/voice/connections.d.ts +3 -2
  47. package/dist/functions/voice/connections.js +48 -10
  48. package/dist/functions/voice/index.d.ts +2 -3
  49. package/dist/functions/voice/index.js +2 -970
  50. package/dist/functions/voice/lyrics.d.ts +34 -0
  51. package/dist/functions/voice/lyrics.js +393 -0
  52. package/dist/functions/voice/voice.d.ts +67 -0
  53. package/dist/functions/voice/voice.js +405 -0
  54. package/dist/functions/voice/voices.d.ts +106 -0
  55. package/dist/functions/voice/voices.js +424 -0
  56. package/dist/functions/volta/index.js +6 -23
  57. package/dist/index.d.ts +97 -3
  58. package/dist/index.js +97 -3
  59. package/dist/jpfun.min.js +63 -67
  60. package/dist/layout/engine.d.ts +4 -3
  61. package/dist/layout/engine.js +190 -24
  62. package/dist/layout/model.d.ts +12 -4
  63. package/dist/layout/model.js +16 -11
  64. package/dist/layout/page.d.ts +2 -1
  65. package/dist/layout/page.js +15 -15
  66. package/dist/layout/path.js +1 -1
  67. package/dist/layout/range.d.ts +7 -6
  68. package/dist/layout/range.js +23 -14
  69. package/dist/layout/types.d.ts +19 -30
  70. package/dist/lowering/loweringContext.d.ts +8 -11
  71. package/dist/lowering/loweringContext.js +7 -13
  72. package/dist/lowering/track.d.ts +8 -1
  73. package/dist/lowering/types.d.ts +12 -8
  74. package/dist/parser/grammarType.d.ts +3 -3
  75. package/dist/parser/parse-utils/brace-utils.js +7 -17
  76. package/dist/parser/parse-utils/call-utils.d.ts +4 -3
  77. package/dist/parser/parse-utils/call-utils.js +23 -66
  78. package/dist/parser/parse-utils/note-utils.d.ts +7 -3
  79. package/dist/parser/parse-utils/note-utils.js +25 -40
  80. package/dist/parser/parse-utils/string-utils.d.ts +1 -1
  81. package/dist/parser/parse-utils/string-utils.js +8 -6
  82. package/dist/parser/parserContext.d.ts +17 -11
  83. package/dist/parser/parserContext.js +111 -65
  84. package/dist/parser/preprocess.d.ts +7 -0
  85. package/dist/parser/preprocess.js +21 -19
  86. package/dist/pipeline.d.ts +1 -1
  87. package/dist/pipeline.js +2 -2
  88. package/dist/playback/compile.js +14 -15
  89. package/dist/playback/event.d.ts +6 -6
  90. package/dist/playback/event.js +13 -49
  91. package/dist/render/canvas.d.ts +0 -2
  92. package/dist/render/canvas.js +50 -56
  93. package/dist/render/index.d.ts +1 -1
  94. package/dist/render/index.js +1 -1
  95. package/dist/render/paint.d.ts +7 -0
  96. package/dist/render/paint.js +70 -20
  97. package/dist/render/recording.d.ts +8 -1
  98. package/dist/render/recording.js +12 -0
  99. package/dist/render/svg.d.ts +5 -2
  100. package/dist/render/svg.js +70 -48
  101. package/dist/render/text.d.ts +37 -2
  102. package/dist/render/text.js +136 -1
  103. package/dist/render/translate.d.ts +3 -1
  104. package/dist/render/translate.js +6 -0
  105. package/dist/render/types.d.ts +9 -2
  106. package/grammar.md +18 -14
  107. package/package.json +8 -18
  108. package/dist/converter/midi/convert.d.ts +0 -42
  109. package/dist/converter/midi/convert.js +0 -729
  110. package/dist/converter/midi/index.d.ts +0 -5
  111. package/dist/converter/midi/index.js +0 -11
  112. package/dist/converter/midi/json.d.ts +0 -38
  113. package/dist/converter/midi/json.js +0 -1
  114. package/dist/converter/midi/triplet.d.ts +0 -30
  115. package/dist/converter/midi/triplet.js +0 -75
  116. package/dist/converter/musicxml/dom.d.ts +0 -25
  117. package/dist/converter/musicxml/dom.js +0 -65
  118. package/dist/converter/musicxml/features.d.ts +0 -114
  119. package/dist/converter/musicxml/features.js +0 -501
  120. package/dist/converter/musicxml/index.d.ts +0 -14
  121. package/dist/converter/musicxml/index.js +0 -1295
  122. package/dist/converter/musicxml/model.d.ts +0 -176
  123. package/dist/converter/musicxml/model.js +0 -1
  124. package/dist/converter/musicxml/resolve.d.ts +0 -10
  125. package/dist/converter/musicxml/resolve.js +0 -152
  126. package/dist/converter/source.d.ts +0 -22
  127. package/dist/converter/source.js +0 -51
  128. package/dist/core.d.ts +0 -24
  129. package/dist/core.js +0 -24
  130. package/dist/iife/core.d.ts +0 -1
  131. package/dist/iife/core.js +0 -3
  132. package/dist/iife/from-midi.d.ts +0 -1
  133. package/dist/iife/from-midi.js +0 -15
  134. package/dist/iife/from-musicxml.d.ts +0 -1
  135. package/dist/iife/from-musicxml.js +0 -3
  136. package/dist/iife/full.d.ts +0 -1
  137. package/dist/iife/full.js +0 -3
  138. package/dist/jpfun.core.min.js +0 -123
  139. package/dist/jpfun.from-midi.min.js +0 -9
  140. package/dist/jpfun.from-musicxml.min.js +0 -12
@@ -1,4 +1,4 @@
1
- import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTBraceNode, ASTLabelNode } from "../ASTtypes.js";
1
+ import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
3
  /**
4
4
  * 临时伴奏紧贴主旋律,用比系统级行距更小的间隙
@@ -65,59 +65,16 @@ class StackFunction extends ASTFunctionNode {
65
65
  const n = nodes[at++];
66
66
  if (!(n.kind === "sugar" && n.data === StackFunction))
67
67
  return null;
68
- // 找上一个非文本节点 实现忽略中间内容的作用
69
- // 另一个做法是如果上一个不是可用节点就报错
70
- let left = ctx.nodes.length - 1;
71
- for (; left >= 0; left--) {
72
- if (ctx.nodes[left] instanceof ASTTextNode)
73
- continue;
74
- break;
75
- }
76
- let leftNode = left >= 0 ? ctx.nodes[left] : null;
77
- if (leftNode === null) {
68
+ const leftNode = ctx.takeLeftOperand();
69
+ if (!leftNode) {
78
70
  throw new ErrorDiagnostic("E_STACK_NO_TARGET", "@stack语法糖错误: 左边没有找到可叠加的目标", n.span);
79
71
  }
80
- /** 左操作数在 ctx.nodes 中的起点;只有这一段会被 stack 吞并,更早的节点必须保留 */
81
- let replaceFrom = left;
82
- // 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
83
- if (leftNode instanceof ASTLabelNode) {
84
- const labelNode = leftNode;
85
- let tgt = labelNode.target;
86
- // 在本层这个节点位于哪里(标签节点可能被wrap)
87
- while (tgt && !ctx.nodes.includes(tgt))
88
- tgt = tgt.parent;
89
- for (let j = left - 1; j >= 0; j--) {
90
- if (ctx.nodes[j] === tgt) {
91
- leftNode = new ASTBraceNode({
92
- start: tgt.sourceSpan.start,
93
- end: labelNode.sourceSpan.end,
94
- }, ctx.nodes.slice(j, left + 1), null);
95
- replaceFrom = j;
96
- break;
97
- }
98
- }
99
- }
100
- // 左操作数收敛成一个 stack,后续合并都在它身上进行
101
72
  const host = leftNode instanceof StackFunction
102
73
  ? leftNode
103
74
  : new StackFunction(n.span, new Map(), ctx).addContent(leftNode);
104
- // 找到下一个非文本节点 通过全量后续解析的方式进行 还是有些trick
105
- const storage = ctx.nodes;
106
- ctx.nodes = [];
107
- ctx.makeNodes(nodes, at);
108
- for (let i = 0; i < ctx.nodes.length; i++) {
109
- // 后向跳过文本节点 和上面保持一致
110
- const right = ctx.nodes[i];
111
- if (right instanceof ASTTextNode)
112
- continue;
113
- host.addContent(right);
114
- storage.length = replaceFrom;
115
- storage.push(host);
116
- while (++i < ctx.nodes.length)
117
- storage.push(ctx.nodes[i]);
118
- ctx.nodes = storage;
119
- return nodes.length;
120
- }
75
+ const next = ctx.consumeRightOperand(nodes, at, right => host.addContent(right));
76
+ if (next !== null)
77
+ return next;
121
78
  throw new ErrorDiagnostic("E_STACK_NO_TARGET", "@stack语法糖错误: 右边没有找到可叠加的目标", n.span);
122
79
  }
123
80
  contents = [];
@@ -131,15 +88,9 @@ class StackFunction extends ASTFunctionNode {
131
88
  }
132
89
  constructor(span, args, ctx, parent = null) {
133
90
  super(span, parent);
134
- for (const [, value] of args) {
135
- if (value instanceof ASTNodeBase) {
91
+ for (const [key, value] of args) {
92
+ if (typeof key === "number")
136
93
  this.addContent(value);
137
- continue;
138
- }
139
- const c = ctx.parseArgWithType(value.valueSpan, "content", span.start);
140
- if (c !== null) {
141
- this.addContent(c);
142
- }
143
94
  }
144
95
  }
145
96
  /** 遇到另一个 stack 就展平;接收者存活,被吞并的成员列表最后清空 */
@@ -2,7 +2,7 @@
2
2
  * 时间线事件的基类
3
3
  * 由 AST 产生,跨越 lowering / layout / playback 三个阶段
4
4
  */
5
- import type { ASTNodeBase } from "./ASTtypes.js";
5
+ import { type ASTNodeBase } from "./ASTtypes.js";
6
6
  import { Fraction } from "../fraction.js";
7
7
  import type { HorizontalSpringConfig, LayoutBox, LayoutDecoration, LayoutHost, LayoutPoint, LayoutPrepareContext, HorizontalLineView, TimeLineEvent } from "../layout/types.js";
8
8
  import type { Track } from "../lowering/track.js";
@@ -23,8 +23,6 @@ export interface TimeState {
23
23
  program: number;
24
24
  /** 当前小节时长(QN);尚无显式拍号或散板时为 undefined */
25
25
  meter?: Fraction;
26
- /** 当前执行的全局事件;复合体内部回调共用该位置 */
27
- currentEvent?: TemporalNodeBase;
28
26
  [key: string]: any;
29
27
  }
30
28
  export declare const DEFAULT_BPM = 120;
@@ -75,6 +73,14 @@ export declare class TemporalNodeBase implements TimeLineEvent {
75
73
  * 按全局事件工作的消费者可以沿链上溯到宿主。
76
74
  */
77
75
  foldedInto?: TemporalNodeBase;
76
+ /** 沿 foldedInto 上溯到最外层宿主(未折叠时返回自身) */
77
+ get rootHost(): TemporalNodeBase;
78
+ /**
79
+ * 绘制分组的语义类型,渲染器据此输出 `<g data-kind>`;空串表示不开组。
80
+ *
81
+ * 缺省取来源函数的主名,同一函数生成的对象画法不同时由子类覆盖。
82
+ */
83
+ get kind(): string;
78
84
  /** 由 LoweringContext 在列归并后固化的谱面行号 */
79
85
  layoutLine: number;
80
86
  /** 不可见状态事件保持 undefined */
@@ -96,7 +102,7 @@ export declare class TemporalNodeBase implements TimeLineEvent {
96
102
  * - tie.top:连音线端点;缺省为 anchor 与内容上沿,up 提供最上层成员的端点。
97
103
  * - div.{level}.left / div.{level}.right:从 0 编号的减时线端点,由 div.place 发布,供 beam 接管。
98
104
  * - dot:附点起点;缺省为盒右边界与 visualAxis,音符覆盖为字形附近的位置。
99
- * - lyric:歌词对齐点(voice 只读 x);未提供则不作为歌词目标,note 在 finalizeLayout 发布。
105
+ * - lyric:歌词对齐点(voice 只读 x);未提供则不作为歌词目标,note 与 dash 在 finalizeLayout 发布。
100
106
  * - bar.number:多声部连谱号顶部的行首小节号锚点。
101
107
  *
102
108
  * up、grace、arpeggio 转发宿主端口并换算局部坐标;端口不表达装饰是否存在。
@@ -113,7 +119,7 @@ export declare class TemporalNodeBase implements TimeLineEvent {
113
119
  * 2. Temporal.prepareLayout 可先加入节点自身的装饰,例如下八度点;
114
120
  * 3. 引擎再通过 addon 对应的 layoutDecorationHandler 加入函数装饰;
115
121
  * 4. 引擎分别按 above/below.order 分配空间并调用 place
116
- * 5. 最终 paintLayout 在主体 paint 后调用可选的 decoration.paint
122
+ * 5. 最终 paintVisual 在主体 paint 后调用可选的 decoration.paint
117
123
  *
118
124
  * 数组必须保留到绘制结束,因为 decoration 通常以闭包保存本次测量得到的几何。它只属于当前 layout pass,不能跨 pass 复用。
119
125
  * prepare 阶段生成、place 后冻结、paint 后失效
@@ -1,3 +1,8 @@
1
+ /**
2
+ * 时间线事件的基类
3
+ * 由 AST 产生,跨越 lowering / layout / playback 三个阶段
4
+ */
5
+ import { ASTFunctionNode } from "./ASTtypes.js";
1
6
  import { Fraction } from "../fraction.js";
2
7
  export const DEFAULT_BPM = 120;
3
8
  export const DEFAULT_VELOCITY = 80;
@@ -41,6 +46,21 @@ export class TemporalNodeBase {
41
46
  * 按全局事件工作的消费者可以沿链上溯到宿主。
42
47
  */
43
48
  foldedInto;
49
+ /** 沿 foldedInto 上溯到最外层宿主(未折叠时返回自身) */
50
+ get rootHost() {
51
+ let host = this;
52
+ while (host.foldedInto)
53
+ host = host.foldedInto;
54
+ return host;
55
+ }
56
+ /**
57
+ * 绘制分组的语义类型,渲染器据此输出 `<g data-kind>`;空串表示不开组。
58
+ *
59
+ * 缺省取来源函数的主名,同一函数生成的对象画法不同时由子类覆盖。
60
+ */
61
+ get kind() {
62
+ return this.ast instanceof ASTFunctionNode ? this.ast.callName : "";
63
+ }
44
64
  /** 由 LoweringContext 在列归并后固化的谱面行号 */
45
65
  layoutLine = 0;
46
66
  /** 不可见状态事件保持 undefined */
@@ -60,7 +80,7 @@ export class TemporalNodeBase {
60
80
  * - tie.top:连音线端点;缺省为 anchor 与内容上沿,up 提供最上层成员的端点。
61
81
  * - div.{level}.left / div.{level}.right:从 0 编号的减时线端点,由 div.place 发布,供 beam 接管。
62
82
  * - dot:附点起点;缺省为盒右边界与 visualAxis,音符覆盖为字形附近的位置。
63
- * - lyric:歌词对齐点(voice 只读 x);未提供则不作为歌词目标,note 在 finalizeLayout 发布。
83
+ * - lyric:歌词对齐点(voice 只读 x);未提供则不作为歌词目标,note 与 dash 在 finalizeLayout 发布。
64
84
  * - bar.number:多声部连谱号顶部的行首小节号锚点。
65
85
  *
66
86
  * up、grace、arpeggio 转发宿主端口并换算局部坐标;端口不表达装饰是否存在。
@@ -77,7 +97,7 @@ export class TemporalNodeBase {
77
97
  * 2. Temporal.prepareLayout 可先加入节点自身的装饰,例如下八度点;
78
98
  * 3. 引擎再通过 addon 对应的 layoutDecorationHandler 加入函数装饰;
79
99
  * 4. 引擎分别按 above/below.order 分配空间并调用 place
80
- * 5. 最终 paintLayout 在主体 paint 后调用可选的 decoration.paint
100
+ * 5. 最终 paintVisual 在主体 paint 后调用可选的 decoration.paint
81
101
  *
82
102
  * 数组必须保留到绘制结束,因为 decoration 通常以闭包保存本次测量得到的几何。它只属于当前 layout pass,不能跨 pass 复用。
83
103
  * prepare 阶段生成、place 后冻结、paint 后失效
@@ -2,6 +2,7 @@ import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { findClosingQuote, quote } from "../../parser/parse-utils/string-utils.js";
4
4
  import { DEFAULT_KEY, TemporalNodeBase } from "../temporal.js";
5
+ import { paintRichLine, parseRichText, prepareRichLine } from "../../render/text.js";
5
6
  class TextFunction extends ASTFunctionNode {
6
7
  static def = {
7
8
  name: ["text"],
@@ -12,7 +13,7 @@ class TextFunction extends ASTFunctionNode {
12
13
  ~~~
13
14
  **简写**:直接写双引号文本,如 \`"进入主题"\`;可跨行,双引号须配对。
14
15
 
15
- 支持 JSON 风格的 Unicode 转义,如 \`\\uE050\`;显示字面反斜杠须写 \`\\\\\`。`,
16
+ 支持 \`*斜体*\`、\`**粗体**\`、\`_下划线_\`、\`^上标^\`(再次出现即关闭,不关闭则一直生效;写 \`\\*\`、\`\\_\`、\`\\^\` 显示原字符)。支持 \`\\uE050\` 形式的 Unicode 转义;反斜杠写 \`\\\\\`。`,
16
17
  allowExtraArgs: false,
17
18
  args: [
18
19
  {
@@ -72,9 +73,9 @@ class TextFunction extends ASTFunctionNode {
72
73
  ctx.pushNode(new TextFunction(node.span, new Map([[0, ctx.parseArgWithType(node.span, "string")]]), ctx, null));
73
74
  return at + 1;
74
75
  }
75
- lines;
76
+ text;
76
77
  size;
77
- lineAdvance;
78
+ lineHeight;
78
79
  align;
79
80
  font;
80
81
  constructor(sourceSpan, args, ctx, parent = null) {
@@ -84,58 +85,51 @@ class TextFunction extends ASTFunctionNode {
84
85
  if (align !== "left" && align !== "center" && align !== "right") {
85
86
  throw new ErrorDiagnostic("E_TEXT_INVALID_ALIGN", "@text 的 align 参数必须是 'left'、'center' 或 'right'", sourceSpan);
86
87
  }
87
- this.lines = text.split(/\r?\n/);
88
+ this.text = text.replace(/\r?\n/g, "\n");
88
89
  this.size = ctx.length2px(size);
89
- this.lineAdvance = lineHeight * this.size;
90
+ this.lineHeight = lineHeight;
90
91
  this.align = align;
91
92
  }
92
93
  loweringEnter() { return [new TextTemporalNode(this)]; }
93
94
  toString() {
94
- const lineHeight = this.size === 0 ? 1.25 : this.lineAdvance / this.size;
95
- return `@text(${quote(this.lines.join("\n"))}, size=${this.size}px, lineheight=${lineHeight}, align=${this.align}, font=${quote(this.font)})`;
95
+ return `@text(${quote(this.text)}, size=${this.size}px, lineheight=${this.lineHeight}, align=${this.align}, font=${quote(this.font)})`;
96
96
  }
97
97
  }
98
98
  export const TextNode = TextFunction;
99
99
  class TextTemporalNode extends TemporalNodeBase {
100
- style;
101
- textBaselineY = 0;
100
+ preparedLines = [];
102
101
  constructor(ast) {
103
102
  super();
104
103
  this.ast = ast;
105
104
  this.mergeKey = DEFAULT_KEY;
106
- this.style = { fontSize: ast.size, fontFamily: ast.font, fill: "#000", textAlign: ast.align };
107
105
  this.initLayoutBox();
108
106
  }
109
107
  prepareLayout(context) {
110
- const { lines, align, lineAdvance } = this.ast;
111
- const measure = (text) => context.textMeasurer.measureText(text, this.style);
112
- const first = measure(lines[0]);
113
- let width = first.w;
114
- for (let i = 1; i < lines.length; i++)
115
- width = Math.max(width, measure(lines[i]).w);
108
+ const { text, align, size, font, lineHeight } = this.ast;
109
+ const style = { fontSize: size, fontFamily: font, fill: "#000", textAlign: align };
110
+ this.preparedLines = parseRichText(text).map(runs => prepareRichLine(runs, style, context.textMeasurer));
111
+ const first = this.preparedLines[0];
112
+ const width = Math.max(...this.preparedLines.map(l => l.w));
116
113
  this.box.w = width;
117
- this.box.h = (lines.length - 1) * lineAdvance + first.h;
114
+ this.box.h = (this.preparedLines.length - 1) * lineHeight * size + first.h;
118
115
  this.box.visualAxis = this.box.h / 2;
119
- this.textBaselineY = first.baseline;
120
116
  if (align === "center")
121
117
  this.box.anchor = width / 2;
122
- else if (align === "right") {
123
- const lastChar = [...lines[0]].at(-1);
124
- this.box.anchor = width - (lastChar ? measure(lastChar).w / 2 : 0);
125
- }
126
118
  else {
127
- // 左对齐时锚点落在首字符中心,单字标记才能像音符一样对准宿主
128
- const firstChar = [...lines[0]][0];
129
- this.box.anchor = firstChar ? measure(firstChar).w / 2 : 0;
119
+ // 左/右对齐时锚点落在首行首/末字符中心,单字标记才能像音符一样对准宿主
120
+ const edgeIdx = align === "right" ? -1 : 0;
121
+ const edgeRun = first.runs.at(edgeIdx);
122
+ const half = edgeRun ? context.textMeasurer.measureText([...edgeRun.text].at(edgeIdx), edgeRun.style).w / 2 : 0;
123
+ this.box.anchor = align === "right" ? width - half : half;
130
124
  }
131
125
  }
132
126
  paint(painter) {
133
- const { lines, align, lineAdvance } = this.ast;
127
+ const { align, lineHeight, size } = this.ast;
134
128
  const x = this.box.x + (align === "center" ? this.box.w / 2 : align === "right" ? this.box.w : 0);
135
- let y = this.box.y + this.textBaselineY;
136
- for (const line of lines) {
137
- painter.drawText(line, x, y, this.style);
138
- y += lineAdvance;
129
+ let y = this.box.y + this.preparedLines[0].baseline;
130
+ for (const line of this.preparedLines) {
131
+ paintRichLine(painter, line, x, y);
132
+ y += lineHeight * size;
139
133
  }
140
134
  }
141
135
  }
@@ -3,8 +3,9 @@ import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { isVisualTemporalNode, } from "../temporal.js";
4
4
  import { pathBounds } from "../../layout/path.js";
5
5
  import { getLayoutBounds } from "../../layout/engine.js";
6
- /** 仅索引被 @tie 引用过的端点,避免为全谱无关音符分配数组与 Map 节点 */
7
- const tieEndpoints = new WeakSet();
6
+ import { lineEntryX, lineRightX } from "../../layout/range.js";
7
+ /** 被 @tie 引用过的端点 → 其上各连音线的最大弧高;播放也只索引这些端点,避免为全谱无关音符分配数组与 Map 节点 */
8
+ const tieEndpoints = new WeakMap();
8
9
  const notesByOriginNode = new WeakMap();
9
10
  function endpointNotes(notes, endpoint) {
10
11
  let index = notesByOriginNode.get(notes);
@@ -57,19 +58,13 @@ class TieFunction extends ASTFunctionNode {
57
58
  };
58
59
  endPoints = [];
59
60
  height;
60
- fontSize;
61
61
  constructor(sourceSpan, args, ctx, parent = null) {
62
62
  super(sourceSpan, parent);
63
63
  // 命名选项统一解析默认值,位置参数仍全部作为端点
64
64
  const [height] = this.getArgValue(args, ctx);
65
65
  for (const [key, value] of args) {
66
- if (key === "height")
67
- continue;
68
- const v = value instanceof ASTFunctionNode
69
- ? value
70
- : ctx.parseArgWithType(value.valueSpan, "label", sourceSpan.start);
71
- if (v !== null)
72
- this.endPoints.push(v);
66
+ if (typeof key === "number")
67
+ this.endPoints.push(value);
73
68
  }
74
69
  // 数目不足,则找最近的;用 unshift 而不是按下标赋值,否则空洞会让 length 虚高、骗过下面的校验
75
70
  let k = ctx.labelableNodes.length - 1;
@@ -83,7 +78,6 @@ class TieFunction extends ASTFunctionNode {
83
78
  throw new ErrorDiagnostic("E_NOT_ENOUGH_ARGS", "@{fn} 连音线需要至少两个端点", sourceSpan, { fn: "tie" });
84
79
  }
85
80
  this.height = Math.max(0, ctx.length2px(height));
86
- this.fontSize = ctx.variables.fontsize;
87
81
  }
88
82
  /**
89
83
  * tie 不推进时间
@@ -101,7 +95,7 @@ class TieFunction extends ASTFunctionNode {
101
95
  ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("tie", this.sourceSpan));
102
96
  return [];
103
97
  }
104
- ctx.addAttachment(new TieLayoutAttachment(endPoints, this.height, this.fontSize, this.sourceSpan));
98
+ ctx.addAttachment(new TieLayoutAttachment(endPoints, this.height, this.sourceSpan));
105
99
  return [];
106
100
  }
107
101
  toString(source) {
@@ -113,22 +107,45 @@ class TieFunction extends ASTFunctionNode {
113
107
  }
114
108
  export const TieNode = TieFunction;
115
109
  class TieLayoutAttachment {
110
+ kind = "tie";
116
111
  layer = "foreground";
117
112
  sourceSpan;
118
113
  endPoints;
119
114
  height;
120
- fontSize;
115
+ size;
121
116
  /** 弧带中部厚度;两端收尖到 0。端点字号不随布局变化,可以一次算定 */
122
117
  thickness;
123
- constructor(endPoints, height, fontSize, sourceSpan) {
118
+ constructor(endPoints, height, sourceSpan) {
124
119
  this.endPoints = endPoints;
125
- for (const endpoint of endPoints)
126
- tieEndpoints.add(endpoint);
120
+ // 跨轨弧线必然穿过两轨之间,加高端点留空只会撑开轨距,因此只有同轨连音线按显式弧高留空
121
+ const fixed = endPoints.every(endpoint => endpoint.track === endPoints[0].track) ? height : 0;
122
+ for (const endpoint of endPoints) {
123
+ const peak = tieEndpoints.get(endpoint);
124
+ tieEndpoints.set(endpoint, Math.max(peak ?? 0, fixed || endpoint.ast.size * AUTO_HEIGHT_RATIO));
125
+ if (peak !== undefined)
126
+ continue;
127
+ const prepareLayout = endpoint.prepareLayout;
128
+ endpoint.prepareLayout = context => {
129
+ prepareLayout.call(endpoint, context);
130
+ const size = endpoint.ast.size;
131
+ const left = endpoint.ports["body.left"]?.x ?? 0;
132
+ const right = endpoint.ports["body.right"]?.x ?? endpoint.box.w;
133
+ const halfWidth = Math.max(0, endpoint.box.anchor - left, right - endpoint.box.anchor);
134
+ const u = Math.min(0.5, halfWidth / (size * AUTO_SPAN_RATIO));
135
+ let t = Math.min(0.5, u / 0.75);
136
+ t = u / (0.75 * (1 + 2 * t * (1 - t)));
137
+ t = Math.min(0.5, u / (0.75 * (1 + 2 * t * (1 - t))));
138
+ // 上方留空使用没有 paint 的纯留白 decoration,以实现 tie 的避让效果,且 box 会忽略纯留白
139
+ // 横排前跨度未知,按参考跨度估算弧线经过主体边缘时的高度,弧高取该端点上各连音线的最大值
140
+ endpoint.decorations.push({
141
+ above: { order: 200, height: size * 0.1 + 4 * t * (1 - t) * tieEndpoints.get(endpoint) },
142
+ });
143
+ };
144
+ }
127
145
  this.height = height;
128
- this.fontSize = fontSize;
129
146
  this.sourceSpan = sourceSpan;
130
- const size = endPoints.reduce((current, endpoint) => Math.max(current, endpoint.ast.size), 0);
131
- this.thickness = Math.max(1.2, size * THICKNESS_RATIO);
147
+ this.size = endPoints.reduce((current, endpoint) => Math.max(current, endpoint.ast.size), 0);
148
+ this.thickness = Math.max(1.2, this.size * THICKNESS_RATIO);
132
149
  }
133
150
  /**
134
151
  * 为同轨、同音且时间相接的逻辑音段建立连接,各次反复及各段装饰保持独立
@@ -213,39 +230,23 @@ class TieLayoutAttachment {
213
230
  segments.push(this.arcSegment(start, end, startNode.layoutLine, startTop <= endTop ? startNode.track : endNode.track));
214
231
  continue;
215
232
  }
216
- const height = this.height > 0 ? this.height : this.fontSize * CROSS_LINE_HEIGHT_RATIO;
233
+ const height = this.height > 0 ? this.height : this.size * CROSS_LINE_HEIGHT_RATIO;
217
234
  // 首行先起弧,再沿水平段延伸到当前谱面行最右侧
218
- segments.push(this.openingSegment(start, this.lineRightX(context, startNode.layoutLine), height, startNode.layoutLine, startNode.track));
235
+ segments.push(this.openingSegment(start, lineRightX(context, startNode.layoutLine), height, startNode.layoutLine, startNode.track));
219
236
  // 中间行可能没有任何可见对象,仍然需要逐行补一段水平线
220
237
  for (let line = startNode.layoutLine + 1; line < endNode.layoutLine; line++) {
221
- segments.push(this.horizontalSegment(this.lineEntryX(context, line, startNode.track), this.lineRightX(context, line), this.hostTop(context, line, startNode.track) - height, line, startNode.track));
238
+ segments.push(this.horizontalSegment(lineEntryX(context, line, startNode.track), lineRightX(context, line), this.hostTop(context, line, startNode.track) - height, line, startNode.track));
222
239
  }
223
240
  // 跳过声部名、大括号等位于端点列之前的系统前缀,再落弧到终点
224
- segments.push(this.closingSegment(this.lineEntryX(context, endNode.layoutLine, endNode.track), end, height, endNode.layoutLine, endNode.track));
241
+ segments.push(this.closingSegment(lineEntryX(context, endNode.layoutLine, endNode.track), end, height, endNode.layoutLine, endNode.track));
225
242
  }
226
243
  return segments;
227
244
  }
228
245
  resolveHeight(span) {
229
246
  if (this.height > 0)
230
247
  return this.height;
231
- const ratio = Math.min(1, span / (this.fontSize * AUTO_SPAN_RATIO));
232
- return this.fontSize * AUTO_HEIGHT_RATIO * ratio * (2 - ratio);
233
- }
234
- lineEntryX(context, lineIndex, track) {
235
- const line = context.lines[lineIndex];
236
- const host = line.trackRuns.get(track)?.find(host => !host.T.isZero());
237
- return host?.box.x ?? context.originX;
238
- }
239
- lineRightX(context, lineIndex) {
240
- const runs = context.lines[lineIndex].trackRuns;
241
- if (runs.size === 0)
242
- return context.originX + context.width;
243
- let right = context.originX;
244
- for (const run of runs.values()) {
245
- for (const host of run)
246
- right = Math.max(right, host.box.x + host.box.w);
247
- }
248
- return right;
248
+ const ratio = Math.min(1, span / (this.size * AUTO_SPAN_RATIO));
249
+ return this.size * AUTO_HEIGHT_RATIO * ratio * (2 - ratio);
249
250
  }
250
251
  hostTop(context, line, track) {
251
252
  return context.getVisualAxis(line, track) + (context.getHostExtent(line, track)?.top ?? 0);
@@ -92,7 +92,6 @@ class TremoloFunction extends ASTFunctionNode {
92
92
  content;
93
93
  n;
94
94
  get children() { return [this.content]; }
95
- timeFlowModel() { return { children: [this.content], mode: "sequence" }; }
96
95
  constructor(span, args, ctx, parent = null) {
97
96
  super(span, parent);
98
97
  const [content, n] = this.getArgValue(args, ctx);
@@ -146,6 +145,7 @@ export const TremoloNode = TremoloFunction;
146
145
  class TremoloAttachment {
147
146
  nodes;
148
147
  owner;
148
+ kind = "tremolo";
149
149
  layer = "foreground";
150
150
  endPoints;
151
151
  get sourceSpan() { return this.owner.sourceSpan; }
@@ -35,12 +35,6 @@ class TupletFunction extends ASTFunctionNode {
35
35
  normal;
36
36
  font;
37
37
  get children() { return [this.content]; }
38
- timeFlowModel() {
39
- return {
40
- mode: "sequence",
41
- children: [this.content],
42
- };
43
- }
44
38
  loweringEnter(ctx) {
45
39
  const group = {
46
40
  nodes: [],
@@ -126,6 +120,7 @@ export const TupletNode = TupletFunction;
126
120
  */
127
121
  class TupletLayoutAttachment {
128
122
  content;
123
+ kind = "tuplet";
129
124
  layer = "foreground";
130
125
  /** lowering 固化的连音语义;重复 layout 时保持不变 */
131
126
  endPoints;