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.
- package/README.md +10 -15
- package/dist/diagnostic.d.ts +29 -2
- package/dist/diagnostic.js +5 -4
- package/dist/functions/ASTtypes.d.ts +1 -14
- package/dist/functions/ASTtypes.js +5 -36
- package/dist/functions/adjust/index.js +45 -33
- package/dist/functions/arpeggio/index.js +44 -48
- package/dist/functions/arpeggio/shape.d.ts +2 -3
- package/dist/functions/arpeggio/shape.js +20 -50
- package/dist/functions/bar/index.d.ts +3 -0
- package/dist/functions/bar/index.js +21 -11
- package/dist/functions/beam/auto.js +54 -123
- package/dist/functions/beam/index.js +4 -9
- package/dist/functions/beam/layout.js +3 -2
- package/dist/functions/box/index.js +1 -6
- package/dist/functions/dash/index.js +6 -4
- package/dist/functions/div/index.d.ts +3 -5
- package/dist/functions/div/index.js +14 -12
- package/dist/functions/dot/index.js +3 -9
- package/dist/functions/dyn/index.js +7 -26
- package/dist/functions/gliss/index.js +99 -91
- package/dist/functions/grace/index.d.ts +2 -5
- package/dist/functions/grace/index.js +47 -94
- package/dist/functions/head/index.js +32 -36
- package/dist/functions/key/index.js +9 -12
- package/dist/functions/meter/index.d.ts +1 -0
- package/dist/functions/meter/index.js +6 -3
- package/dist/functions/meter/shape.d.ts +1 -1
- package/dist/functions/note/accidentals.d.ts +3 -3
- package/dist/functions/note/accidentals.js +29 -16
- package/dist/functions/note/index.js +12 -14
- package/dist/functions/note/source-transform.d.ts +15 -0
- package/dist/functions/note/source-transform.js +75 -71
- package/dist/functions/ottava/index.js +12 -32
- package/dist/functions/page/index.js +15 -17
- package/dist/functions/rest/index.js +10 -10
- package/dist/functions/slide/index.js +1 -1
- package/dist/functions/stack/index.js +8 -57
- package/dist/functions/temporal.d.ts +11 -5
- package/dist/functions/temporal.js +22 -2
- package/dist/functions/text/index.js +24 -30
- package/dist/functions/tie/index.js +42 -41
- package/dist/functions/tremolo/index.js +1 -1
- package/dist/functions/tuplet/index.js +1 -6
- package/dist/functions/up/index.js +82 -88
- package/dist/functions/voice/connections.d.ts +3 -2
- package/dist/functions/voice/connections.js +48 -10
- package/dist/functions/voice/index.d.ts +2 -3
- package/dist/functions/voice/index.js +2 -970
- package/dist/functions/voice/lyrics.d.ts +34 -0
- package/dist/functions/voice/lyrics.js +393 -0
- package/dist/functions/voice/voice.d.ts +67 -0
- package/dist/functions/voice/voice.js +405 -0
- package/dist/functions/voice/voices.d.ts +106 -0
- package/dist/functions/voice/voices.js +424 -0
- package/dist/functions/volta/index.js +6 -23
- package/dist/index.d.ts +97 -3
- package/dist/index.js +97 -3
- package/dist/jpfun.min.js +63 -67
- package/dist/layout/engine.d.ts +4 -3
- package/dist/layout/engine.js +190 -24
- package/dist/layout/model.d.ts +12 -4
- package/dist/layout/model.js +16 -11
- package/dist/layout/page.d.ts +2 -1
- package/dist/layout/page.js +15 -15
- package/dist/layout/path.js +1 -1
- package/dist/layout/range.d.ts +7 -6
- package/dist/layout/range.js +23 -14
- package/dist/layout/types.d.ts +19 -30
- package/dist/lowering/loweringContext.d.ts +8 -11
- package/dist/lowering/loweringContext.js +7 -13
- package/dist/lowering/track.d.ts +8 -1
- package/dist/lowering/types.d.ts +12 -8
- package/dist/parser/grammarType.d.ts +3 -3
- package/dist/parser/parse-utils/brace-utils.js +7 -17
- package/dist/parser/parse-utils/call-utils.d.ts +4 -3
- package/dist/parser/parse-utils/call-utils.js +23 -66
- package/dist/parser/parse-utils/note-utils.d.ts +7 -3
- package/dist/parser/parse-utils/note-utils.js +25 -40
- package/dist/parser/parse-utils/string-utils.d.ts +1 -1
- package/dist/parser/parse-utils/string-utils.js +8 -6
- package/dist/parser/parserContext.d.ts +17 -11
- package/dist/parser/parserContext.js +111 -65
- package/dist/parser/preprocess.d.ts +7 -0
- package/dist/parser/preprocess.js +21 -19
- package/dist/pipeline.d.ts +1 -1
- package/dist/pipeline.js +2 -2
- package/dist/playback/compile.js +14 -15
- package/dist/playback/event.d.ts +6 -6
- package/dist/playback/event.js +13 -49
- package/dist/render/canvas.d.ts +0 -2
- package/dist/render/canvas.js +50 -56
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/paint.d.ts +7 -0
- package/dist/render/paint.js +70 -20
- package/dist/render/recording.d.ts +8 -1
- package/dist/render/recording.js +12 -0
- package/dist/render/svg.d.ts +5 -2
- package/dist/render/svg.js +70 -48
- package/dist/render/text.d.ts +37 -2
- package/dist/render/text.js +136 -1
- package/dist/render/translate.d.ts +3 -1
- package/dist/render/translate.js +6 -0
- package/dist/render/types.d.ts +9 -2
- package/grammar.md +18 -14
- package/package.json +8 -18
- package/dist/converter/midi/convert.d.ts +0 -42
- package/dist/converter/midi/convert.js +0 -729
- package/dist/converter/midi/index.d.ts +0 -5
- package/dist/converter/midi/index.js +0 -11
- package/dist/converter/midi/json.d.ts +0 -38
- package/dist/converter/midi/json.js +0 -1
- package/dist/converter/midi/triplet.d.ts +0 -30
- package/dist/converter/midi/triplet.js +0 -75
- package/dist/converter/musicxml/dom.d.ts +0 -25
- package/dist/converter/musicxml/dom.js +0 -65
- package/dist/converter/musicxml/features.d.ts +0 -114
- package/dist/converter/musicxml/features.js +0 -501
- package/dist/converter/musicxml/index.d.ts +0 -14
- package/dist/converter/musicxml/index.js +0 -1295
- package/dist/converter/musicxml/model.d.ts +0 -176
- package/dist/converter/musicxml/model.js +0 -1
- package/dist/converter/musicxml/resolve.d.ts +0 -10
- package/dist/converter/musicxml/resolve.js +0 -152
- package/dist/converter/source.d.ts +0 -22
- package/dist/converter/source.js +0 -51
- package/dist/core.d.ts +0 -24
- package/dist/core.js +0 -24
- package/dist/iife/core.d.ts +0 -1
- package/dist/iife/core.js +0 -3
- package/dist/iife/from-midi.d.ts +0 -1
- package/dist/iife/from-midi.js +0 -15
- package/dist/iife/from-musicxml.d.ts +0 -1
- package/dist/iife/from-musicxml.js +0 -3
- package/dist/iife/full.d.ts +0 -1
- package/dist/iife/full.js +0 -3
- package/dist/jpfun.core.min.js +0 -123
- package/dist/jpfun.from-midi.min.js +0 -9
- package/dist/jpfun.from-musicxml.min.js +0 -12
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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 (
|
|
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
|
|
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. 最终
|
|
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. 最终
|
|
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
|
-
支持
|
|
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
|
-
|
|
76
|
+
text;
|
|
76
77
|
size;
|
|
77
|
-
|
|
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.
|
|
88
|
+
this.text = text.replace(/\r?\n/g, "\n");
|
|
88
89
|
this.size = ctx.length2px(size);
|
|
89
|
-
this.
|
|
90
|
+
this.lineHeight = lineHeight;
|
|
90
91
|
this.align = align;
|
|
91
92
|
}
|
|
92
93
|
loweringEnter() { return [new TextTemporalNode(this)]; }
|
|
93
94
|
toString() {
|
|
94
|
-
|
|
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
|
-
|
|
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 {
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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 = (
|
|
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
|
|
129
|
-
|
|
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 {
|
|
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.
|
|
136
|
-
for (const line of
|
|
137
|
-
painter
|
|
138
|
-
y +=
|
|
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
|
-
|
|
7
|
-
|
|
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 === "
|
|
67
|
-
|
|
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.
|
|
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
|
-
|
|
115
|
+
size;
|
|
121
116
|
/** 弧带中部厚度;两端收尖到 0。端点字号不随布局变化,可以一次算定 */
|
|
122
117
|
thickness;
|
|
123
|
-
constructor(endPoints, height,
|
|
118
|
+
constructor(endPoints, height, sourceSpan) {
|
|
124
119
|
this.endPoints = endPoints;
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
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.
|
|
233
|
+
const height = this.height > 0 ? this.height : this.size * CROSS_LINE_HEIGHT_RATIO;
|
|
217
234
|
// 首行先起弧,再沿水平段延伸到当前谱面行最右侧
|
|
218
|
-
segments.push(this.openingSegment(start,
|
|
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(
|
|
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(
|
|
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.
|
|
232
|
-
return this.
|
|
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;
|