jpfun 0.2.0 → 0.4.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 +141 -11
- package/dist/converter/midi/convert.d.ts +50 -0
- package/dist/converter/midi/convert.js +700 -0
- package/dist/converter/midi/index.d.ts +5 -0
- package/dist/converter/midi/index.js +11 -0
- package/dist/converter/midi/json.d.ts +38 -0
- package/dist/converter/midi/json.js +1 -0
- package/dist/converter/midi/triplet.d.ts +30 -0
- package/dist/converter/midi/triplet.js +75 -0
- package/dist/converter/musicxml/dom.d.ts +25 -0
- package/dist/converter/musicxml/dom.js +65 -0
- package/dist/converter/musicxml/features.d.ts +51 -0
- package/dist/converter/musicxml/features.js +232 -0
- package/dist/converter/musicxml/index.d.ts +9 -0
- package/dist/converter/musicxml/index.js +1140 -0
- package/dist/converter/musicxml/model.d.ts +123 -0
- package/dist/converter/musicxml/model.js +1 -0
- package/dist/converter/source.d.ts +22 -0
- package/dist/converter/source.js +51 -0
- package/dist/core.d.ts +22 -0
- package/dist/core.js +22 -0
- package/dist/diagnostic.js +5 -2
- package/dist/fraction.d.ts +24 -0
- package/dist/fraction.js +132 -0
- package/dist/functions/ASTtypes.d.ts +51 -31
- package/dist/functions/ASTtypes.js +59 -38
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/arpeggio/index.d.ts +2 -0
- package/dist/functions/arpeggio/index.js +180 -0
- package/dist/functions/arpeggio/shape.d.ts +7 -0
- package/dist/functions/arpeggio/shape.js +122 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +62 -25
- package/dist/functions/beam/auto.d.ts +1 -1
- package/dist/functions/beam/auto.js +33 -16
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +4 -3
- package/dist/functions/beam/layout.js +43 -65
- package/dist/functions/box/index.js +37 -36
- package/dist/functions/br/index.js +10 -8
- package/dist/functions/dash/index.js +40 -7
- package/dist/functions/default.js +19 -8
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +45 -17
- package/dist/functions/dot/index.js +23 -25
- package/dist/functions/dyn/index.d.ts +2 -0
- package/dist/functions/dyn/index.js +242 -0
- package/dist/functions/grace/index.d.ts +8 -11
- package/dist/functions/grace/index.js +84 -79
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +496 -0
- package/dist/functions/key/index.js +98 -3
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +155 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +106 -79
- package/dist/functions/note/noteNameFSM.js +52 -63
- package/dist/functions/page/index.d.ts +11 -2
- package/dist/functions/page/index.js +65 -4
- package/dist/functions/program/index.d.ts +2 -0
- package/dist/functions/program/index.js +30 -0
- package/dist/functions/set/index.js +43 -32
- package/dist/functions/stack/index.js +26 -22
- package/dist/functions/symbol/index.d.ts +31 -0
- package/dist/functions/symbol/index.js +139 -0
- package/dist/functions/symbol/ornament.d.ts +15 -0
- package/dist/functions/symbol/ornament.js +73 -0
- package/dist/functions/symbol/shape.d.ts +24 -0
- package/dist/functions/symbol/shape.js +52 -0
- package/dist/functions/symbol/symbols/accent.d.ts +2 -0
- package/dist/functions/symbol/symbols/accent.js +24 -0
- package/dist/functions/symbol/symbols/dc.d.ts +4 -0
- package/dist/functions/symbol/symbols/dc.js +82 -0
- package/dist/functions/symbol/symbols/ds.d.ts +2 -0
- package/dist/functions/symbol/symbols/ds.js +93 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
- package/dist/functions/symbol/symbols/dynamics.js +168 -0
- package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
- package/dist/functions/symbol/symbols/fermata.js +24 -0
- package/dist/functions/symbol/symbols/fine.d.ts +2 -0
- package/dist/functions/symbol/symbols/fine.js +12 -0
- package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +56 -0
- package/dist/functions/symbol/symbols/prall.d.ts +2 -0
- package/dist/functions/symbol/symbols/prall.js +42 -0
- package/dist/functions/symbol/symbols/segno.d.ts +4 -0
- package/dist/functions/symbol/symbols/segno.js +82 -0
- package/dist/functions/symbol/symbols/tr.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +71 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +57 -4
- package/dist/functions/temporal.d.ts +138 -0
- package/dist/functions/temporal.js +101 -0
- package/dist/functions/text/index.js +86 -27
- package/dist/functions/tie/index.js +136 -152
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +218 -0
- package/dist/functions/up/index.d.ts +1 -0
- package/dist/functions/up/index.js +258 -182
- package/dist/functions/voice/index.js +203 -109
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +230 -0
- package/dist/iife/core.d.ts +1 -0
- package/dist/iife/core.js +3 -0
- package/dist/iife/from-midi.d.ts +1 -0
- package/dist/iife/from-midi.js +15 -0
- package/dist/iife/from-musicxml.d.ts +1 -0
- package/dist/iife/from-musicxml.js +3 -0
- package/dist/iife/full.d.ts +1 -0
- package/dist/iife/full.js +3 -0
- package/dist/index.d.ts +3 -17
- package/dist/index.js +3 -17
- package/dist/jpfun.core.min.js +153 -0
- package/dist/jpfun.from-midi.min.js +9 -0
- package/dist/jpfun.from-musicxml.min.js +12 -0
- package/dist/jpfun.min.js +166 -0
- package/dist/layout/default.js +2 -3
- package/dist/layout/engine.d.ts +6 -12
- package/dist/layout/engine.js +115 -64
- package/dist/layout/model.d.ts +12 -5
- package/dist/layout/model.js +31 -18
- package/dist/layout/page.js +5 -4
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +71 -20
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +20 -20
- package/dist/lowering/loweringContext.js +153 -101
- package/dist/lowering/track.d.ts +5 -9
- package/dist/lowering/track.js +7 -2
- package/dist/lowering/types.d.ts +26 -112
- package/dist/lowering/types.js +1 -92
- package/dist/parser/parse-utils/call-utils.d.ts +0 -2
- package/dist/parser/parse-utils/call-utils.js +0 -6
- package/dist/parser/parse-utils/note-utils.d.ts +8 -0
- package/dist/parser/parse-utils/note-utils.js +35 -13
- package/dist/parser/parse-utils/string-utils.d.ts +10 -0
- package/dist/parser/parse-utils/string-utils.js +24 -0
- package/dist/parser/parserContext.d.ts +11 -9
- package/dist/parser/parserContext.js +65 -55
- package/dist/parser/preprocess.js +17 -11
- package/dist/pipeline.d.ts +6 -5
- package/dist/pipeline.js +4 -5
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +414 -0
- package/dist/playback/event.d.ts +105 -0
- package/dist/playback/event.js +105 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +129 -0
- package/dist/playback/types.js +3 -0
- package/dist/render/canvas.d.ts +4 -4
- package/dist/render/canvas.js +20 -5
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/dist/render/paint.d.ts +8 -0
- package/dist/render/paint.js +101 -0
- package/dist/render/svg.d.ts +3 -3
- package/dist/render/svg.js +10 -8
- package/dist/render/text.d.ts +22 -0
- package/dist/render/text.js +42 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +132 -13
- package/package.json +19 -6
|
@@ -4,35 +4,15 @@ export function functionAddonKey(name) {
|
|
|
4
4
|
return `@${name}`;
|
|
5
5
|
}
|
|
6
6
|
export class ASTNodeBase {
|
|
7
|
-
sourceSpan;
|
|
7
|
+
sourceSpan;
|
|
8
8
|
parent;
|
|
9
9
|
// 空数组表示应该有 null表示自己就是叶子 和 timeFlowMode 的默认实现对应
|
|
10
10
|
get children() { return null; }
|
|
11
|
-
constructor(sourceSpan,
|
|
11
|
+
constructor(sourceSpan, // 源码区间
|
|
12
|
+
parent = null) {
|
|
12
13
|
this.sourceSpan = sourceSpan;
|
|
13
14
|
this.parent = parent;
|
|
14
15
|
}
|
|
15
|
-
/**
|
|
16
|
-
* lowering 固化后的 attachment 生成钩子
|
|
17
|
-
*
|
|
18
|
-
* 普通 loweringEnter/loweringExit 在递归遍历 AST 时执行,
|
|
19
|
-
* 此时尚未完成锚点归并,事件的 t、track 和 layoutLine 不可用,因此需要观察完整事件流的功能不能在普通 hook 中可靠决定分组。
|
|
20
|
-
* 该 hook 读取 LoweringResult 并返回新建的 LayoutAttachment,LoweringContext 将返回值追加到当前文档或 fragment 的 attachments,不会把它们放入时间列或推进时间。
|
|
21
|
-
*
|
|
22
|
-
* 例:autobeam 扫描最终列,按轨道、谱面行、拍点和显式 beam 端点决定分组,再生成 BeamLayoutAttachment
|
|
23
|
-
*/
|
|
24
|
-
static loweringAugment;
|
|
25
|
-
/**
|
|
26
|
-
* 所有 loweringAugment 结果追加完成后的最终处理;此时已经得到了完整的 lowering 语义
|
|
27
|
-
* 可以进行校验(如 beam 和 tie)或者修改某些值
|
|
28
|
-
*/
|
|
29
|
-
static loweringFinalize;
|
|
30
|
-
/**
|
|
31
|
-
* 装饰函数可选的排版声明
|
|
32
|
-
* layout 引擎按 key 收集,不需要识别具体函数类
|
|
33
|
-
* addon 中的 `@key` 标志会使用该装饰
|
|
34
|
-
*/
|
|
35
|
-
static layoutDecorationHandler;
|
|
36
16
|
/**
|
|
37
17
|
* 进入当前层级的回调
|
|
38
18
|
* @param ctx 当前的 lowering 上下文
|
|
@@ -44,14 +24,18 @@ export class ASTNodeBase {
|
|
|
44
24
|
}
|
|
45
25
|
/**
|
|
46
26
|
* 时间求解模式
|
|
47
|
-
*
|
|
27
|
+
* loweringEnter 返回的事件已规范化并加入索引后调用,决定子节点的展开方式。
|
|
28
|
+
* 需要本轮事件时通过 ctx.getTemporalNodes 查询
|
|
48
29
|
* @returns 返回 null 表示没有子元素,返回对象表示参与时间求解
|
|
49
30
|
*/
|
|
50
|
-
timeFlowModel() { return null; }
|
|
31
|
+
timeFlowModel(_ctx) { return null; }
|
|
51
32
|
/**
|
|
52
33
|
* 离开当前层级的回调 同 loweringEnter
|
|
34
|
+
*
|
|
35
|
+
* timeOffset 是 children 展开后的当前时间游标。
|
|
36
|
+
* 只有在同步重写本节点已经产生的 Temporal.t/T 时才可原地修改它,避免事件时间与后继事件起点不一致。
|
|
53
37
|
*/
|
|
54
|
-
loweringExit(_ctx, _track) {
|
|
38
|
+
loweringExit(_ctx, _track, _timeOffset) {
|
|
55
39
|
return [];
|
|
56
40
|
}
|
|
57
41
|
// 去糖后文本输出
|
|
@@ -66,10 +50,12 @@ export class ASTTextNode extends ASTNodeBase {
|
|
|
66
50
|
// 主要用途是高亮等编辑器功能
|
|
67
51
|
export class ASTLabelNode extends ASTNodeBase {
|
|
68
52
|
label;
|
|
69
|
-
|
|
70
|
-
|
|
53
|
+
/** 被标注的节点。parent 是树结构、会被容器改写,不能用来表达这层关系 */
|
|
54
|
+
target;
|
|
55
|
+
constructor(sourceSpan, label, target, parent = null) {
|
|
71
56
|
super(sourceSpan, parent);
|
|
72
57
|
this.label = label;
|
|
58
|
+
this.target = target;
|
|
73
59
|
}
|
|
74
60
|
toString(_) {
|
|
75
61
|
return `@${this.label} `;
|
|
@@ -117,13 +103,13 @@ export function resolveArgType(def, name, index) {
|
|
|
117
103
|
return def.args.find(arg => arg.name?.toLowerCase() === name)?.type;
|
|
118
104
|
return def.args[index]?.type ?? (def.allowExtraArgs ? def.extraArgType : undefined);
|
|
119
105
|
}
|
|
106
|
+
/** 别名里的第一个是主名;`@set` 的 key 和 `getArgValue` 的前缀必须用同一个 */
|
|
107
|
+
export function primaryName(def) {
|
|
108
|
+
return Array.isArray(def.name) ? def.name[0] : def.name;
|
|
109
|
+
}
|
|
120
110
|
// 所有函数节点的基类,提供通用的参数提取方法和标签功能
|
|
121
111
|
// 非正常函数则实例化该函数 特征是getDef为undefined
|
|
122
112
|
export class ASTFunctionNode extends ASTNodeBase {
|
|
123
|
-
/**
|
|
124
|
-
* 返回真正承载标签的 AST 节点,null 表示不可被标注
|
|
125
|
-
*/
|
|
126
|
-
labelable() { return null; }
|
|
127
113
|
label; // 可选的标签名,只对 labelable() 选中的节点有效; 或者是label节点
|
|
128
114
|
// 获取函数定义 对于未知函数,不定义def
|
|
129
115
|
static def;
|
|
@@ -133,10 +119,7 @@ export class ASTFunctionNode extends ASTNodeBase {
|
|
|
133
119
|
// 实例访问: new ().def 或 new ().constructor.def
|
|
134
120
|
// 静态访问: 类名.def 或 类名.prototype.def
|
|
135
121
|
get callName() {
|
|
136
|
-
|
|
137
|
-
if (!names)
|
|
138
|
-
return "";
|
|
139
|
-
return Array.isArray(names) ? names[0] : names;
|
|
122
|
+
return this.def ? primaryName(this.def) : "";
|
|
140
123
|
}
|
|
141
124
|
// 去糖函数 详见 ParserContext 中 deSugarAtomFns 和 deSugarRelationFns 的定义
|
|
142
125
|
static deSugarAtom = () => null; // 默认没有去糖,子类只需要定义static deSugarAtom方法即可
|
|
@@ -149,8 +132,7 @@ export class ASTFunctionNode extends ASTNodeBase {
|
|
|
149
132
|
if (!def)
|
|
150
133
|
return [];
|
|
151
134
|
const defArgs = def.args;
|
|
152
|
-
|
|
153
|
-
const prefix = Array.isArray(def.name) ? def.name[0] : def.name;
|
|
135
|
+
const prefix = primaryName(def);
|
|
154
136
|
return defArgs.map((argDef, index) => {
|
|
155
137
|
let argNameL = argDef.name ? argDef.name.toLowerCase() : null; // 统一小写处理
|
|
156
138
|
// 先查询是否传递
|
|
@@ -169,6 +151,45 @@ export class ASTFunctionNode extends ASTNodeBase {
|
|
|
169
151
|
return super.toString(source);
|
|
170
152
|
return `@${name} `;
|
|
171
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* lowering 固化后的 attachment 生成钩子
|
|
156
|
+
*
|
|
157
|
+
* 普通 loweringEnter/loweringExit 在递归遍历 AST 时执行,
|
|
158
|
+
* 此时尚未完成锚点归并,事件的 t、track 和 layoutLine 不可用,因此需要观察完整事件流的功能不能在普通 hook 中可靠决定分组。
|
|
159
|
+
* 该 hook 读取 LoweringResult 并返回新建的 LoweringAttachment,LoweringContext 将返回值追加到当前文档或 fragment 的 attachments,不会把它们放入时间列或推进时间。
|
|
160
|
+
*
|
|
161
|
+
* 例:autobeam 扫描最终列,按轨道、谱面行、拍点和显式 beam 端点决定分组,再生成 BeamLayoutAttachment
|
|
162
|
+
*/
|
|
163
|
+
static loweringAugment;
|
|
164
|
+
/**
|
|
165
|
+
* 所有 loweringAugment 结果追加完成后的最终处理;此时已经得到了完整的 lowering 语义
|
|
166
|
+
* 所有派生 attachment 追加完成后,按函数注册顺序处理完整结果
|
|
167
|
+
* 可以进行校验(如 beam 和 tie)或者修改某些值
|
|
168
|
+
*/
|
|
169
|
+
static loweringFinalize;
|
|
170
|
+
/**
|
|
171
|
+
* 装饰函数可选的排版声明
|
|
172
|
+
* layout 引擎按 key 收集,不需要识别具体函数类
|
|
173
|
+
* addon 中的 `@key` 标志会使用该装饰
|
|
174
|
+
*/
|
|
175
|
+
static layoutDecorationHandler;
|
|
176
|
+
/**
|
|
177
|
+
* 在子树里找标签承载者
|
|
178
|
+
* 未实现 labelable = 透明,继续下潜;实现了就采信它的返回值,null 即拒绝,不再往里找。
|
|
179
|
+
* 拒绝时别退回去遍历 children —— grace 的 labelable() 内部已经走过整棵子树
|
|
180
|
+
* 只有函数节点声明 labelable,别的节点读到的必是 undefined,所以无需 instanceof
|
|
181
|
+
*/
|
|
182
|
+
static findLabelable(node) {
|
|
183
|
+
const self = node;
|
|
184
|
+
if (self.labelable)
|
|
185
|
+
return self.labelable();
|
|
186
|
+
for (const child of node.children ?? []) {
|
|
187
|
+
const target = ASTFunctionNode.findLabelable(child);
|
|
188
|
+
if (target)
|
|
189
|
+
return target;
|
|
190
|
+
}
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
172
193
|
/**
|
|
173
194
|
* 用于语法糖向前结合
|
|
174
195
|
* 一般向前结合都是寻找brace或者function
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { WarningDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { isVisualTemporalNode } from "../temporal.js";
|
|
4
|
+
import { isLayoutAttachment, } from "../../layout/types.js";
|
|
5
|
+
class AdjustFunction extends ASTFunctionNode {
|
|
6
|
+
static def = {
|
|
7
|
+
name: ["adjust", "adj"],
|
|
8
|
+
description: "微调目标的位置与占位",
|
|
9
|
+
example: `@adjust(content, dx=0px, dy=0px, dw=0px, dh=0px)
|
|
10
|
+
dw/dh 在排版前增减占位,最终尺寸不小于 0;dw 按原左右占位比例分配,dh 调整下方;
|
|
11
|
+
dx/dy 在排版完成后平移目标,邻居不会让开,因此可以故意重叠。
|
|
12
|
+
括住的若是关系对象(连音线等)且其中没有对象,平移的就是这条关系对象`,
|
|
13
|
+
allowExtraArgs: false,
|
|
14
|
+
args: [
|
|
15
|
+
{
|
|
16
|
+
type: "content",
|
|
17
|
+
default: null,
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
name: "dx",
|
|
21
|
+
type: "length",
|
|
22
|
+
default: { value: 0, unit: "px" },
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
name: "dy",
|
|
26
|
+
type: "length",
|
|
27
|
+
default: { value: 0, unit: "px" },
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
name: "dw",
|
|
31
|
+
type: "length",
|
|
32
|
+
default: { value: 0, unit: "px" },
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: "dh",
|
|
36
|
+
type: "length",
|
|
37
|
+
default: { value: 0, unit: "px" },
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
target;
|
|
42
|
+
dx;
|
|
43
|
+
dy;
|
|
44
|
+
dw;
|
|
45
|
+
dh;
|
|
46
|
+
get children() { return [this.target]; }
|
|
47
|
+
timeFlowModel() {
|
|
48
|
+
return {
|
|
49
|
+
children: [this.target],
|
|
50
|
+
mode: "sequence",
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** 微调不创建时间事件,只收集作用域内的对象与关系对象 */
|
|
54
|
+
loweringEnter(ctx) {
|
|
55
|
+
const targets = { temporal: 0, attachment: 0 };
|
|
56
|
+
const group = {
|
|
57
|
+
targets,
|
|
58
|
+
onTemporal: node => {
|
|
59
|
+
if (!isVisualTemporalNode(node))
|
|
60
|
+
return;
|
|
61
|
+
targets.temporal++;
|
|
62
|
+
offsetTemporal(node, this);
|
|
63
|
+
},
|
|
64
|
+
onAttachment: attachment => {
|
|
65
|
+
if (!isLayoutAttachment(attachment))
|
|
66
|
+
return;
|
|
67
|
+
targets.attachment++;
|
|
68
|
+
offsetAttachment(attachment, targets, this.dx, this.dy);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
ctx.beginLoweringGroup(this, group);
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
loweringExit(ctx) {
|
|
75
|
+
const group = ctx.endLoweringGroup(this);
|
|
76
|
+
if (group.targets.temporal === 0 && group.targets.attachment === 0) {
|
|
77
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_ADJUST_NO_TARGET", "@adjust 没有找到可调整的对象", this.sourceSpan));
|
|
78
|
+
}
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
constructor(sourceSpan, args, ctx, parent = null) {
|
|
82
|
+
super(sourceSpan, parent);
|
|
83
|
+
const [tgt, dx, dy, dw, dh] = this.getArgValue(args, ctx);
|
|
84
|
+
tgt.parent = this;
|
|
85
|
+
this.target = tgt;
|
|
86
|
+
this.dx = ctx.length2px(dx);
|
|
87
|
+
this.dy = ctx.length2px(dy);
|
|
88
|
+
this.dw = ctx.length2px(dw);
|
|
89
|
+
this.dh = ctx.length2px(dh);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export const AdjustNode = AdjustFunction;
|
|
93
|
+
/** 在原对象上组合布局生命周期,保持布局索引依赖的对象身份 */
|
|
94
|
+
function offsetTemporal(node, owner) {
|
|
95
|
+
const { dx, dy, dw, dh } = owner;
|
|
96
|
+
if (dx === 0 && dy === 0 && dw === 0 && dh === 0)
|
|
97
|
+
return;
|
|
98
|
+
let placedX;
|
|
99
|
+
let placedY;
|
|
100
|
+
const finalize = node.finalizeLayout;
|
|
101
|
+
const onPlaced = node.onPlaced;
|
|
102
|
+
node.finalizeLayout = (context) => {
|
|
103
|
+
finalize?.call(node, context);
|
|
104
|
+
const width = Math.max(0, node.box.w + dw);
|
|
105
|
+
node.box.anchor = node.box.w > 0 ? node.box.anchor * width / node.box.w : 0;
|
|
106
|
+
node.box.w = width;
|
|
107
|
+
node.box.h = Math.max(0, node.box.h + dh);
|
|
108
|
+
};
|
|
109
|
+
node.onPlaced = () => {
|
|
110
|
+
if (node.box.x !== placedX)
|
|
111
|
+
node.box.x += dx;
|
|
112
|
+
if (node.box.y !== placedY)
|
|
113
|
+
node.box.y += dy;
|
|
114
|
+
onPlaced?.call(node);
|
|
115
|
+
placedX = node.box.x;
|
|
116
|
+
placedY = node.box.y;
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 平移一条关系对象
|
|
121
|
+
*
|
|
122
|
+
* 在原 Attachment 上组合 createGeometry,保留依赖查找与 instanceof 所需的对象身份
|
|
123
|
+
* 作用域里只要有对象被平移过,关系对象就已经跟着端点走了,这里不能再平移第二次
|
|
124
|
+
*/
|
|
125
|
+
function offsetAttachment(attachment, targets, dx, dy) {
|
|
126
|
+
if (dx === 0 && dy === 0)
|
|
127
|
+
return;
|
|
128
|
+
const createGeometry = attachment.createGeometry;
|
|
129
|
+
attachment.createGeometry = (context) => {
|
|
130
|
+
const geometry = createGeometry.call(attachment, context);
|
|
131
|
+
if (targets.temporal > 0)
|
|
132
|
+
return geometry;
|
|
133
|
+
const shift = (region) => region.line === void 0
|
|
134
|
+
? { x: region.x + dx, y: region.y + dy, w: region.w, h: region.h }
|
|
135
|
+
: { x: region.x + dx, y: region.y + dy, w: region.w, h: region.h, line: region.line, track: region.track };
|
|
136
|
+
return {
|
|
137
|
+
regions: geometry.regions.map(shift),
|
|
138
|
+
occupancy: geometry.occupancy?.map(shift),
|
|
139
|
+
paint(painter) {
|
|
140
|
+
// attachment 的绘制大多用闭包存储了,所以只能代理 painter
|
|
141
|
+
geometry.paint(new TranslatingPainter(painter, dx, dy));
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/** Painter 没有变换栈,被微调的关系对象只能逐条命令平移 */
|
|
147
|
+
class TranslatingPainter {
|
|
148
|
+
target;
|
|
149
|
+
dx;
|
|
150
|
+
dy;
|
|
151
|
+
constructor(target, dx, dy) {
|
|
152
|
+
this.target = target;
|
|
153
|
+
this.dx = dx;
|
|
154
|
+
this.dy = dy;
|
|
155
|
+
}
|
|
156
|
+
drawText(text, x, y, style) {
|
|
157
|
+
this.target.drawText(text, x + this.dx, y + this.dy, style);
|
|
158
|
+
}
|
|
159
|
+
drawLine(x1, y1, x2, y2, style) {
|
|
160
|
+
this.target.drawLine(x1 + this.dx, y1 + this.dy, x2 + this.dx, y2 + this.dy, style);
|
|
161
|
+
}
|
|
162
|
+
drawRect(x, y, w, h, style) {
|
|
163
|
+
this.target.drawRect(x + this.dx, y + this.dy, w, h, style);
|
|
164
|
+
}
|
|
165
|
+
drawCircle(cx, cy, r, style) {
|
|
166
|
+
this.target.drawCircle(cx + this.dx, cy + this.dy, r, style);
|
|
167
|
+
}
|
|
168
|
+
drawPath(commands, style, transform) {
|
|
169
|
+
if (transform) {
|
|
170
|
+
const moved = { ...transform, x: transform.x + this.dx, y: transform.y + this.dy };
|
|
171
|
+
this.target.drawPath(commands, style, moved);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
this.target.drawPath(commands.map(command => this.translateCommand(command)), style);
|
|
175
|
+
}
|
|
176
|
+
translateCommand(command) {
|
|
177
|
+
switch (command.op) {
|
|
178
|
+
case "Z": return command;
|
|
179
|
+
case "Q": return {
|
|
180
|
+
op: "Q",
|
|
181
|
+
cx: command.cx + this.dx, cy: command.cy + this.dy,
|
|
182
|
+
x: command.x + this.dx, y: command.y + this.dy,
|
|
183
|
+
};
|
|
184
|
+
case "C": return {
|
|
185
|
+
op: "C",
|
|
186
|
+
cx1: command.cx1 + this.dx, cy1: command.cy1 + this.dy,
|
|
187
|
+
cx2: command.cx2 + this.dx, cy2: command.cy2 + this.dy,
|
|
188
|
+
x: command.x + this.dx, y: command.y + this.dy,
|
|
189
|
+
};
|
|
190
|
+
default: return { op: command.op, x: command.x + this.dx, y: command.y + this.dy };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { Fraction } from "../../fraction.js";
|
|
3
|
+
import { prepareLayoutHost } from "../../layout/engine.js";
|
|
4
|
+
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
5
|
+
import { TemporalNodeBase } from "../temporal.js";
|
|
6
|
+
import { prepareArpeggioShape } from "./shape.js";
|
|
7
|
+
/** Fold 保持模块私有;琶音只在自己的边界读取它已经公开在实例上的结构。 */
|
|
8
|
+
function readFold(node) {
|
|
9
|
+
const candidate = node;
|
|
10
|
+
const ast = node.ast;
|
|
11
|
+
return (ast.callName === "up" || ast.callName === "down")
|
|
12
|
+
&& Array.isArray(candidate.members)
|
|
13
|
+
&& Array.isArray(ast.contents)
|
|
14
|
+
? candidate
|
|
15
|
+
: null;
|
|
16
|
+
}
|
|
17
|
+
class ArpeggioFunction extends ASTFunctionNode {
|
|
18
|
+
static def = {
|
|
19
|
+
name: ["arp", "arpeggio"],
|
|
20
|
+
description: "琶音",
|
|
21
|
+
example: `@arp({1 ^ 3 ^ 5}) 默认从低到高,无箭头
|
|
22
|
+
@arp({1 ^ 3 ^ 5}, direction=up) 从低到高,顶端带箭头
|
|
23
|
+
@arp({1 ^ 3 ^ 5}, direction=down) 从高到低,底端带箭头`,
|
|
24
|
+
allowExtraArgs: false,
|
|
25
|
+
args: [
|
|
26
|
+
{
|
|
27
|
+
name: "content",
|
|
28
|
+
type: "content",
|
|
29
|
+
default: null
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: "direction",
|
|
33
|
+
type: "string",
|
|
34
|
+
default: ""
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
};
|
|
38
|
+
content;
|
|
39
|
+
direction;
|
|
40
|
+
size;
|
|
41
|
+
get children() { return [this.content]; }
|
|
42
|
+
constructor(span, args, ctx, parent = null) {
|
|
43
|
+
super(span, parent);
|
|
44
|
+
const [content, direction] = this.getArgValue(args, ctx);
|
|
45
|
+
let normalized = "";
|
|
46
|
+
if (direction !== "" && direction !== "up" && direction !== "down") {
|
|
47
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_ARPEGGIO_INVALID_DIRECTION", "@arp 的 direction 只能是 up 或 down,已回退到无箭头上行琶音", span));
|
|
48
|
+
}
|
|
49
|
+
else
|
|
50
|
+
normalized = direction;
|
|
51
|
+
this.content = content;
|
|
52
|
+
this.direction = normalized;
|
|
53
|
+
this.size = ctx.variables.fontsize;
|
|
54
|
+
content.parent = this;
|
|
55
|
+
}
|
|
56
|
+
loweringEnter(ctx, track) {
|
|
57
|
+
let host = null;
|
|
58
|
+
ctx.isolateFromLoweringGroups(() => {
|
|
59
|
+
const events = ctx.trackedEvents(this.content, new Fraction(), track).flat();
|
|
60
|
+
if (events.length !== 1 || !events[0].box) {
|
|
61
|
+
throw new ErrorDiagnostic("E_ARPEGGIO_INVALID_CONTENT", "@arp 的内容必须恰好产生一个同轨和弦", this.content.sourceSpan);
|
|
62
|
+
}
|
|
63
|
+
host = events[0];
|
|
64
|
+
});
|
|
65
|
+
const fold = readFold(host);
|
|
66
|
+
if (!fold || fold.members.length < 2 || fold.members.some(member => member.track !== track)) {
|
|
67
|
+
throw new ErrorDiagnostic("E_ARPEGGIO_INVALID_CONTENT", "@arp 只接受至少包含两个成员的同轨 @up/@down 折叠体", this.content.sourceSpan);
|
|
68
|
+
}
|
|
69
|
+
return [new ArpeggioTemporal(this, fold)];
|
|
70
|
+
}
|
|
71
|
+
toString(source) {
|
|
72
|
+
const direction = this.direction ? `, direction=${this.direction}` : "";
|
|
73
|
+
return `@arp(${this.content.toString(source)}${direction})`;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export const ArpeggioNode = ArpeggioFunction;
|
|
77
|
+
class ArpeggioTemporal extends TemporalNodeBase {
|
|
78
|
+
host;
|
|
79
|
+
playbackMembers;
|
|
80
|
+
hostX = 0;
|
|
81
|
+
markX = 0;
|
|
82
|
+
markCommands = [];
|
|
83
|
+
constructor(ast, host) {
|
|
84
|
+
super();
|
|
85
|
+
this.ast = ast;
|
|
86
|
+
this.host = host;
|
|
87
|
+
const aboveCount = host.ast.contents.length;
|
|
88
|
+
const bottomToTop = [
|
|
89
|
+
...host.members.slice(aboveCount).reverse(),
|
|
90
|
+
host.members[0],
|
|
91
|
+
...host.members.slice(1, aboveCount),
|
|
92
|
+
];
|
|
93
|
+
this.playbackMembers = ast.direction === "down" ? bottomToTop.reverse() : bottomToTop;
|
|
94
|
+
this.T.copyFrom(host.T);
|
|
95
|
+
this.mergeKey = host.mergeKey;
|
|
96
|
+
this.initLayoutBox();
|
|
97
|
+
if (host.addon)
|
|
98
|
+
this.addon = { ...host.addon };
|
|
99
|
+
host.addon = void 0;
|
|
100
|
+
host.foldedInto = this;
|
|
101
|
+
}
|
|
102
|
+
onTimeState(state) {
|
|
103
|
+
this.host.t.copyFrom(this.t);
|
|
104
|
+
this.host.T.copyFrom(this.T);
|
|
105
|
+
this.host.track = this.track;
|
|
106
|
+
this.host.layoutLine = this.layoutLine;
|
|
107
|
+
this.host.onTimeState?.(state);
|
|
108
|
+
}
|
|
109
|
+
prepareLayout(context) {
|
|
110
|
+
if (this.addon) {
|
|
111
|
+
this.host.addon = this.addon;
|
|
112
|
+
this.addon = void 0;
|
|
113
|
+
}
|
|
114
|
+
prepareLayoutHost(this.host, context);
|
|
115
|
+
const em = this.ast.size;
|
|
116
|
+
const gap = em * 0.16;
|
|
117
|
+
const { commands, bounds: markBounds } = prepareArpeggioShape(this.host.box.h, em, this.ast.direction);
|
|
118
|
+
this.markCommands = commands;
|
|
119
|
+
this.markX = -markBounds.x;
|
|
120
|
+
this.hostX = markBounds.w + gap;
|
|
121
|
+
this.box.w = this.hostX + this.host.box.w;
|
|
122
|
+
this.box.h = this.host.box.h;
|
|
123
|
+
this.box.anchor = this.hostX + this.host.box.anchor;
|
|
124
|
+
this.box.visualAxis = this.host.box.visualAxis;
|
|
125
|
+
for (const name in this.host.ports) {
|
|
126
|
+
const port = this.host.ports[name];
|
|
127
|
+
this.ports[name] = { x: this.hostX + port.x, y: port.y };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
onPlaced() {
|
|
131
|
+
this.host.box.x = this.box.x + this.hostX;
|
|
132
|
+
this.host.box.y = this.box.y;
|
|
133
|
+
this.host.onPlaced?.();
|
|
134
|
+
}
|
|
135
|
+
emitPlayback(emitter) {
|
|
136
|
+
emitter.play(this.host);
|
|
137
|
+
const total = emitter.end.clone().sub(emitter.start);
|
|
138
|
+
const step = new Fraction(1, 8);
|
|
139
|
+
const maxStep = total.clone().div(2).div(this.playbackMembers.length - 1);
|
|
140
|
+
if (step.compare(maxStep) > 0)
|
|
141
|
+
step.copyFrom(maxStep);
|
|
142
|
+
const slots = new Map(this.playbackMembers.map((member, index) => [member, index]));
|
|
143
|
+
const owner = this;
|
|
144
|
+
emitter.defer(context => {
|
|
145
|
+
let rootOrigin;
|
|
146
|
+
for (let index = context.events.length - 1; index >= 0; index--) {
|
|
147
|
+
const event = context.events[index];
|
|
148
|
+
if (!event.origins.some(origin => origin.node === owner))
|
|
149
|
+
continue;
|
|
150
|
+
rootOrigin = event.origins[0];
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
if (!rootOrigin)
|
|
154
|
+
return;
|
|
155
|
+
for (const event of context.events) {
|
|
156
|
+
if ((event.kind !== "note-on" && event.kind !== "note-off")
|
|
157
|
+
|| event.origins[0] !== rootOrigin)
|
|
158
|
+
continue;
|
|
159
|
+
const member = event.origins.find(origin => slots.has(origin.node))?.node;
|
|
160
|
+
const slot = member ? slots.get(member) : undefined;
|
|
161
|
+
if (!slot)
|
|
162
|
+
continue;
|
|
163
|
+
event.at.add(step.clone().mul(slot));
|
|
164
|
+
if (event.kind === "note-off" && event.at.compare(emitter.end) > 0) {
|
|
165
|
+
event.at.copyFrom(emitter.end);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
paint(painter) {
|
|
171
|
+
const originX = this.box.x;
|
|
172
|
+
const originY = this.box.y;
|
|
173
|
+
painter.drawPath(this.markCommands, { fill: "#000" }, {
|
|
174
|
+
x: originX + this.markX, y: originY, scaleX: 1, scaleY: 1,
|
|
175
|
+
});
|
|
176
|
+
this.host.paint(painter);
|
|
177
|
+
for (const decoration of this.host.decorations)
|
|
178
|
+
decoration.paint(painter);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Rect } from "../../layout/types.js";
|
|
2
|
+
import type { PathCommand } from "../../render/types.js";
|
|
3
|
+
/** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界。 */
|
|
4
|
+
export declare function prepareArpeggioShape(height: number, em: number, direction: "" | "up" | "down"): {
|
|
5
|
+
commands: PathCommand[];
|
|
6
|
+
bounds: Rect;
|
|
7
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { pathBounds } from "../../layout/path.js";
|
|
2
|
+
const TILE_WIDTH = 54.2813;
|
|
3
|
+
const TILE_HEIGHT = 87.37505;
|
|
4
|
+
const TILE_STEP = 72.8125;
|
|
5
|
+
const ARROW_HEIGHT = 93.6719;
|
|
6
|
+
const ARROW_X = -27.4688;
|
|
7
|
+
const ARROW_Y = -7.9375;
|
|
8
|
+
const ARROW_BOTTOM = ARROW_Y + ARROW_HEIGHT;
|
|
9
|
+
/** MuseScore 4.7.4 导出的琶音波形块,已旋转到自上而下的局部坐标。 */
|
|
10
|
+
const TILE = [
|
|
11
|
+
{ op: "M", x: 28.1406, y: 86.06255 },
|
|
12
|
+
{ op: "C", cx1: 28.1406, cy1: 86.06255, cx2: 30.4531, cy2: 84.07815, x: 32.7656, y: 81.42185 },
|
|
13
|
+
{ op: "C", cx1: 33.7656, cy1: 80.43755, cx2: 34.0938, cy2: 79.10935, x: 34.0938, y: 78.10935 },
|
|
14
|
+
{ op: "C", cx1: 34.0938, cy1: 77.12505, cx2: 33.7656, cy2: 76.45315, x: 33.0938, y: 75.79685 },
|
|
15
|
+
{ op: "L", x: 29.4531, y: 72.15625 },
|
|
16
|
+
{ op: "C", cx1: 28.7969, cy1: 71.50005, cx2: 28.4688, cy2: 70.82815, x: 28.4688, y: 70.17185 },
|
|
17
|
+
{ op: "C", cx1: 28.4688, cy1: 68.84375, cx2: 29.4531, cy2: 68.18755, x: 29.4531, y: 68.18755 },
|
|
18
|
+
{ op: "C", cx1: 29.4531, cy1: 68.18755, cx2: 47.6563, cy2: 47.00005, x: 50.6406, y: 43.68755 },
|
|
19
|
+
{ op: "C", cx1: 53.2969, cy1: 40.04685, cx2: 54.2813, cy2: 36.40625, x: 54.2813, y: 33.09375 },
|
|
20
|
+
{ op: "C", cx1: 54.2813, cy1: 26.14065, cx2: 50.3125, cy2: 19.85935, x: 47.3281, y: 17.21875 },
|
|
21
|
+
{ op: "L", x: 31.4375, y: 1.32812 },
|
|
22
|
+
{ op: "C", cx1: 30.4531, cy1: 0.32812, cx2: 29.7969, cy2: 0, x: 28.7969, y: 0 },
|
|
23
|
+
{ op: "C", cx1: 27.1406, cy1: 0, cx2: 25.8125, cy2: 1.32812, x: 25.8125, y: 1.32812 },
|
|
24
|
+
{ op: "C", cx1: 25.8125, cy1: 1.32812, cx2: 23.5, cy2: 3.3125, x: 21.1875, y: 5.95312 },
|
|
25
|
+
{ op: "C", cx1: 20.1875, cy1: 6.95313, cx2: 19.8594, cy2: 7.9375, x: 19.8594, y: 8.9375 },
|
|
26
|
+
{ op: "C", cx1: 19.8594, cy1: 9.92188, cx2: 20.1875, cy2: 10.59375, x: 20.8594, y: 11.25 },
|
|
27
|
+
{ op: "L", x: 24.5, y: 14.89063 },
|
|
28
|
+
{ op: "C", cx1: 25.1563, cy1: 15.5625, cx2: 25.4844, cy2: 16.21875, x: 25.4844, y: 16.875 },
|
|
29
|
+
{ op: "C", cx1: 25.4844, cy1: 18.20315, cx2: 24.5, cy2: 19.20315, x: 24.5, y: 19.20315 },
|
|
30
|
+
{ op: "C", cx1: 24.5, cy1: 19.20315, cx2: 6.625, cy2: 40.37505, x: 3.64063, y: 43.68755 },
|
|
31
|
+
{ op: "C", cx1: 1, cy1: 47.32815, cx2: 0, cy2: 50.96875, x: 0, y: 54.28125 },
|
|
32
|
+
{ op: "C", cx1: 0, cy1: 61.23435, cx2: 3.96875, cy2: 67.51565, x: 6.95313, y: 70.17185 },
|
|
33
|
+
{ op: "L", x: 22.8438, y: 86.06255 },
|
|
34
|
+
{ op: "C", cx1: 23.8281, cy1: 87.04685, cx2: 24.8281, cy2: 87.37505, x: 25.4844, y: 87.37505 },
|
|
35
|
+
{ op: "C", cx1: 27.1406, cy1: 87.37505, cx2: 28.1406, cy2: 86.06255, x: 28.1406, y: 86.06255 },
|
|
36
|
+
];
|
|
37
|
+
/** MuseScore 下行琶音的末端箭头轮廓,坐标方向同 TILE。 */
|
|
38
|
+
const DOWN_ARROW = [
|
|
39
|
+
{ op: "M", x: 111.8751, y: 21.5156 },
|
|
40
|
+
{ op: "C", cx1: 112.5313, cy1: 20.1875, cx2: 113.5313, cy2: 18.2031, x: 113.5313, y: 16.875 },
|
|
41
|
+
{ op: "C", cx1: 113.5313, cy1: 12.23438, cx2: 109.8907, cy2: 8.9375, x: 105.2501, y: 8.9375 },
|
|
42
|
+
{ op: "C", cx1: 103.9219, cy1: 8.9375, cx2: 101.6094, cy2: 9.26563, x: 100.2813, y: 10.25 },
|
|
43
|
+
{ op: "L", x: 72.1563, y: 31.7656 },
|
|
44
|
+
{ op: "L", x: 72.1563, y: 28.4531 },
|
|
45
|
+
{ op: "C", cx1: 72.1563, cy1: 16.54688, cx2: 66.8594, cy2: 8.26563, x: 58.2501, y: 0.98437 },
|
|
46
|
+
{ op: "C", cx1: 57.2657, cy1: 0.32812, cx2: 55.9376, cy2: 0, x: 55.2657, y: 0 },
|
|
47
|
+
{ op: "C", cx1: 53.6251, cy1: 0, cx2: 52.9532, cy2: 0.98437, x: 52.9532, y: 0.98437 },
|
|
48
|
+
{ op: "C", cx1: 51.6251, cy1: 2.3125, cx2: 49.3126, cy2: 4.29687, x: 48.3282, y: 5.625 },
|
|
49
|
+
{ op: "C", cx1: 47.3282, cy1: 6.9375, cx2: 47.0001, cy2: 7.9375, x: 47.0001, y: 8.9375 },
|
|
50
|
+
{ op: "C", cx1: 47.0001, cy1: 9.92188, cx2: 47.3282, cy2: 10.57813, x: 47.6563, y: 11.25 },
|
|
51
|
+
{ op: "C", cx1: 49.9688, cy1: 13.5625, cx2: 50.9688, cy2: 16.21875, x: 50.9688, y: 18.8594 },
|
|
52
|
+
{ op: "C", cx1: 50.9688, cy1: 24.8125, cx2: 46.3282, cy2: 30.1094, x: 41.0469, y: 30.1094 },
|
|
53
|
+
{ op: "C", cx1: 39.3907, cy1: 30.1094, cx2: 37.7344, cy2: 29.4531, x: 36.4063, y: 28.4531 },
|
|
54
|
+
{ op: "L", x: 12.9063, y: 10.57813 },
|
|
55
|
+
{ op: "C", cx1: 11.5782, cy1: 9.59375, cx2: 9.5938, cy2: 8.9375, x: 7.9375, y: 8.9375 },
|
|
56
|
+
{ op: "C", cx1: 3.6407, cy1: 8.9375, cx2: 0, cy2: 12.57813, x: 0, y: 16.875 },
|
|
57
|
+
{ op: "C", cx1: 0, cy1: 18.2031, cx2: 0.6563, cy2: 20.1875, x: 1.3125, y: 21.5156 },
|
|
58
|
+
{ op: "L", x: 49.9688, y: 90.3594 },
|
|
59
|
+
{ op: "C", cx1: 51.2969, cy1: 92.3438, cx2: 54.2813, cy2: 93.6719, x: 56.5938, y: 93.6719 },
|
|
60
|
+
{ op: "C", cx1: 58.9063, cy1: 93.6719, cx2: 61.8907, cy2: 92.3438, x: 63.2188, y: 90.3594 },
|
|
61
|
+
{ op: "L", x: 111.8751, y: 21.5156 },
|
|
62
|
+
];
|
|
63
|
+
function appendShape(output, shape, width, height, offsetX, offsetY, scale, reverse) {
|
|
64
|
+
const x = (value) => offsetX + (reverse ? width - value : value) * scale;
|
|
65
|
+
const y = (value) => offsetY + (reverse ? height - value : value) * scale;
|
|
66
|
+
for (const command of shape) {
|
|
67
|
+
if (command.op === "Z")
|
|
68
|
+
output.push(command);
|
|
69
|
+
else if (command.op === "C")
|
|
70
|
+
output.push({
|
|
71
|
+
op: "C",
|
|
72
|
+
cx1: x(command.cx1), cy1: y(command.cy1),
|
|
73
|
+
cx2: x(command.cx2), cy2: y(command.cy2),
|
|
74
|
+
x: x(command.x), y: y(command.y),
|
|
75
|
+
});
|
|
76
|
+
else if (command.op === "Q")
|
|
77
|
+
output.push({
|
|
78
|
+
op: "Q",
|
|
79
|
+
cx: x(command.cx), cy: y(command.cy),
|
|
80
|
+
x: x(command.x), y: y(command.y),
|
|
81
|
+
});
|
|
82
|
+
else
|
|
83
|
+
output.push({ op: command.op, x: x(command.x), y: y(command.y) });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界。 */
|
|
87
|
+
export function prepareArpeggioShape(height, em, direction) {
|
|
88
|
+
const arrow = direction === "up" || direction === "down";
|
|
89
|
+
const segments = Math.max(1, Math.round(height / (em * 0.3)));
|
|
90
|
+
const regularCount = arrow ? Math.max(0, segments - 1) : segments;
|
|
91
|
+
const naturalHeight = !arrow
|
|
92
|
+
? TILE_HEIGHT + TILE_STEP * (regularCount - 1)
|
|
93
|
+
: ARROW_BOTTOM + TILE_STEP * regularCount;
|
|
94
|
+
const scale = height / naturalHeight;
|
|
95
|
+
const commands = [];
|
|
96
|
+
for (let index = 0; index < regularCount; index++) {
|
|
97
|
+
appendShape(commands, TILE, TILE_WIDTH, TILE_HEIGHT, 0, index * TILE_STEP * scale, scale, direction === "");
|
|
98
|
+
}
|
|
99
|
+
if (arrow) {
|
|
100
|
+
appendShape(commands, DOWN_ARROW, 113.5313, ARROW_HEIGHT, ARROW_X * scale, (regularCount * TILE_STEP + ARROW_Y) * scale, scale, false);
|
|
101
|
+
}
|
|
102
|
+
const bounds = pathBounds(commands);
|
|
103
|
+
if (direction !== "up")
|
|
104
|
+
return { commands, bounds };
|
|
105
|
+
const rotated = commands.map(command => command.op === "Z" ? command : command.op === "C" ? {
|
|
106
|
+
op: "C",
|
|
107
|
+
cx1: bounds.x + bounds.w - (command.cx1 - bounds.x), cy1: height - command.cy1,
|
|
108
|
+
cx2: bounds.x + bounds.w - (command.cx2 - bounds.x), cy2: height - command.cy2,
|
|
109
|
+
x: bounds.x + bounds.w - (command.x - bounds.x), y: height - command.y,
|
|
110
|
+
} : command.op === "Q" ? {
|
|
111
|
+
op: "Q",
|
|
112
|
+
cx: bounds.x + bounds.w - (command.cx - bounds.x), cy: height - command.cy,
|
|
113
|
+
x: bounds.x + bounds.w - (command.x - bounds.x), y: height - command.y,
|
|
114
|
+
} : {
|
|
115
|
+
op: command.op,
|
|
116
|
+
x: bounds.x + bounds.w - (command.x - bounds.x), y: height - command.y,
|
|
117
|
+
});
|
|
118
|
+
return {
|
|
119
|
+
commands: rotated,
|
|
120
|
+
bounds: { x: bounds.x, y: height - bounds.y - bounds.h, w: bounds.w, h: bounds.h },
|
|
121
|
+
};
|
|
122
|
+
}
|