jpfun 0.1.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/LICENSE +201 -0
- package/README.md +134 -0
- package/dist/diagnostic.d.ts +27 -0
- package/dist/diagnostic.js +141 -0
- package/dist/functions/ASTtypes.d.ts +115 -0
- package/dist/functions/ASTtypes.js +187 -0
- package/dist/functions/bar/index.d.ts +2 -0
- package/dist/functions/bar/index.js +180 -0
- package/dist/functions/beam/auto.d.ts +4 -0
- package/dist/functions/beam/auto.js +202 -0
- package/dist/functions/beam/index.d.ts +2 -0
- package/dist/functions/beam/index.js +54 -0
- package/dist/functions/beam/layout.d.ts +9 -0
- package/dist/functions/beam/layout.js +233 -0
- package/dist/functions/box/index.d.ts +2 -0
- package/dist/functions/box/index.js +202 -0
- package/dist/functions/br/index.d.ts +2 -0
- package/dist/functions/br/index.js +83 -0
- package/dist/functions/dash/index.d.ts +2 -0
- package/dist/functions/dash/index.js +61 -0
- package/dist/functions/default.d.ts +2 -0
- package/dist/functions/default.js +32 -0
- package/dist/functions/div/index.d.ts +50 -0
- package/dist/functions/div/index.js +190 -0
- package/dist/functions/dot/index.d.ts +2 -0
- package/dist/functions/dot/index.js +160 -0
- package/dist/functions/grace/index.d.ts +84 -0
- package/dist/functions/grace/index.js +464 -0
- package/dist/functions/key/index.d.ts +2 -0
- package/dist/functions/key/index.js +40 -0
- package/dist/functions/note/accidentals.d.ts +10 -0
- package/dist/functions/note/accidentals.js +60 -0
- package/dist/functions/note/index.d.ts +2 -0
- package/dist/functions/note/index.js +284 -0
- package/dist/functions/note/noteNameFSM.d.ts +8 -0
- package/dist/functions/note/noteNameFSM.js +192 -0
- package/dist/functions/over/index.d.ts +2 -0
- package/dist/functions/over/index.js +307 -0
- package/dist/functions/page/index.d.ts +20 -0
- package/dist/functions/page/index.js +63 -0
- package/dist/functions/set/index.d.ts +2 -0
- package/dist/functions/set/index.js +55 -0
- package/dist/functions/stack/index.d.ts +2 -0
- package/dist/functions/stack/index.js +164 -0
- package/dist/functions/tempo/index.d.ts +2 -0
- package/dist/functions/tempo/index.js +37 -0
- package/dist/functions/text/index.d.ts +2 -0
- package/dist/functions/text/index.js +65 -0
- package/dist/functions/tie/index.d.ts +2 -0
- package/dist/functions/tie/index.js +301 -0
- package/dist/functions/voice/index.d.ts +3 -0
- package/dist/functions/voice/index.js +714 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/layout/default.d.ts +12 -0
- package/dist/layout/default.js +27 -0
- package/dist/layout/engine.d.ts +47 -0
- package/dist/layout/engine.js +410 -0
- package/dist/layout/model.d.ts +73 -0
- package/dist/layout/model.js +422 -0
- package/dist/layout/page.d.ts +27 -0
- package/dist/layout/page.js +121 -0
- package/dist/layout/types.d.ts +197 -0
- package/dist/layout/types.js +1 -0
- package/dist/lowering/loweringContext.d.ts +126 -0
- package/dist/lowering/loweringContext.js +625 -0
- package/dist/lowering/track.d.ts +61 -0
- package/dist/lowering/track.js +35 -0
- package/dist/lowering/types.d.ts +176 -0
- package/dist/lowering/types.js +82 -0
- package/dist/parser/grammarType.d.ts +81 -0
- package/dist/parser/grammarType.js +1 -0
- package/dist/parser/parse-utils/brace-utils.d.ts +1 -0
- package/dist/parser/parse-utils/brace-utils.js +29 -0
- package/dist/parser/parse-utils/call-utils.d.ts +24 -0
- package/dist/parser/parse-utils/call-utils.js +178 -0
- package/dist/parser/parse-utils/label-utils.d.ts +4 -0
- package/dist/parser/parse-utils/label-utils.js +17 -0
- package/dist/parser/parse-utils/length-utils.d.ts +3 -0
- package/dist/parser/parse-utils/length-utils.js +20 -0
- package/dist/parser/parse-utils/note-utils.d.ts +10 -0
- package/dist/parser/parse-utils/note-utils.js +111 -0
- package/dist/parser/parserContext.d.ts +93 -0
- package/dist/parser/parserContext.js +542 -0
- package/dist/parser/preprocess.d.ts +6 -0
- package/dist/parser/preprocess.js +193 -0
- package/dist/parser/types.d.ts +31 -0
- package/dist/parser/types.js +2 -0
- package/dist/pipeline.d.ts +36 -0
- package/dist/pipeline.js +51 -0
- package/dist/render/canvas.d.ts +19 -0
- package/dist/render/canvas.js +121 -0
- package/dist/render/index.d.ts +5 -0
- package/dist/render/index.js +5 -0
- package/dist/render/recording.d.ts +46 -0
- package/dist/render/recording.js +76 -0
- package/dist/render/svg.d.ts +18 -0
- package/dist/render/svg.js +126 -0
- package/dist/render/text.d.ts +6 -0
- package/dist/render/text.js +17 -0
- package/dist/render/types.d.ts +86 -0
- package/dist/render/types.js +1 -0
- package/grammar.md +123 -0
- package/package.json +58 -0
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { Diagnostic } from "../diagnostic.js";
|
|
2
|
+
/** 固化到 Temporal.addon 的函数语义统一使用 `@主函数名` */
|
|
3
|
+
export function functionAddonKey(name) {
|
|
4
|
+
return `@${name}`;
|
|
5
|
+
}
|
|
6
|
+
export class ASTNodeBase {
|
|
7
|
+
sourceSpan; // 和源码的映射
|
|
8
|
+
parent;
|
|
9
|
+
// 空数组表示应该有 null表示自己就是叶子 和 timeFlowMode 的默认实现对应
|
|
10
|
+
get children() { return null; }
|
|
11
|
+
constructor(sourceSpan, parent = null) {
|
|
12
|
+
this.sourceSpan = sourceSpan;
|
|
13
|
+
this.parent = parent;
|
|
14
|
+
}
|
|
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
|
+
/**
|
|
37
|
+
* 进入当前层级的回调
|
|
38
|
+
* @param ctx 当前的 lowering 上下文
|
|
39
|
+
* @param track 当前事件所在的纵向音轨
|
|
40
|
+
* @returns 当前节点产生的事件
|
|
41
|
+
*/
|
|
42
|
+
loweringEnter(_ctx, _track) {
|
|
43
|
+
return [];
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 时间求解模式
|
|
47
|
+
* 由 loweringContext 调用,决定当前节点在时间求解阶段的展开方式
|
|
48
|
+
* @returns 返回 null 表示没有子元素,返回对象表示参与时间求解
|
|
49
|
+
*/
|
|
50
|
+
timeFlowModel() { return null; }
|
|
51
|
+
/**
|
|
52
|
+
* 离开当前层级的回调 同 loweringEnter
|
|
53
|
+
*/
|
|
54
|
+
loweringExit(_ctx, _track) {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
// 去糖后文本输出
|
|
58
|
+
toString(source) {
|
|
59
|
+
return source.slice(this.sourceSpan.start, this.sourceSpan.end);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export class ASTTextNode extends ASTNodeBase {
|
|
63
|
+
}
|
|
64
|
+
// label节点只负责语义标记,不参与渲染和其他逻辑
|
|
65
|
+
// 绑定也在解析后完成 和本节点无关
|
|
66
|
+
// 主要用途是高亮等编辑器功能
|
|
67
|
+
export class ASTLabelNode extends ASTNodeBase {
|
|
68
|
+
label;
|
|
69
|
+
// parent 就是指向的节点
|
|
70
|
+
constructor(sourceSpan, label, parent) {
|
|
71
|
+
super(sourceSpan, parent);
|
|
72
|
+
this.label = label;
|
|
73
|
+
}
|
|
74
|
+
toString(_) {
|
|
75
|
+
return `@${this.label} `;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// 表示`{}`
|
|
79
|
+
// span.start 是 `{` 的位置; (span.end-1) 是 `}` 的位置
|
|
80
|
+
// 判断是否是创建的节点:
|
|
81
|
+
export class ASTBraceNode extends ASTNodeBase {
|
|
82
|
+
content;
|
|
83
|
+
get children() { return this.content; }
|
|
84
|
+
constructor(sourceSpan, content, parent = null) {
|
|
85
|
+
super(sourceSpan, parent);
|
|
86
|
+
this.content = content;
|
|
87
|
+
this.content.forEach(item => item.parent = this);
|
|
88
|
+
}
|
|
89
|
+
innerSpan() {
|
|
90
|
+
return ASTBraceNode.getContentSpan(this.content);
|
|
91
|
+
}
|
|
92
|
+
// 是复制的
|
|
93
|
+
static getContentSpan(content) {
|
|
94
|
+
if (Array.isArray(content)) {
|
|
95
|
+
if (content.length === 0)
|
|
96
|
+
return { start: 0, end: 0 };
|
|
97
|
+
const firstSpan = ASTBraceNode.getContentSpan(content[0]);
|
|
98
|
+
const lastSpan = ASTBraceNode.getContentSpan(content[content.length - 1]);
|
|
99
|
+
return { start: firstSpan.start, end: lastSpan.end };
|
|
100
|
+
}
|
|
101
|
+
else
|
|
102
|
+
return { start: content.sourceSpan.start, end: content.sourceSpan.end };
|
|
103
|
+
}
|
|
104
|
+
timeFlowModel() {
|
|
105
|
+
return {
|
|
106
|
+
children: this.content,
|
|
107
|
+
mode: "sequence",
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
toString(source) {
|
|
111
|
+
return `{${this.content.map(item => item.toString(source)).join("")}}`;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** 参数名 -> 类型的规则:命名参数只认 def.args,位置参数可回落到 extraArgType */
|
|
115
|
+
export function resolveArgType(def, name, index) {
|
|
116
|
+
if (name)
|
|
117
|
+
return def.args.find(arg => arg.name?.toLowerCase() === name)?.type;
|
|
118
|
+
return def.args[index]?.type ?? (def.allowExtraArgs ? def.extraArgType : undefined);
|
|
119
|
+
}
|
|
120
|
+
// 所有函数节点的基类,提供通用的参数提取方法和标签功能
|
|
121
|
+
// 非正常函数则实例化该函数 特征是getDef为undefined
|
|
122
|
+
export class ASTFunctionNode extends ASTNodeBase {
|
|
123
|
+
/**
|
|
124
|
+
* 返回真正承载标签的 AST 节点,null 表示不可被标注
|
|
125
|
+
*/
|
|
126
|
+
labelable() { return null; }
|
|
127
|
+
label; // 可选的标签名,只对 labelable() 选中的节点有效; 或者是label节点
|
|
128
|
+
// 获取函数定义 对于未知函数,不定义def
|
|
129
|
+
static def;
|
|
130
|
+
get def() {
|
|
131
|
+
return this.constructor.def;
|
|
132
|
+
} // 默认让实例返回静态属性def 后面的类应该只重写static def
|
|
133
|
+
// 实例访问: new ().def 或 new ().constructor.def
|
|
134
|
+
// 静态访问: 类名.def 或 类名.prototype.def
|
|
135
|
+
get callName() {
|
|
136
|
+
const names = this.def?.name;
|
|
137
|
+
if (!names)
|
|
138
|
+
return "";
|
|
139
|
+
return Array.isArray(names) ? names[0] : names;
|
|
140
|
+
}
|
|
141
|
+
// 去糖函数 详见 ParserContext 中 deSugarAtomFns 和 deSugarRelationFns 的定义
|
|
142
|
+
static deSugarAtom = () => null; // 默认没有去糖,子类只需要定义static deSugarAtom方法即可
|
|
143
|
+
get deSugarAtom() { return this.constructor.deSugarAtom; }
|
|
144
|
+
static deSugarRelation = () => null;
|
|
145
|
+
get deSugarRelation() { return this.constructor.deSugarRelation; }
|
|
146
|
+
// 通用的参数提取方法 从定义找传参
|
|
147
|
+
getArgValue(args, ctx) {
|
|
148
|
+
const def = this.def;
|
|
149
|
+
if (!def)
|
|
150
|
+
return [];
|
|
151
|
+
const defArgs = def.args;
|
|
152
|
+
// 使用第一个名称作为前缀
|
|
153
|
+
const prefix = Array.isArray(def.name) ? def.name[0] : def.name;
|
|
154
|
+
return defArgs.map((argDef, index) => {
|
|
155
|
+
let argNameL = argDef.name ? argDef.name.toLowerCase() : null; // 统一小写处理
|
|
156
|
+
// 先查询是否传递
|
|
157
|
+
let argValue = (argNameL ? args.get(argNameL) : null)
|
|
158
|
+
?? args.get(index) // 优先使用命名参数,否则使用位置参数
|
|
159
|
+
?? (argNameL ? ctx.variables[`${prefix}.${argNameL}`.toLowerCase()] : null)
|
|
160
|
+
?? argDef.default;
|
|
161
|
+
if (argValue === null)
|
|
162
|
+
throw Diagnostic.error.MissingArg(prefix, argDef.name || index, this.sourceSpan);
|
|
163
|
+
return argValue; // 假设解析器已经保证了类型正确
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
toString(source) {
|
|
167
|
+
const name = this.callName;
|
|
168
|
+
if (!name)
|
|
169
|
+
return super.toString(source);
|
|
170
|
+
return `@${name} `;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 用于语法糖向前结合
|
|
174
|
+
* 一般向前结合都是寻找brace或者function
|
|
175
|
+
* 文本节点会打断连接 注意解析时已经跳过了空格 因此允许语法糖之间有空格
|
|
176
|
+
*/
|
|
177
|
+
static findLastFuncContentNode(nodes, i) {
|
|
178
|
+
for (; i >= 0; i--) {
|
|
179
|
+
const n = nodes[i];
|
|
180
|
+
if (n instanceof ASTTextNode)
|
|
181
|
+
return null;
|
|
182
|
+
if (n instanceof ASTFunctionNode || n instanceof ASTBraceNode)
|
|
183
|
+
return n;
|
|
184
|
+
}
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { TemporalNodeBase } from "../../lowering/types.js";
|
|
3
|
+
class BarFunction extends ASTFunctionNode {
|
|
4
|
+
static def = {
|
|
5
|
+
name: ["bar", "|"],
|
|
6
|
+
description: "小节线",
|
|
7
|
+
example: `@bar(type, lengthEM) 创建一个小节线
|
|
8
|
+
语法糖:
|
|
9
|
+
- type0: '|' 普通小节线
|
|
10
|
+
- type1: '||' 终止小节线 左细右粗
|
|
11
|
+
- type2: '|:' 重复小节线 左粗右细
|
|
12
|
+
- type3: ':|'
|
|
13
|
+
- type4: ':|:' 左右反复
|
|
14
|
+
`,
|
|
15
|
+
allowExtraArgs: false,
|
|
16
|
+
args: [
|
|
17
|
+
{
|
|
18
|
+
name: "type",
|
|
19
|
+
type: "number",
|
|
20
|
+
default: 0,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "length",
|
|
24
|
+
type: "length",
|
|
25
|
+
default: {
|
|
26
|
+
value: 1,
|
|
27
|
+
unit: "em",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
]
|
|
31
|
+
};
|
|
32
|
+
static deSugarAtom(source, start, _end) {
|
|
33
|
+
let pos = start;
|
|
34
|
+
let type = 0;
|
|
35
|
+
const slice2 = source.slice(pos, pos + 2);
|
|
36
|
+
if (slice2 === "||")
|
|
37
|
+
type = 1, pos += 2;
|
|
38
|
+
else if (slice2 === "|:")
|
|
39
|
+
type = 2, pos += 2;
|
|
40
|
+
else if (slice2 === ":|") {
|
|
41
|
+
if (source[pos + 2] === ":")
|
|
42
|
+
type = 4, pos += 3;
|
|
43
|
+
else
|
|
44
|
+
type = 3, pos += 2;
|
|
45
|
+
}
|
|
46
|
+
else if (source[pos] === "|")
|
|
47
|
+
type = 0, pos += 1;
|
|
48
|
+
else
|
|
49
|
+
return null;
|
|
50
|
+
const argMap = new Map();
|
|
51
|
+
argMap.set("type", type);
|
|
52
|
+
const node = {
|
|
53
|
+
kind: "call",
|
|
54
|
+
typed: true,
|
|
55
|
+
name: "bar",
|
|
56
|
+
args: argMap,
|
|
57
|
+
span: { start, end: pos },
|
|
58
|
+
syntaxKind: "atom",
|
|
59
|
+
};
|
|
60
|
+
return { next: pos, node };
|
|
61
|
+
}
|
|
62
|
+
;
|
|
63
|
+
type;
|
|
64
|
+
barLength; // 固化值
|
|
65
|
+
size;
|
|
66
|
+
constructor(sourceSpan, args, ctx, parent = null) {
|
|
67
|
+
super(sourceSpan, parent);
|
|
68
|
+
const [type, barlen] = this.getArgValue(args, ctx);
|
|
69
|
+
this.type = type;
|
|
70
|
+
this.barLength = ctx.length2px(barlen);
|
|
71
|
+
this.size = ctx.fontSize;
|
|
72
|
+
}
|
|
73
|
+
loweringEnter() {
|
|
74
|
+
return [new BarTemporalNode(this)];
|
|
75
|
+
}
|
|
76
|
+
toString() {
|
|
77
|
+
return `@bar(${this.type}, ${this.barLength}px)`;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
export const BarNode = BarFunction;
|
|
81
|
+
class BarTemporalNode extends TemporalNodeBase {
|
|
82
|
+
lines = [];
|
|
83
|
+
repeatDots = [];
|
|
84
|
+
constructor(ast) {
|
|
85
|
+
super();
|
|
86
|
+
this.ast = ast;
|
|
87
|
+
this.T = 0;
|
|
88
|
+
this.type = 0 /* ColType.ANCHOR */;
|
|
89
|
+
this.initLayoutBox();
|
|
90
|
+
// 由于有最小时长,因此设置alpha依然有效
|
|
91
|
+
this.springConfig = {
|
|
92
|
+
alpha_L: 128,
|
|
93
|
+
alpha_R: 128,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 给 bar 两侧的相邻元素增加 mu,避免被压缩到一起
|
|
98
|
+
*/
|
|
99
|
+
prepareHorizontal(line) {
|
|
100
|
+
const run = line.trackRuns.get(this.track);
|
|
101
|
+
const index = run?.indexOf(this) ?? -1;
|
|
102
|
+
if (!run || index < 0)
|
|
103
|
+
return;
|
|
104
|
+
line.registerHorizontalLayoutHook(this, this, ({ columns, start }) => {
|
|
105
|
+
const current = columns[start].find(element => element.time === this);
|
|
106
|
+
if (!current)
|
|
107
|
+
return;
|
|
108
|
+
const elementOf = (node) => {
|
|
109
|
+
const column = line.columnOf(node);
|
|
110
|
+
return column < 0 ? undefined : columns[column].find(element => element.time === node);
|
|
111
|
+
};
|
|
112
|
+
const left = index > 0 ? elementOf(run[index - 1]) : undefined;
|
|
113
|
+
if (left) {
|
|
114
|
+
left.config.mu_R *= 4;
|
|
115
|
+
current.config.mu_L *= 4;
|
|
116
|
+
}
|
|
117
|
+
const rightNode = run[index + 1];
|
|
118
|
+
const right = rightNode ? elementOf(rightNode) : undefined;
|
|
119
|
+
// 相邻两个 bar 的中间 gap 由右侧 bar 处理,避免重复增强
|
|
120
|
+
if (right && !(rightNode instanceof BarTemporalNode)) {
|
|
121
|
+
current.config.mu_R *= 4;
|
|
122
|
+
right.config.mu_L *= 4;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
prepareLayout() {
|
|
127
|
+
const { type, barLength, size } = this.ast;
|
|
128
|
+
this.lines.length = 0;
|
|
129
|
+
this.repeatDots.length = 0;
|
|
130
|
+
const h = Math.max(barLength, size * 0.6);
|
|
131
|
+
const thin = Math.max(1, size * 0.065);
|
|
132
|
+
const thick = thin * 2.6;
|
|
133
|
+
const gap = thin * 1.8;
|
|
134
|
+
const dotRadius = thin * 0.8;
|
|
135
|
+
const dotGap = gap + dotRadius;
|
|
136
|
+
const hasLeftDots = type === 3 || type === 4;
|
|
137
|
+
const hasRightDots = type === 2 || type === 4;
|
|
138
|
+
let x = hasLeftDots ? dotRadius * 2 + dotGap : 0;
|
|
139
|
+
if (type === 1 || type === 3 || type === 4) {
|
|
140
|
+
this.lines.push({ x, w: thin });
|
|
141
|
+
x += thin + gap;
|
|
142
|
+
this.lines.push({ x, w: thick });
|
|
143
|
+
x += thick;
|
|
144
|
+
}
|
|
145
|
+
else if (type === 2) {
|
|
146
|
+
this.lines.push({ x, w: thick });
|
|
147
|
+
x += thick + gap;
|
|
148
|
+
this.lines.push({ x, w: thin });
|
|
149
|
+
x += thin;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
this.lines.push({ x, w: thin });
|
|
153
|
+
x += thin;
|
|
154
|
+
}
|
|
155
|
+
if (hasLeftDots) {
|
|
156
|
+
const dotX = dotRadius;
|
|
157
|
+
this.repeatDots.push({ x: dotX, y: h * 0.38 });
|
|
158
|
+
this.repeatDots.push({ x: dotX, y: h * 0.62 });
|
|
159
|
+
}
|
|
160
|
+
if (hasRightDots) {
|
|
161
|
+
const dotX = x + dotGap;
|
|
162
|
+
this.repeatDots.push({ x: dotX, y: h * 0.38 });
|
|
163
|
+
this.repeatDots.push({ x: dotX, y: h * 0.62 });
|
|
164
|
+
x = dotX + dotRadius;
|
|
165
|
+
}
|
|
166
|
+
this.box.w = x;
|
|
167
|
+
this.box.h = h;
|
|
168
|
+
this.box.anchor = x / 2;
|
|
169
|
+
this.box.visualAxis = h / 2;
|
|
170
|
+
}
|
|
171
|
+
paint(painter) {
|
|
172
|
+
for (const line of this.lines) {
|
|
173
|
+
painter.drawRect(this.box.x + line.x, this.box.y, line.w, this.box.h, { fill: "#000" });
|
|
174
|
+
}
|
|
175
|
+
const radius = Math.max(1, this.box.w * 0.06);
|
|
176
|
+
for (const dot of this.repeatDots) {
|
|
177
|
+
painter.drawCircle(this.box.x + dot.x, this.box.y + dot.y, radius, { fill: "#000" });
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type LoweringResult } from "../../lowering/types.js";
|
|
2
|
+
import type { LayoutAttachment } from "../../layout/types.js";
|
|
3
|
+
/** beam 域在 lowering 固化后统一生成作用域连接和相邻自动连接 */
|
|
4
|
+
export declare function createAutomaticBeamAttachments(result: LoweringResult): LayoutAttachment[];
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { isVisualTemporalNode, } from "../../lowering/types.js";
|
|
2
|
+
import { DIV_ADDON_KEY, DivNode, } from "../div/index.js";
|
|
3
|
+
import { collectExplicitBeamEndpoints, createBeamLayoutAttachment, } from "./layout.js";
|
|
4
|
+
const QUARTER_NOTE_DURATION = 1;
|
|
5
|
+
const TIME_EPSILON = 1e-6;
|
|
6
|
+
/** 只返回实际参与当前事件 div addon 的 div,先沿祖先链找,不够再按书写顺序向下补 */
|
|
7
|
+
function findActiveDivs(node) {
|
|
8
|
+
const result = [];
|
|
9
|
+
let remainingLevels = Math.max(0, Math.floor(Number(node.addon?.[DIV_ADDON_KEY]) || 0));
|
|
10
|
+
const take = (div) => {
|
|
11
|
+
result.push(div);
|
|
12
|
+
remainingLevels -= Math.max(0, div.n);
|
|
13
|
+
};
|
|
14
|
+
let ast = node.ast.parent;
|
|
15
|
+
while (ast && remainingLevels > 0) {
|
|
16
|
+
if (ast instanceof DivNode)
|
|
17
|
+
take(ast);
|
|
18
|
+
ast = ast.parent;
|
|
19
|
+
}
|
|
20
|
+
// 复合节点(如 up)把代表成员的修饰提升成了自己的,提供层级的 div 在子树里
|
|
21
|
+
const descend = (parent) => {
|
|
22
|
+
for (const child of parent.children ?? []) {
|
|
23
|
+
if (remainingLevels <= 0)
|
|
24
|
+
return;
|
|
25
|
+
if (child instanceof DivNode)
|
|
26
|
+
take(child);
|
|
27
|
+
descend(child);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
descend(node.ast);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
/** 收集每个 div 作用域内的可见事件,顺序与最终时间列一致 */
|
|
34
|
+
function collectDivScopes(result) {
|
|
35
|
+
const scopesByOwner = new Map();
|
|
36
|
+
const nodeScopes = new Map();
|
|
37
|
+
for (const column of result.columns) {
|
|
38
|
+
for (const node of column) {
|
|
39
|
+
if (!isVisualTemporalNode(node))
|
|
40
|
+
continue;
|
|
41
|
+
const owners = findActiveDivs(node);
|
|
42
|
+
if (owners.length === 0)
|
|
43
|
+
continue;
|
|
44
|
+
const memberships = [];
|
|
45
|
+
for (const owner of owners) {
|
|
46
|
+
let scope = scopesByOwner.get(owner);
|
|
47
|
+
if (!scope) {
|
|
48
|
+
scope = { owner, nodes: [] };
|
|
49
|
+
scopesByOwner.set(owner, scope);
|
|
50
|
+
}
|
|
51
|
+
scope.nodes.push(node);
|
|
52
|
+
memberships.push(scope);
|
|
53
|
+
}
|
|
54
|
+
nodeScopes.set(node, memberships);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
scopes: [...scopesByOwner.values()],
|
|
59
|
+
nodeScopes,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** 嵌套 div 由最外层作用域一次连接,内部级别通过端口自然形成次级 beam */
|
|
63
|
+
function outermostScopes(scopes) {
|
|
64
|
+
const owners = new Set(scopes.map(scope => scope.owner));
|
|
65
|
+
return scopes.filter(scope => {
|
|
66
|
+
let parent = scope.owner.parent;
|
|
67
|
+
while (parent) {
|
|
68
|
+
if (parent instanceof DivNode && owners.has(parent))
|
|
69
|
+
return false;
|
|
70
|
+
parent = parent.parent;
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/** 同一个 div 内部始终按轨道连接,手工 beam 端点会截断自动片段 */
|
|
76
|
+
function createScopeBeams(scopes, explicitEndpoints) {
|
|
77
|
+
const attachments = [];
|
|
78
|
+
for (const scope of outermostScopes(scopes)) {
|
|
79
|
+
const tracks = new Map();
|
|
80
|
+
for (const node of scope.nodes) {
|
|
81
|
+
const trackNodes = tracks.get(node.track);
|
|
82
|
+
if (trackNodes)
|
|
83
|
+
trackNodes.push(node);
|
|
84
|
+
else
|
|
85
|
+
tracks.set(node.track, [node]);
|
|
86
|
+
}
|
|
87
|
+
for (const nodes of tracks.values()) {
|
|
88
|
+
let segment = [];
|
|
89
|
+
const flush = () => {
|
|
90
|
+
if (segment.length >= 2)
|
|
91
|
+
attachments.push(createBeamLayoutAttachment(segment));
|
|
92
|
+
segment = [];
|
|
93
|
+
};
|
|
94
|
+
for (const node of nodes) {
|
|
95
|
+
if (explicitEndpoints.has(node)) {
|
|
96
|
+
flush();
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
segment.push(node);
|
|
100
|
+
}
|
|
101
|
+
flush();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return attachments;
|
|
105
|
+
}
|
|
106
|
+
function beatAt(time) {
|
|
107
|
+
return Math.floor((time + TIME_EPSILON) / QUARTER_NOTE_DURATION);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* 多个独立 div 的默认连接规则
|
|
111
|
+
*
|
|
112
|
+
* MuseScore 和 LilyPond 都以拍号的 beat structure 作为默认分组边界,并允许手工 beam 覆盖。
|
|
113
|
+
* 当前语言没有拍号,暂以四分音符为拍单位。
|
|
114
|
+
* 分组不跨轨道、逻辑行、拍点、小节线、时间间隙、普通长事件、开关边界或显式 beam。
|
|
115
|
+
*/
|
|
116
|
+
function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
|
|
117
|
+
const attachments = [];
|
|
118
|
+
const groups = new Map();
|
|
119
|
+
const lineStarts = new Map();
|
|
120
|
+
let measureStart = 0;
|
|
121
|
+
for (const column of result.columns) {
|
|
122
|
+
for (const node of column) {
|
|
123
|
+
const current = lineStarts.get(node.layoutLine);
|
|
124
|
+
if (current === undefined || node.t < current)
|
|
125
|
+
lineStarts.set(node.layoutLine, node.t);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const flush = (track) => {
|
|
129
|
+
const group = groups.get(track);
|
|
130
|
+
if (!group)
|
|
131
|
+
return;
|
|
132
|
+
groups.delete(track);
|
|
133
|
+
if (group.nodes.length >= 2)
|
|
134
|
+
attachments.push(createBeamLayoutAttachment(group.nodes));
|
|
135
|
+
};
|
|
136
|
+
const flushAll = () => {
|
|
137
|
+
for (const track of [...groups.keys()])
|
|
138
|
+
flush(track);
|
|
139
|
+
};
|
|
140
|
+
for (const column of result.columns) {
|
|
141
|
+
const anchor = column.find(node => node.type === 0 /* ColType.ANCHOR */);
|
|
142
|
+
if (anchor) {
|
|
143
|
+
flushAll();
|
|
144
|
+
measureStart = anchor.t;
|
|
145
|
+
}
|
|
146
|
+
for (const node of column) {
|
|
147
|
+
if (node.type === 0 /* ColType.ANCHOR */)
|
|
148
|
+
continue;
|
|
149
|
+
const memberships = isVisualTemporalNode(node) ? nodeScopes.get(node) : undefined;
|
|
150
|
+
const isIndependentDiv = memberships?.every(scope => scope.nodes.length === 1) ?? false;
|
|
151
|
+
const autoBeamEnabled = memberships?.every(scope => scope.owner.autoBeamEnabled) ?? false;
|
|
152
|
+
const isCandidate = isVisualTemporalNode(node)
|
|
153
|
+
&& isIndependentDiv
|
|
154
|
+
&& autoBeamEnabled
|
|
155
|
+
&& node.T < QUARTER_NOTE_DURATION - TIME_EPSILON
|
|
156
|
+
&& !explicitEndpoints.has(node);
|
|
157
|
+
if (!isCandidate) {
|
|
158
|
+
// 同轨可见元素必须打断相邻关系,即使它本身是零时长文本
|
|
159
|
+
// 不可见的 set/key/tempo 等零时长状态不会打断
|
|
160
|
+
if (isVisualTemporalNode(node) || node.T > TIME_EPSILON)
|
|
161
|
+
flush(node.track);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const lineStart = lineStarts.get(node.layoutLine) ?? node.t;
|
|
165
|
+
const beat = beatAt(node.t - Math.max(measureStart, lineStart));
|
|
166
|
+
const current = groups.get(node.track);
|
|
167
|
+
if (!current) {
|
|
168
|
+
groups.set(node.track, {
|
|
169
|
+
nodes: [node],
|
|
170
|
+
line: node.layoutLine,
|
|
171
|
+
beat,
|
|
172
|
+
endTime: node.t + node.T,
|
|
173
|
+
});
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
const isContinuous = Math.abs(node.t - current.endTime) < TIME_EPSILON;
|
|
177
|
+
if (node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
|
|
178
|
+
flush(node.track);
|
|
179
|
+
groups.set(node.track, {
|
|
180
|
+
nodes: [node],
|
|
181
|
+
line: node.layoutLine,
|
|
182
|
+
beat,
|
|
183
|
+
endTime: node.t + node.T,
|
|
184
|
+
});
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
current.nodes.push(node);
|
|
188
|
+
current.endTime = node.t + node.T;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
flushAll();
|
|
192
|
+
return attachments;
|
|
193
|
+
}
|
|
194
|
+
/** beam 域在 lowering 固化后统一生成作用域连接和相邻自动连接 */
|
|
195
|
+
export function createAutomaticBeamAttachments(result) {
|
|
196
|
+
const { scopes, nodeScopes } = collectDivScopes(result);
|
|
197
|
+
const explicitEndpoints = collectExplicitBeamEndpoints(result.attachments);
|
|
198
|
+
return [
|
|
199
|
+
...createScopeBeams(scopes, explicitEndpoints),
|
|
200
|
+
...createAdjacentBeams(result, nodeScopes, explicitEndpoints),
|
|
201
|
+
];
|
|
202
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { createAutomaticBeamAttachments } from "./auto.js";
|
|
4
|
+
import { createBeamLayoutAttachment, validateExplicitBeamAttachments, } from "./layout.js";
|
|
5
|
+
class BeamFunction extends ASTFunctionNode {
|
|
6
|
+
static loweringAugment = createAutomaticBeamAttachments;
|
|
7
|
+
static loweringFinalize = validateExplicitBeamAttachments;
|
|
8
|
+
static def = {
|
|
9
|
+
name: ["beam"],
|
|
10
|
+
description: "减时线连接",
|
|
11
|
+
example: "@beam(label1, label2, ...): 将多个已标记音符按顺序连接为减时线组",
|
|
12
|
+
allowExtraArgs: true,
|
|
13
|
+
extraArgType: "label",
|
|
14
|
+
args: [],
|
|
15
|
+
};
|
|
16
|
+
endPoints = [];
|
|
17
|
+
constructor(sourceSpan, args, ctx, parent = null) {
|
|
18
|
+
super(sourceSpan, parent);
|
|
19
|
+
for (const [, value] of args) {
|
|
20
|
+
const parsed = value instanceof ASTFunctionNode
|
|
21
|
+
? value
|
|
22
|
+
: ctx.parseArgWithType(value.start, value.end, "label", sourceSpan.start);
|
|
23
|
+
if (parsed instanceof ASTFunctionNode)
|
|
24
|
+
this.endPoints.push(parsed);
|
|
25
|
+
}
|
|
26
|
+
// 未显式给全时,回退到最近可标记对象,至少取两个。
|
|
27
|
+
let k = ctx.labelableNodes.length - 1;
|
|
28
|
+
while (this.endPoints.length < 2 && k >= 0) {
|
|
29
|
+
const candidate = ctx.labelableNodes[k--];
|
|
30
|
+
if (this.endPoints.includes(candidate))
|
|
31
|
+
continue;
|
|
32
|
+
this.endPoints.unshift(candidate);
|
|
33
|
+
}
|
|
34
|
+
if (this.endPoints.length < 2) {
|
|
35
|
+
throw new ErrorDiagnostic("E_NOT_ENOUGH_ARGS", "@beam 至少需要两个端点", sourceSpan);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** 与 tie 一样,只注册不推进时间的关系排版对象 */
|
|
39
|
+
loweringEnter(ctx) {
|
|
40
|
+
const endPoints = [];
|
|
41
|
+
for (const ast of this.endPoints) {
|
|
42
|
+
// 暂时只用最后一个;理应有且仅有一个
|
|
43
|
+
const temporal = ctx.getTemporalNodes(ast).at(-1);
|
|
44
|
+
if (!temporal)
|
|
45
|
+
continue;
|
|
46
|
+
endPoints.push(temporal);
|
|
47
|
+
}
|
|
48
|
+
if (endPoints.length < 2)
|
|
49
|
+
return [];
|
|
50
|
+
ctx.addLayoutAttachment(createBeamLayoutAttachment(endPoints, true, this.sourceSpan));
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export const BeamNode = BeamFunction;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type LoweringResult, type TemporalNodeBase } from "../../lowering/types.js";
|
|
2
|
+
import type { SourceSpan } from "../../parser/types.js";
|
|
3
|
+
import type { LayoutAttachment } from "../../layout/types.js";
|
|
4
|
+
/** 显式和自动分组共享同一个几何对象 */
|
|
5
|
+
export declare function createBeamLayoutAttachment(endPoints: TemporalNodeBase[], explicit?: boolean, sourceSpan?: SourceSpan): LayoutAttachment;
|
|
6
|
+
/** 显式 beam 必须严格连接最终同轨同谱面行中的相邻可见主体 */
|
|
7
|
+
export declare function validateExplicitBeamAttachments(result: LoweringResult): void;
|
|
8
|
+
/** 自动分组在 loweringAugment 当下读取已经注册的显式 beam 端点 */
|
|
9
|
+
export declare function collectExplicitBeamEndpoints(attachments: readonly LayoutAttachment[]): Set<TemporalNodeBase>;
|