jpfun 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/converter/midi/convert.d.ts +3 -1
- package/dist/converter/midi/convert.js +78 -66
- package/dist/converter/musicxml/features.d.ts +19 -5
- package/dist/converter/musicxml/features.js +46 -8
- package/dist/converter/musicxml/index.js +142 -23
- package/dist/converter/musicxml/model.d.ts +15 -0
- package/dist/diagnostic.js +1 -1
- package/dist/functions/arpeggio/index.js +2 -5
- package/dist/functions/arpeggio/shape.d.ts +1 -1
- package/dist/functions/arpeggio/shape.js +27 -21
- package/dist/functions/bar/index.js +37 -36
- package/dist/functions/beam/auto.js +16 -13
- package/dist/functions/beam/layout.js +15 -23
- package/dist/functions/br/index.js +3 -4
- package/dist/functions/default.js +8 -4
- package/dist/functions/gliss/index.d.ts +2 -0
- package/dist/functions/gliss/index.js +180 -0
- package/dist/functions/grace/index.js +3 -6
- package/dist/functions/head/index.js +40 -59
- package/dist/functions/meter/index.js +19 -18
- package/dist/functions/note/accidentals.js +10 -16
- package/dist/functions/note/index.js +13 -3
- package/dist/functions/ottava/index.d.ts +2 -0
- package/dist/functions/ottava/index.js +217 -0
- package/dist/functions/page/index.js +3 -3
- package/dist/functions/pagebreak/index.d.ts +2 -0
- package/dist/functions/pagebreak/index.js +36 -0
- package/dist/functions/rest/index.js +12 -14
- package/dist/functions/slide/index.d.ts +2 -0
- package/dist/functions/slide/index.js +132 -0
- package/dist/functions/stack/index.js +8 -15
- package/dist/functions/symbol/index.d.ts +9 -0
- package/dist/functions/symbol/index.js +41 -12
- package/dist/functions/symbol/ornament.js +14 -19
- package/dist/functions/symbol/shape.js +4 -1
- package/dist/functions/symbol/symbols/fine.js +1 -1
- package/dist/functions/symbol/symbols/tr.js +46 -1
- package/dist/functions/temporal.d.ts +16 -6
- package/dist/functions/temporal.js +16 -6
- package/dist/functions/tie/index.js +35 -25
- package/dist/functions/up/index.js +5 -7
- package/dist/functions/voice/connections.js +7 -5
- package/dist/functions/voice/index.js +2 -1
- package/dist/functions/volta/index.js +41 -21
- package/dist/jpfun.core.min.js +44 -68
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +53 -77
- package/dist/layout/engine.d.ts +1 -0
- package/dist/layout/engine.js +42 -11
- package/dist/layout/page.d.ts +3 -2
- package/dist/layout/page.js +6 -5
- package/dist/layout/types.d.ts +4 -0
- package/dist/lowering/loweringContext.js +7 -167
- package/dist/parser/parserContext.d.ts +2 -0
- package/dist/parser/parserContext.js +27 -6
- package/dist/playback/compile.js +11 -2
- package/dist/playback/types.d.ts +4 -2
- package/dist/render/svg.js +1 -0
- package/dist/render/text.d.ts +1 -1
- package/dist/render/text.js +3 -2
- package/dist/render/types.d.ts +1 -0
- package/grammar.md +30 -9
- package/package.json +1 -1
|
@@ -12,9 +12,23 @@ import { mordentSymbol } from "./symbols/mordent.js";
|
|
|
12
12
|
import { prallSymbol } from "./symbols/prall.js";
|
|
13
13
|
import { segnoSymbol } from "./symbols/segno.js";
|
|
14
14
|
import { trSymbol } from "./symbols/tr.js";
|
|
15
|
-
const symbolTable = new Map(
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const symbolTable = new Map();
|
|
16
|
+
const symbolPatterns = [];
|
|
17
|
+
for (const definition of [trSymbol, fermataSymbol, prallSymbol, mordentSymbol, accentSymbol,
|
|
18
|
+
...dynamicSymbols, dcSymbol, dsSymbol, segnoSymbol, fineSymbol]) {
|
|
19
|
+
const name = definition.name.toLowerCase();
|
|
20
|
+
if (symbolTable.has(name))
|
|
21
|
+
throw new Error(`Duplicate symbol name: ${definition.name}`);
|
|
22
|
+
symbolTable.set(name, definition);
|
|
23
|
+
const pattern = definition.pattern;
|
|
24
|
+
if (!pattern)
|
|
25
|
+
continue;
|
|
26
|
+
const { regex } = pattern;
|
|
27
|
+
if (regex.global || regex.sticky)
|
|
28
|
+
throw new Error(`Symbol pattern must not use g or y flags: ${name}`);
|
|
29
|
+
symbolPatterns.push(regex.ignoreCase ? pattern
|
|
30
|
+
: { ...pattern, regex: new RegExp(regex.source, `${regex.flags}i`) });
|
|
31
|
+
}
|
|
18
32
|
class SymbolFunction extends ASTFunctionNode {
|
|
19
33
|
static def = {
|
|
20
34
|
name: ["symbol", "$"],
|
|
@@ -23,7 +37,7 @@ class SymbolFunction extends ASTFunctionNode {
|
|
|
23
37
|
~~~jpfun
|
|
24
38
|
1 ^ @symbol(p, size=0.5em)
|
|
25
39
|
~~~
|
|
26
|
-
**简写**:\`$name\`,如 \`1 ^ $p
|
|
40
|
+
**简写**:\`$name\`,如 \`1 ^ $p\`。名称读取到下一个空白或当前内容区间末尾,连续的标点也属于名称。
|
|
27
41
|
|
|
28
42
|
**内置记号**
|
|
29
43
|
${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.description}`).join("\n")}`,
|
|
@@ -44,12 +58,12 @@ ${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.descriptio
|
|
|
44
58
|
],
|
|
45
59
|
};
|
|
46
60
|
static deSugarAtom(source, start, end) {
|
|
47
|
-
if (source[start] !== "$"
|
|
61
|
+
if (source[start] !== "$")
|
|
62
|
+
return null;
|
|
63
|
+
const name = /^\S+/.exec(source.slice(start + 1, end))?.[0];
|
|
64
|
+
if (!name)
|
|
48
65
|
return null;
|
|
49
|
-
|
|
50
|
-
while (next < end && /[A-Za-z0-9_-]/.test(source[next]))
|
|
51
|
-
next++;
|
|
52
|
-
const name = source.slice(start + 1, next);
|
|
66
|
+
const next = start + 1 + name.length;
|
|
53
67
|
const node = {
|
|
54
68
|
kind: "call",
|
|
55
69
|
typed: true,
|
|
@@ -66,7 +80,20 @@ ${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.descriptio
|
|
|
66
80
|
super(sourceSpan, parent);
|
|
67
81
|
const [name, size] = this.getArgValue(args, ctx);
|
|
68
82
|
this.size = ctx.length2px(size);
|
|
69
|
-
const
|
|
83
|
+
const normalized = name.toLowerCase();
|
|
84
|
+
let definition = symbolTable.get(normalized);
|
|
85
|
+
if (!definition) {
|
|
86
|
+
let matched;
|
|
87
|
+
for (const pattern of symbolPatterns) {
|
|
88
|
+
const match = pattern.regex.exec(normalized);
|
|
89
|
+
if (!match || match.index !== 0 || match[0] !== normalized)
|
|
90
|
+
continue;
|
|
91
|
+
if (matched)
|
|
92
|
+
throw new Error(`Ambiguous symbol patterns: ${normalized}`);
|
|
93
|
+
matched = { pattern, match };
|
|
94
|
+
}
|
|
95
|
+
definition = matched?.pattern.resolve(matched.match);
|
|
96
|
+
}
|
|
70
97
|
if (!definition) {
|
|
71
98
|
const message = `@symbol: 未知符号: $${name}`;
|
|
72
99
|
if (ctx.variables.strict)
|
|
@@ -107,7 +134,7 @@ class SymbolTemporal extends TemporalNodeBase {
|
|
|
107
134
|
// 文字记号的字号也是 size × weight,所以与图形记号视觉等高
|
|
108
135
|
this.label = text && {
|
|
109
136
|
content: text.content,
|
|
110
|
-
style: { fontSize: this.height, fontFamily: text.fontFamily },
|
|
137
|
+
style: { fontSize: this.height, fontFamily: text.fontFamily, fontStyle: text.fontStyle },
|
|
111
138
|
};
|
|
112
139
|
this.initLayoutBox();
|
|
113
140
|
}
|
|
@@ -123,7 +150,9 @@ class SymbolTemporal extends TemporalNodeBase {
|
|
|
123
150
|
this.box.w = this.bounds.w * this.scale;
|
|
124
151
|
this.box.h = this.height + (this.ast.definition.paddingBottom ?? 0) * this.scale;
|
|
125
152
|
}
|
|
126
|
-
this.box.anchor = this.
|
|
153
|
+
this.box.anchor = !this.label && this.ast.definition.anchor !== undefined
|
|
154
|
+
? (this.ast.definition.anchor - this.bounds.x) * this.scale
|
|
155
|
+
: this.box.w / 2;
|
|
127
156
|
this.box.visualAxis = this.box.h / 2;
|
|
128
157
|
}
|
|
129
158
|
paint(painter) {
|
|
@@ -22,13 +22,21 @@ export function ornament(pattern) {
|
|
|
22
22
|
continue;
|
|
23
23
|
}
|
|
24
24
|
const duration = note.end.clone().sub(note.start);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
let count = pattern.body.length;
|
|
26
|
+
if (pattern.repeat) {
|
|
27
|
+
// 此处读取最终速度,结构延长和其他声部的速度效果都已计入。
|
|
28
|
+
const bpm = context.stateAt(note.start).effectiveBpm;
|
|
29
|
+
// 名义密度与速度无关(秒数里的 bpm 与频率里的 bpm 恰好抵消),只在 bpm 越界时按比例修正
|
|
30
|
+
const ratio = Math.min(MAX_BPM, Math.max(MIN_BPM, bpm)) / bpm;
|
|
31
|
+
// 预算按完整音型计算,避免在达到子音上限时留下半个音型。
|
|
32
|
+
const limit = Math.floor(MAX_SUBNOTES / pattern.body.length);
|
|
33
|
+
const cycles = Math.min(limit, Math.max(1, Math.round(duration.toNumber() * NOMINAL_RATE * ratio / pattern.body.length)));
|
|
34
|
+
count *= cycles;
|
|
35
|
+
}
|
|
36
|
+
const step = duration.div(count);
|
|
29
37
|
// 用 Fraction 等分最终区间,保证首尾边界准确且不会积累浮点时间误差。
|
|
30
|
-
for (let index = 0; index <
|
|
31
|
-
const offset =
|
|
38
|
+
for (let index = 0; index < count; index++) {
|
|
39
|
+
const offset = pattern.body[index % pattern.body.length];
|
|
32
40
|
const start = step.clone().mul(index).add(note.start);
|
|
33
41
|
result.push({
|
|
34
42
|
...note,
|
|
@@ -44,16 +52,3 @@ export function ornament(pattern) {
|
|
|
44
52
|
return result;
|
|
45
53
|
};
|
|
46
54
|
}
|
|
47
|
-
/** 按最终速度计算完整音型的重复次数,并限制总子音数 */
|
|
48
|
-
function repeatToFill(body, duration, bpm) {
|
|
49
|
-
// 名义密度与速度无关(秒数里的 bpm 与频率里的 bpm 恰好抵消),只在 bpm 越界时按比例修正
|
|
50
|
-
const ratio = Math.min(MAX_BPM, Math.max(MIN_BPM, bpm)) / bpm;
|
|
51
|
-
// 预算按完整音型计算,避免在达到子音上限时留下半个音型。
|
|
52
|
-
const limit = Math.floor(MAX_SUBNOTES / body.length);
|
|
53
|
-
const cycles = Math.min(limit, Math.max(1, Math.round(duration * NOMINAL_RATE * ratio / body.length)));
|
|
54
|
-
const result = [];
|
|
55
|
-
// 先得到完整音级序列,调用方再按实际子音总数统一分配宿主时值。
|
|
56
|
-
for (let i = 0; i < cycles; i++)
|
|
57
|
-
result.push(...body);
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
@@ -6,6 +6,8 @@
|
|
|
6
6
|
* 否则墨迹会偏出框——符号原点不一定落在墨迹左上角。
|
|
7
7
|
*/
|
|
8
8
|
import { pathBounds } from "../../layout/path.js";
|
|
9
|
+
// 固定字形路径不可原地修改;共享路径的重复片段只求一次曲线极值
|
|
10
|
+
const pathBoundsCache = new WeakMap();
|
|
9
11
|
/** 全体图元的并集;宽高钳到正数,否则纯横线或纯竖线的符号会让缩放比例除零 */
|
|
10
12
|
export function symbolBounds(shapes) {
|
|
11
13
|
let left = Infinity;
|
|
@@ -25,7 +27,8 @@ export function symbolBounds(shapes) {
|
|
|
25
27
|
merge(cx + dx - r, cy - r, 2 * r, 2 * r);
|
|
26
28
|
}
|
|
27
29
|
if (shape.path?.length) {
|
|
28
|
-
const rect = pathBounds(shape.path);
|
|
30
|
+
const rect = pathBoundsCache.get(shape.path) ?? pathBounds(shape.path);
|
|
31
|
+
pathBoundsCache.set(shape.path, rect);
|
|
29
32
|
merge(rect.x + dx, rect.y, rect.w, rect.h);
|
|
30
33
|
}
|
|
31
34
|
}
|
|
@@ -3,7 +3,7 @@ export const fineSymbol = {
|
|
|
3
3
|
name: "fine",
|
|
4
4
|
description: "终止记号:回跳之后经过这里就结束乐曲",
|
|
5
5
|
weight: 1.4,
|
|
6
|
-
text: { content: "Fine.", fontFamily: '"Times New Roman", serif' },
|
|
6
|
+
text: { content: "Fine.", fontFamily: '"Times New Roman", serif', fontStyle: "italic" },
|
|
7
7
|
// 判据是回跳已经发生,不是“第二次经过”;否则写在 |: :| 里会被反复线提前触发
|
|
8
8
|
onVisit: (cursor, at) => {
|
|
9
9
|
const jump = cursor.seek(JUMP_MARK, at, 1);
|
|
@@ -1,7 +1,18 @@
|
|
|
1
|
+
import { symbolBounds } from "../shape.js";
|
|
1
2
|
import { ornament } from "../ornament.js";
|
|
2
3
|
export const trSymbol = {
|
|
3
4
|
name: "tr",
|
|
4
|
-
description:
|
|
5
|
+
description: '颤音:本音与调内上方二度交替。可写为 `$tr++`,每个紧接的 `+` 添加一截波浪尾巴',
|
|
6
|
+
pattern: {
|
|
7
|
+
regex: /^tr(\++)/,
|
|
8
|
+
resolve: match => {
|
|
9
|
+
const shapes = [...trSymbol.shapes];
|
|
10
|
+
for (let i = 0; i < match[1].length; i++) {
|
|
11
|
+
shapes.push({ ...tailShape, dx: 14.11212 + i * 5.95219 });
|
|
12
|
+
}
|
|
13
|
+
return { ...trSymbol, name: match[0], anchor: trAnchor, shapes };
|
|
14
|
+
},
|
|
15
|
+
},
|
|
5
16
|
shapes: [
|
|
6
17
|
{
|
|
7
18
|
path: [
|
|
@@ -69,3 +80,37 @@ export const trSymbol = {
|
|
|
69
80
|
],
|
|
70
81
|
emitPlayback: emitter => emitter.affectFollowing(ornament({ body: [0, 1], repeat: true })),
|
|
71
82
|
};
|
|
83
|
+
const trBounds = symbolBounds(trSymbol.shapes);
|
|
84
|
+
const trAnchor = trBounds.x + trBounds.w / 2;
|
|
85
|
+
// MuseScore 导出的 TrillSegment,按 tr 字形高度归一化,保留片段之间的重叠。
|
|
86
|
+
const tailShape = {
|
|
87
|
+
path: [
|
|
88
|
+
{ op: "M", x: 7.03619, y: 4.89762 },
|
|
89
|
+
{ op: "C", cx1: 7.03619, cy1: 4.89762, cx2: 6.87351, cy2: 4.70878, x: 6.65687, y: 4.5183 },
|
|
90
|
+
{ op: "C", cx1: 6.57594, cy1: 4.43737, cx2: 6.46721, cy2: 4.41039, x: 6.38546, y: 4.41039 },
|
|
91
|
+
{ op: "C", cx1: 6.30535, cy1: 4.41039, cx2: 6.25058, cy2: 4.43737, x: 6.19662, y: 4.49132 },
|
|
92
|
+
{ op: "L", x: 5.89905, y: 4.78889 },
|
|
93
|
+
{ op: "C", cx1: 5.8451, cy1: 4.84367, cx2: 5.79033, cy2: 4.87064, x: 5.73719, y: 4.87064 },
|
|
94
|
+
{ op: "C", cx1: 5.62846, cy1: 4.87064, cx2: 5.57451, cy2: 4.78889, x: 5.57451, y: 4.78889 },
|
|
95
|
+
{ op: "C", cx1: 5.57451, cy1: 4.78889, cx2: 3.84223, cy2: 3.30105, x: 3.57164, y: 3.05662 },
|
|
96
|
+
{ op: "C", cx1: 3.27407, cy1: 2.8408, cx2: 2.9765, cy2: 2.75905, x: 2.70591, y: 2.75905 },
|
|
97
|
+
{ op: "C", cx1: 2.13857, cy1: 2.75905, cx2: 1.62355, cy2: 3.08523, x: 1.40773, y: 3.32721 },
|
|
98
|
+
{ op: "L", x: 0.10873, y: 4.62703 },
|
|
99
|
+
{ op: "C", cx1: 0.02698, cy1: 4.70878, cx2: 0, cy2: 4.76192, x: 0, y: 4.84367 },
|
|
100
|
+
{ op: "C", cx1: 0, cy1: 4.97773, cx2: 0.10873, cy2: 5.08646, x: 0.10873, y: 5.08646 },
|
|
101
|
+
{ op: "C", cx1: 0.10873, cy1: 5.08646, cx2: 0.27141, cy2: 5.2753, x: 0.48723, y: 5.46578 },
|
|
102
|
+
{ op: "C", cx1: 0.56898, cy1: 5.54671, cx2: 0.64909, cy2: 5.57451, x: 0.73084, y: 5.57451 },
|
|
103
|
+
{ op: "C", cx1: 0.81259, cy1: 5.57451, cx2: 0.86655, cy2: 5.54671, x: 0.91968, y: 5.49276 },
|
|
104
|
+
{ op: "L", x: 1.21725, y: 5.19519 },
|
|
105
|
+
{ op: "C", cx1: 1.27284, cy1: 5.14123, cx2: 1.32598, cy2: 5.11344, x: 1.37993, y: 5.11344 },
|
|
106
|
+
{ op: "C", cx1: 1.48866, cy1: 5.11344, cx2: 1.57041, cy2: 5.19519, x: 1.57041, y: 5.19519 },
|
|
107
|
+
{ op: "C", cx1: 1.57041, cy1: 5.19519, cx2: 3.30105, cy2: 6.65605, x: 3.57164, y: 6.89885 },
|
|
108
|
+
{ op: "C", cx1: 3.86921, cy1: 7.1163, cx2: 4.16678, cy2: 7.19805, x: 4.43819, y: 7.19805 },
|
|
109
|
+
{ op: "C", cx1: 5.00635, cy1: 7.19805, cx2: 5.51973, cy2: 6.87187, x: 5.73719, y: 6.62989 },
|
|
110
|
+
{ op: "L", x: 7.03619, y: 5.33008 },
|
|
111
|
+
{ op: "C", cx1: 7.1163, cy1: 5.24833, cx2: 7.1441, cy2: 5.16821, x: 7.1441, y: 5.11344 },
|
|
112
|
+
{ op: "C", cx1: 7.1441, cy1: 4.97773, cx2: 7.03619, cy2: 4.89762, x: 7.03619, y: 4.89762 },
|
|
113
|
+
{ op: "Z" },
|
|
114
|
+
],
|
|
115
|
+
style: { fill: "#000" },
|
|
116
|
+
};
|
|
@@ -23,17 +23,27 @@ export interface TimeState {
|
|
|
23
23
|
program: number;
|
|
24
24
|
/** 当前小节时长(QN);尚无显式拍号或散板时为 undefined */
|
|
25
25
|
meter?: Fraction;
|
|
26
|
+
/** 当前执行的全局事件;复合体内部回调共用该位置 */
|
|
27
|
+
currentEvent?: TemporalNodeBase;
|
|
26
28
|
[key: string]: any;
|
|
27
29
|
}
|
|
28
30
|
export declare const DEFAULT_BPM = 120;
|
|
29
31
|
export declare const DEFAULT_VELOCITY = 80;
|
|
30
32
|
export declare const DEFAULT_TONALITY = "C4";
|
|
31
33
|
export declare const DEFAULT_PROGRAM = 0;
|
|
34
|
+
/** 每个事件的可选语义声明;核心字段与函数私有的 @键分开 */
|
|
35
|
+
export interface TemporalAddon {
|
|
36
|
+
/** 请求在当前时间列之前换几行,缺省为 0 */
|
|
37
|
+
breakBefore?: number;
|
|
38
|
+
/** 在所属行之前强制换页;配合 breakBefore 使用,布尔值控制上一页的余白分配 */
|
|
39
|
+
pageBreakBefore?: boolean;
|
|
40
|
+
[key: `@${string}`]: unknown;
|
|
41
|
+
}
|
|
32
42
|
/**
|
|
33
43
|
* 事件的合并组:同一时刻 mergeKey 相等的事件归并成一列
|
|
34
44
|
*
|
|
35
45
|
* 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
|
|
36
|
-
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1
|
|
46
|
+
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 换行/换页,-2 声部名
|
|
37
47
|
*/
|
|
38
48
|
/** 时间对齐点(小节线);必须是最小值,最先出队 */
|
|
39
49
|
export declare const ANCHOR_KEY: number;
|
|
@@ -50,12 +60,12 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
50
60
|
track: Track;
|
|
51
61
|
ast: ASTNodeBase;
|
|
52
62
|
order: number;
|
|
53
|
-
addon?:
|
|
63
|
+
addon?: TemporalAddon;
|
|
54
64
|
/**
|
|
55
65
|
* 时间列合并组:同一时刻 mergeKey 相等的事件归并成一列
|
|
56
66
|
*
|
|
57
67
|
* 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
|
|
58
|
-
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1
|
|
68
|
+
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 换行/换页,-2 声部名
|
|
59
69
|
*/
|
|
60
70
|
mergeKey: number;
|
|
61
71
|
/**
|
|
@@ -65,8 +75,6 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
65
75
|
* 按全局事件工作的消费者可以沿链上溯到宿主。
|
|
66
76
|
*/
|
|
67
77
|
foldedInto?: TemporalNodeBase;
|
|
68
|
-
/** 请求在自己所在时间列之前换几行,默认 0 */
|
|
69
|
-
breakBefore: number;
|
|
70
78
|
/** 由 LoweringContext 在列归并后固化的谱面行号 */
|
|
71
79
|
layoutLine: number;
|
|
72
80
|
/** 不可见状态事件保持 undefined */
|
|
@@ -82,7 +90,7 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
82
90
|
* 装饰与关系函数使用的命名局部坐标,相对 box 左上角,每轮布局重建。
|
|
83
91
|
*
|
|
84
92
|
* 当前内置端口:
|
|
85
|
-
* - body.left / body.right:主体核心左右边界,供 div、tuplet、volta 使用;缺省为盒左右边界。
|
|
93
|
+
* - body.left / body.right:主体核心左右边界,供 div、tuplet、volta、ottava 使用;缺省为盒左右边界。
|
|
86
94
|
* - decoration.below:下方装饰排列起点(只读 y);缺省为正文 box.h。
|
|
87
95
|
* - shoulder:倚音的纵向定位基准(只读 y);音符提供数字顶边,缺省为盒顶。
|
|
88
96
|
* - tie.top:连音线端点;缺省为 anchor 与内容上沿,up 提供最上层成员的端点。
|
|
@@ -147,6 +155,8 @@ export declare class TemporalNodeBase implements TimeLineEvent {
|
|
|
147
155
|
/** 只执行绘制,不允许在这里重新测量或改变 LayoutBox */
|
|
148
156
|
paint(_painter: Painter): void;
|
|
149
157
|
}
|
|
158
|
+
/** 复合体下放函数语义时,核心声明仍留在全局事件上 */
|
|
159
|
+
export declare function transferFunctionAddons(source: TemporalNodeBase, target: TemporalNodeBase): void;
|
|
150
160
|
/** 可以进入视觉布局的 Temporal,同时满足 decoration 所需的 LayoutHost 协议 */
|
|
151
161
|
export type VisualTemporalNode = TemporalNodeBase & LayoutHost;
|
|
152
162
|
export declare function isVisualTemporalNode(node: TemporalNodeBase): node is VisualTemporalNode;
|
|
@@ -7,7 +7,7 @@ export const DEFAULT_PROGRAM = 0;
|
|
|
7
7
|
* 事件的合并组:同一时刻 mergeKey 相等的事件归并成一列
|
|
8
8
|
*
|
|
9
9
|
* 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
|
|
10
|
-
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1
|
|
10
|
+
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 换行/换页,-2 声部名
|
|
11
11
|
*/
|
|
12
12
|
/** 时间对齐点(小节线);必须是最小值,最先出队 */
|
|
13
13
|
export const ANCHOR_KEY = -Infinity;
|
|
@@ -26,12 +26,12 @@ export class TemporalNodeBase {
|
|
|
26
26
|
track; // 所在的纵向音轨;同轨判断使用引用相等
|
|
27
27
|
ast; // 对应的 AST 节点
|
|
28
28
|
order; // 事件创建序号,作为id。可用于区分同时发生的父子、排序同一时刻发生的事件
|
|
29
|
-
addon; //
|
|
29
|
+
addon; // 仅在存在语义声明时创建,比如换行或 div 和 dot 的数目
|
|
30
30
|
/**
|
|
31
31
|
* 时间列合并组:同一时刻 mergeKey 相等的事件归并成一列
|
|
32
32
|
*
|
|
33
33
|
* 缺省取事件自身的 order,因而互不相等、各自独占一列;需要合并的显式取相同的值。
|
|
34
|
-
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1
|
|
34
|
+
* 数值同时决定同时刻的列先后,越小越靠左。已占用的共享常量:-1 换行/换页,-2 声部名
|
|
35
35
|
*/
|
|
36
36
|
mergeKey;
|
|
37
37
|
/**
|
|
@@ -41,8 +41,6 @@ export class TemporalNodeBase {
|
|
|
41
41
|
* 按全局事件工作的消费者可以沿链上溯到宿主。
|
|
42
42
|
*/
|
|
43
43
|
foldedInto;
|
|
44
|
-
/** 请求在自己所在时间列之前换几行,默认 0 */
|
|
45
|
-
breakBefore = 0;
|
|
46
44
|
/** 由 LoweringContext 在列归并后固化的谱面行号 */
|
|
47
45
|
layoutLine = 0;
|
|
48
46
|
/** 不可见状态事件保持 undefined */
|
|
@@ -56,7 +54,7 @@ export class TemporalNodeBase {
|
|
|
56
54
|
* 装饰与关系函数使用的命名局部坐标,相对 box 左上角,每轮布局重建。
|
|
57
55
|
*
|
|
58
56
|
* 当前内置端口:
|
|
59
|
-
* - body.left / body.right:主体核心左右边界,供 div、tuplet、volta 使用;缺省为盒左右边界。
|
|
57
|
+
* - body.left / body.right:主体核心左右边界,供 div、tuplet、volta、ottava 使用;缺省为盒左右边界。
|
|
60
58
|
* - decoration.below:下方装饰排列起点(只读 y);缺省为正文 box.h。
|
|
61
59
|
* - shoulder:倚音的纵向定位基准(只读 y);音符提供数字顶边,缺省为盒顶。
|
|
62
60
|
* - tie.top:连音线端点;缺省为 anchor 与内容上沿,up 提供最上层成员的端点。
|
|
@@ -106,6 +104,18 @@ export class TemporalNodeBase {
|
|
|
106
104
|
/** 只执行绘制,不允许在这里重新测量或改变 LayoutBox */
|
|
107
105
|
paint(_painter) { }
|
|
108
106
|
}
|
|
107
|
+
/** 复合体下放函数语义时,核心声明仍留在全局事件上 */
|
|
108
|
+
export function transferFunctionAddons(source, target) {
|
|
109
|
+
const addon = source.addon;
|
|
110
|
+
if (!addon)
|
|
111
|
+
return;
|
|
112
|
+
for (const key of Object.keys(addon).filter((key) => key.startsWith("@"))) {
|
|
113
|
+
(target.addon ??= {})[key] = addon[key];
|
|
114
|
+
delete addon[key];
|
|
115
|
+
}
|
|
116
|
+
if (!Object.keys(addon).length)
|
|
117
|
+
source.addon = undefined;
|
|
118
|
+
}
|
|
109
119
|
export function isVisualTemporalNode(node) {
|
|
110
120
|
return node.box !== undefined;
|
|
111
121
|
}
|
|
@@ -7,6 +7,10 @@ import { getLayoutBounds } from "../../layout/engine.js";
|
|
|
7
7
|
const CUBIC_LIFT = 4 / 3;
|
|
8
8
|
/** 弧带中部最大厚度与字号之比 */
|
|
9
9
|
const THICKNESS_RATIO = 0.09;
|
|
10
|
+
/** 参考弧线的跨度约为弧高的 5 倍;在 2.5em 处平滑达到 0.5em 上限 */
|
|
11
|
+
const AUTO_SPAN_RATIO = 2.5;
|
|
12
|
+
const AUTO_HEIGHT_RATIO = 0.5;
|
|
13
|
+
const CROSS_LINE_HEIGHT_RATIO = 0.45;
|
|
10
14
|
class TieFunction extends ASTFunctionNode {
|
|
11
15
|
static def = {
|
|
12
16
|
name: ["tie"],
|
|
@@ -16,21 +20,22 @@ class TieFunction extends ASTFunctionNode {
|
|
|
16
20
|
1@a 2 3@b @tie(a, b, height=0.5em)
|
|
17
21
|
~~~
|
|
18
22
|
- **位置参数**:端点标签,可传多个,按顺序连接;省略时查找最近的可标记对象,包括增时线
|
|
19
|
-
- **height**:仅命名的弧线高度参数,默认 \`0.5em
|
|
23
|
+
- **height**:仅命名的弧线高度参数,默认 \`0\`(自动);非正数按水平跨度自动调整,在 \`2.5em\` 跨度处平滑达到 \`0.5em\` 上限,正数指定固定弧高。em 相对调用处字号;可用 \`@set(tie.height=0.8em)\` 设置局部默认值,显式传参优先
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
同行端点用一条弧线连接,自动高度按水平跨度计算;跨行时自动拆成多段,自动模式下各段统一使用 \`0.45em\` 高度。同轨同音且时间连续的音段建立播放连接,各段保留自己的装饰,例如 \`1 ^ $tr 1 @tie()\` 的后一音段不继承颤音。涉及增时线的连接仅绘制,不合并播放音符。`,
|
|
22
26
|
allowExtraArgs: true,
|
|
23
27
|
extraArgType: "label",
|
|
24
28
|
args: [{
|
|
25
29
|
name: "height",
|
|
26
30
|
namedOnly: true,
|
|
27
|
-
description: "弧线高度,支持 px/em;em
|
|
31
|
+
description: "弧线高度,支持 px/em;em 相对调用处字号,非正数自动调整,最高 0.5em",
|
|
28
32
|
type: "length",
|
|
29
|
-
default: { value: 0
|
|
33
|
+
default: { value: 0, unit: "em" },
|
|
30
34
|
}]
|
|
31
35
|
};
|
|
32
36
|
endPoints = [];
|
|
33
37
|
height;
|
|
38
|
+
fontSize;
|
|
34
39
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
35
40
|
super(sourceSpan, parent);
|
|
36
41
|
// 命名选项统一解析默认值,位置参数仍全部作为端点
|
|
@@ -55,6 +60,7 @@ class TieFunction extends ASTFunctionNode {
|
|
|
55
60
|
if (this.endPoints.length < 2)
|
|
56
61
|
throw new ErrorDiagnostic("E_NOT_ENOUGH_ARGS", "@tie 连音线需要至少两个端点", sourceSpan);
|
|
57
62
|
this.height = Math.max(0, ctx.length2px(height));
|
|
63
|
+
this.fontSize = ctx.variables.fontsize;
|
|
58
64
|
}
|
|
59
65
|
/**
|
|
60
66
|
* tie 不推进时间
|
|
@@ -72,7 +78,7 @@ class TieFunction extends ASTFunctionNode {
|
|
|
72
78
|
ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("tie", this.sourceSpan));
|
|
73
79
|
return [];
|
|
74
80
|
}
|
|
75
|
-
ctx.addAttachment(new TieLayoutAttachment(endPoints, this.height, this.sourceSpan));
|
|
81
|
+
ctx.addAttachment(new TieLayoutAttachment(endPoints, this.height, this.fontSize, this.sourceSpan));
|
|
76
82
|
return [];
|
|
77
83
|
}
|
|
78
84
|
toString(source) {
|
|
@@ -88,11 +94,13 @@ class TieLayoutAttachment {
|
|
|
88
94
|
sourceSpan;
|
|
89
95
|
endPoints;
|
|
90
96
|
height;
|
|
97
|
+
fontSize;
|
|
91
98
|
/** 弧带中部厚度;两端收尖到 0。端点字号不随布局变化,可以一次算定 */
|
|
92
99
|
thickness;
|
|
93
|
-
constructor(endPoints, height, sourceSpan) {
|
|
100
|
+
constructor(endPoints, height, fontSize, sourceSpan) {
|
|
94
101
|
this.endPoints = endPoints;
|
|
95
102
|
this.height = height;
|
|
103
|
+
this.fontSize = fontSize;
|
|
96
104
|
this.sourceSpan = sourceSpan;
|
|
97
105
|
const size = endPoints.reduce((current, endpoint) => Math.max(current, endpoint.ast.size), 0);
|
|
98
106
|
this.thickness = Math.max(1.2, size * THICKNESS_RATIO);
|
|
@@ -176,24 +184,29 @@ class TieLayoutAttachment {
|
|
|
176
184
|
const start = this.tiePort(startNode);
|
|
177
185
|
const end = this.tiePort(endNode);
|
|
178
186
|
if (startNode.layoutLine === endNode.layoutLine) {
|
|
179
|
-
const startTop =
|
|
180
|
-
|
|
181
|
-
const endTop = context.getVisualAxis(endNode.layoutLine, endNode.track)
|
|
182
|
-
+ (context.getHostExtent(endNode.layoutLine, endNode.track)?.top ?? 0);
|
|
187
|
+
const startTop = this.hostTop(context, startNode.layoutLine, startNode.track);
|
|
188
|
+
const endTop = this.hostTop(context, endNode.layoutLine, endNode.track);
|
|
183
189
|
segments.push(this.arcSegment(start, end, startNode.layoutLine, startTop <= endTop ? startNode.track : endNode.track));
|
|
184
190
|
continue;
|
|
185
191
|
}
|
|
192
|
+
const height = this.height > 0 ? this.height : this.fontSize * CROSS_LINE_HEIGHT_RATIO;
|
|
186
193
|
// 首行先起弧,再沿水平段延伸到当前谱面行最右侧
|
|
187
|
-
segments.push(this.openingSegment(start, this.lineRightX(context, startNode.layoutLine),
|
|
194
|
+
segments.push(this.openingSegment(start, this.lineRightX(context, startNode.layoutLine), height, startNode.layoutLine, startNode.track));
|
|
188
195
|
// 中间行可能没有任何可见对象,仍然需要逐行补一段水平线
|
|
189
196
|
for (let line = startNode.layoutLine + 1; line < endNode.layoutLine; line++) {
|
|
190
|
-
segments.push(this.horizontalSegment(this.lineEntryX(context, line, startNode.track), this.lineRightX(context, line), this.
|
|
197
|
+
segments.push(this.horizontalSegment(this.lineEntryX(context, line, startNode.track), this.lineRightX(context, line), this.hostTop(context, line, startNode.track) - height, line, startNode.track));
|
|
191
198
|
}
|
|
192
199
|
// 跳过声部名、大括号等位于端点列之前的系统前缀,再落弧到终点
|
|
193
|
-
segments.push(this.closingSegment(this.lineEntryX(context, endNode.layoutLine, endNode.track), end,
|
|
200
|
+
segments.push(this.closingSegment(this.lineEntryX(context, endNode.layoutLine, endNode.track), end, height, endNode.layoutLine, endNode.track));
|
|
194
201
|
}
|
|
195
202
|
return segments;
|
|
196
203
|
}
|
|
204
|
+
resolveHeight(span) {
|
|
205
|
+
if (this.height > 0)
|
|
206
|
+
return this.height;
|
|
207
|
+
const ratio = Math.min(1, span / (this.fontSize * AUTO_SPAN_RATIO));
|
|
208
|
+
return this.fontSize * AUTO_HEIGHT_RATIO * ratio * (2 - ratio);
|
|
209
|
+
}
|
|
197
210
|
lineEntryX(context, lineIndex, track) {
|
|
198
211
|
const line = context.lines[lineIndex];
|
|
199
212
|
const host = line.trackRuns.get(track)?.find(host => !host.T.isZero());
|
|
@@ -210,13 +223,8 @@ class TieLayoutAttachment {
|
|
|
210
223
|
}
|
|
211
224
|
return right;
|
|
212
225
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (Number.isFinite(endpointY))
|
|
216
|
-
return endpointY - this.height;
|
|
217
|
-
const axis = context.getVisualAxis(line, track);
|
|
218
|
-
const hostTop = context.getHostExtent(line, track)?.top ?? 0;
|
|
219
|
-
return axis + hostTop - this.height;
|
|
226
|
+
hostTop(context, line, track) {
|
|
227
|
+
return context.getVisualAxis(line, track) + (context.getHostExtent(line, track)?.top ?? 0);
|
|
220
228
|
}
|
|
221
229
|
/** 一条三次贝塞尔的外缘配上反向内缘,得到两端收尖、中部最厚的乐谱弧线 */
|
|
222
230
|
arcSegment(startInput, endInput, line, track) {
|
|
@@ -227,7 +235,7 @@ class TieLayoutAttachment {
|
|
|
227
235
|
const run = (end.x - start.x) * 0.25;
|
|
228
236
|
const leftX = start.x + run;
|
|
229
237
|
const rightX = end.x - run;
|
|
230
|
-
const lift = this.
|
|
238
|
+
const lift = this.resolveHeight(end.x - start.x) * CUBIC_LIFT;
|
|
231
239
|
const inner = Math.max(0, lift - this.thickness * CUBIC_LIFT);
|
|
232
240
|
const commands = [
|
|
233
241
|
{ op: "M", x: start.x, y: start.y },
|
|
@@ -238,8 +246,9 @@ class TieLayoutAttachment {
|
|
|
238
246
|
return makeSegment(commands, line, track);
|
|
239
247
|
}
|
|
240
248
|
/** 跨行首段:从端点起弧,随后水平延伸到右边界 */
|
|
241
|
-
openingSegment(start, right,
|
|
242
|
-
const
|
|
249
|
+
openingSegment(start, right, height, line, track) {
|
|
250
|
+
const plateauY = start.y - height;
|
|
251
|
+
const run = height * 1.2;
|
|
243
252
|
right = Math.max(right, start.x + run);
|
|
244
253
|
const curveX = start.x + run;
|
|
245
254
|
const controlX = start.x + run * 0.45;
|
|
@@ -255,8 +264,9 @@ class TieLayoutAttachment {
|
|
|
255
264
|
], line, track);
|
|
256
265
|
}
|
|
257
266
|
/** 跨行末段:从左边界水平进入,最后落弧到端点 */
|
|
258
|
-
closingSegment(left, end,
|
|
259
|
-
const
|
|
267
|
+
closingSegment(left, end, height, line, track) {
|
|
268
|
+
const plateauY = end.y - height;
|
|
269
|
+
const run = height * 1.2;
|
|
260
270
|
left = Math.min(left, end.x - run);
|
|
261
271
|
const curveX = end.x - run;
|
|
262
272
|
const controlX = end.x - run * 0.45;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ASTNodeBase, ASTFunctionNode, ASTTextNode, ASTLabelNode, ASTBraceNode } from "../ASTtypes.js";
|
|
2
2
|
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
-
import { ANCHOR_KEY, DEFAULT_KEY, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
|
|
3
|
+
import { ANCHOR_KEY, DEFAULT_KEY, transferFunctionAddons, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
|
|
4
4
|
import { Fraction } from "../../fraction.js";
|
|
5
5
|
import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
|
|
6
6
|
/**
|
|
@@ -60,8 +60,8 @@ class FoldFunction extends ASTFunctionNode {
|
|
|
60
60
|
for (const content of this.children) {
|
|
61
61
|
const columns = ctx.trackedEvents(content, new Fraction(), track);
|
|
62
62
|
const member = columns.length === 1 && columns[0].length === 1 ? columns[0][0] : undefined;
|
|
63
|
-
if (!member || !isVisualTemporalNode(member)) {
|
|
64
|
-
throw new ErrorDiagnostic("E_UP_INVALID_CHILD", "@up 的每个参数必须恰好产生一个可见 Temporal
|
|
63
|
+
if (!member || !isVisualTemporalNode(member) || (member.addon?.breakBefore ?? 0) > 0) {
|
|
64
|
+
throw new ErrorDiagnostic("E_UP_INVALID_CHILD", "@up 的每个参数必须恰好产生一个可见 Temporal,且不能换行或包含多声部结构", content.sourceSpan);
|
|
65
65
|
}
|
|
66
66
|
members.push(member);
|
|
67
67
|
}
|
|
@@ -318,10 +318,8 @@ class FoldTemporal extends TemporalNodeBase {
|
|
|
318
318
|
prepareLayout(context) {
|
|
319
319
|
// lowering 期间修饰挂在折叠体上(augmenter 要看到整体节奏),渲染时交给宿主:
|
|
320
320
|
// 减时线要落在它的数字与下八度点之间,而不是压在整个盒子下面
|
|
321
|
-
if (this.
|
|
322
|
-
this.members[0]
|
|
323
|
-
this.addon = void 0;
|
|
324
|
-
}
|
|
321
|
+
if (this.members[0])
|
|
322
|
+
transferFunctionAddons(this, this.members[0]);
|
|
325
323
|
const first = this.members[0];
|
|
326
324
|
if (!first) {
|
|
327
325
|
this.box.w = this.box.h = 0;
|
|
@@ -48,20 +48,22 @@ export const BRACKET_HOOK_COMMANDS = [
|
|
|
48
48
|
{ op: "Z" },
|
|
49
49
|
];
|
|
50
50
|
function invalidConnection(span) {
|
|
51
|
-
return new ErrorDiagnostic("E_VOICES_CONNECT", '@voices 的 connect 必须是 [起点-终点] 或 {
|
|
51
|
+
return new ErrorDiagnostic("E_VOICES_CONNECT", '@voices 的 connect 必须是 [起点-终点]、{起点-终点}、[编号] 或 {编号} 的组合;范围端点可省略,否则须为有效声部编号。空字符串表示只有连谱线。', span);
|
|
52
52
|
}
|
|
53
53
|
export function parseConnections(value, span) {
|
|
54
54
|
const source = value.trim();
|
|
55
55
|
// 粘连匹配必须消费整串,不能跳过非法字符
|
|
56
56
|
// 非空数字与其后空白一起可选,避免省略端点时重复回溯
|
|
57
|
-
const pattern = /\s*([\[{])\s*(?:(\d+)\s*)?-\s*(?:(\d+)\s*)
|
|
57
|
+
const pattern = /\s*([\[{])\s*(?:(?:(\d+)\s*)?-\s*(?:(\d+)\s*)?|(\d+)\s*)([\]}])\s*/y;
|
|
58
58
|
const result = [];
|
|
59
59
|
while (pattern.lastIndex < source.length) {
|
|
60
60
|
const match = pattern.exec(source);
|
|
61
|
-
if (!match || match[
|
|
61
|
+
if (!match || match[5] !== (match[1] === "[" ? "]" : "}"))
|
|
62
62
|
throw invalidConnection(span);
|
|
63
|
-
const
|
|
64
|
-
const
|
|
63
|
+
const fromText = match[4] ?? match[2];
|
|
64
|
+
const toText = match[4] ?? match[3];
|
|
65
|
+
const from = fromText ? Number(fromText) : undefined;
|
|
66
|
+
const to = toText ? Number(toText) : undefined;
|
|
65
67
|
for (const endpoint of [from, to]) {
|
|
66
68
|
if (endpoint !== undefined && (!Number.isSafeInteger(endpoint) || endpoint < 1)) {
|
|
67
69
|
throw invalidConnection(span);
|
|
@@ -463,6 +463,7 @@ class VoicesFunction extends ASTFunctionNode {
|
|
|
463
463
|
|
|
464
464
|
**视觉连接**:仅命名参数 \`connect\` 使用字符串,如 \`connect="[1-4]{5-7}"\`。
|
|
465
465
|
\`[]\` 是现有括线,\`{}\` 是弯曲大括号;编号从 1 开始,省略起点/终点表示首/末声部。
|
|
466
|
+
\`[3]\` 等价于 \`[3-3]\`,\`{3}\` 等价于 \`{3-3}\`,用于单个声部。
|
|
466
467
|
默认 \`"[-]"\` 连接全部声部;\`""\` 只保留公共细连谱线。未指定的声部仍有细线,范围可以重叠,不改变声部或播放关系。
|
|
467
468
|
用 \`@set(voices.connect="{1-2}[3-]")\` 设置后续多声部块的默认方案,显式参数写在所有声部之后。
|
|
468
469
|
|
|
@@ -493,7 +494,7 @@ N(女低): 1 2 3
|
|
|
493
494
|
namedOnly: true,
|
|
494
495
|
type: "string",
|
|
495
496
|
default: "[-]",
|
|
496
|
-
description: '视觉连接:[1-4] 为现有括线,{5-7}
|
|
497
|
+
description: '视觉连接:[1-4] 为现有括线,{5-7} 为弯曲大括号;[3]、{3} 表示单个声部,省略范围端点表示首/末声部,空字符串只保留公共连谱线。',
|
|
497
498
|
}]
|
|
498
499
|
};
|
|
499
500
|
static deSugarAtom(source, start, end) {
|