jpfun 0.7.0 → 0.8.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/Bravura-LICENSE.txt +93 -0
- package/README.md +49 -146
- package/dist/Bravura-LICENSE.txt +93 -0
- package/dist/converter/midi/convert.d.ts +0 -10
- package/dist/converter/midi/convert.js +11 -1
- package/dist/converter/musicxml/features.d.ts +15 -7
- package/dist/converter/musicxml/features.js +87 -18
- package/dist/converter/musicxml/index.js +80 -32
- package/dist/converter/musicxml/model.d.ts +9 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/diagnostic.d.ts +7 -1
- package/dist/diagnostic.js +40 -88
- package/dist/fraction.js +3 -2
- package/dist/functions/ASTtypes.d.ts +2 -18
- package/dist/functions/ASTtypes.js +0 -16
- package/dist/functions/arpeggio/index.js +15 -17
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +102 -0
- package/dist/functions/beam/auto.d.ts +1 -2
- package/dist/functions/beam/auto.js +30 -16
- package/dist/functions/beam/index.js +2 -3
- package/dist/functions/beam/layout.d.ts +1 -1
- package/dist/functions/beam/layout.js +1 -1
- package/dist/functions/br/index.d.ts +34 -2
- package/dist/functions/br/index.js +5 -4
- package/dist/functions/dash/index.js +6 -6
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +1 -0
- package/dist/functions/div/index.js +8 -4
- package/dist/functions/dot/index.js +2 -2
- package/dist/functions/dyn/index.js +49 -31
- package/dist/functions/gliss/index.js +75 -53
- package/dist/functions/grace/index.d.ts +1 -7
- package/dist/functions/grace/index.js +8 -13
- package/dist/functions/head/index.js +1 -1
- package/dist/functions/key/index.js +8 -10
- package/dist/functions/meter/index.d.ts +56 -1
- package/dist/functions/meter/index.js +12 -7
- package/dist/functions/note/index.js +5 -5
- package/dist/functions/note/noteName.d.ts +16 -0
- package/dist/functions/note/noteName.js +88 -0
- package/dist/functions/note/source-transform.js +6 -5
- package/dist/functions/ottava/index.js +24 -8
- package/dist/functions/page/index.d.ts +1 -0
- package/dist/functions/page/index.js +20 -11
- package/dist/functions/pagebreak/index.js +2 -2
- package/dist/functions/rest/index.js +3 -1
- package/dist/functions/set/index.js +4 -4
- package/dist/functions/symbol/index.d.ts +21 -2
- package/dist/functions/symbol/index.js +69 -24
- package/dist/functions/symbol/ornament.d.ts +1 -2
- package/dist/functions/symbol/shape.d.ts +6 -0
- package/dist/functions/symbol/shape.js +16 -3
- package/dist/functions/symbol/symbols/coda.d.ts +4 -0
- package/dist/functions/symbol/symbols/coda.js +56 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
- package/dist/functions/symbol/symbols/dynamics.js +18 -8
- package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
- package/dist/functions/symbol/symbols/harmonic.js +11 -0
- package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
- package/dist/functions/symbol/symbols/marcato.js +22 -0
- package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
- package/dist/functions/symbol/symbols/staccato.js +24 -0
- package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
- package/dist/functions/symbol/symbols/tenuto.js +11 -0
- package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
- package/dist/functions/symbol/symbols/tocoda.js +16 -0
- package/dist/functions/symbol/symbols/turn.d.ts +3 -0
- package/dist/functions/symbol/symbols/turn.js +50 -0
- package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
- package/dist/functions/symbol/symbols/wheel.js +36 -0
- package/dist/functions/temporal.d.ts +1 -0
- package/dist/functions/temporal.js +1 -0
- package/dist/functions/text/index.js +15 -6
- package/dist/functions/tie/index.js +30 -6
- package/dist/functions/tremolo/index.d.ts +2 -0
- package/dist/functions/tremolo/index.js +319 -0
- package/dist/functions/tuplet/index.js +4 -8
- package/dist/functions/up/index.js +2 -2
- package/dist/functions/voice/index.js +17 -10
- package/dist/jpfun.core.min.js +57 -49
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +62 -54
- package/dist/layout/default.d.ts +0 -1
- package/dist/layout/default.js +0 -1
- package/dist/lowering/loweringContext.d.ts +11 -13
- package/dist/lowering/loweringContext.js +26 -33
- package/dist/lowering/types.d.ts +1 -3
- package/dist/parser/parse-utils/note-utils.d.ts +1 -1
- package/dist/parser/parse-utils/note-utils.js +8 -9
- package/dist/parser/parse-utils/string-utils.d.ts +3 -2
- package/dist/parser/parse-utils/string-utils.js +16 -3
- package/dist/parser/parserContext.js +9 -2
- package/dist/pipeline.js +0 -1
- package/dist/playback/compile.js +120 -70
- package/dist/playback/event.d.ts +1 -1
- package/dist/playback/event.js +9 -9
- package/dist/playback/time.js +42 -31
- package/dist/playback/types.d.ts +3 -1
- package/dist/render/canvas.d.ts +2 -0
- package/dist/render/canvas.js +25 -22
- package/dist/render/recording.js +1 -4
- package/dist/render/translate.d.ts +0 -1
- package/dist/render/translate.js +6 -23
- package/grammar.md +26 -20
- package/package.json +3 -3
- package/dist/functions/note/noteNameFSM.d.ts +0 -8
- package/dist/functions/note/noteNameFSM.js +0 -181
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
function isLetterNote(ch) {
|
|
3
|
+
return (ch >= "A" && ch <= "G") || ch === "X" || ch === "Z";
|
|
4
|
+
}
|
|
5
|
+
function isDigit(ch) {
|
|
6
|
+
return ch >= "0" && ch <= "9";
|
|
7
|
+
}
|
|
8
|
+
function isAccidentalChar(ch) {
|
|
9
|
+
return ch === "#" || ch === "b" || ch === "n";
|
|
10
|
+
}
|
|
11
|
+
function readAccidental(str, pos, end) {
|
|
12
|
+
while (pos < end && isAccidentalChar(str[pos]))
|
|
13
|
+
pos++;
|
|
14
|
+
return pos;
|
|
15
|
+
}
|
|
16
|
+
/** 后置升降号不能紧接数字音名(例如 `C4#1` 或 `1#2` 中的 `#1`/`#2` 属于下一个音符) */
|
|
17
|
+
function readTrailingAccidental(str, pos, end) {
|
|
18
|
+
const accEnd = readAccidental(str, pos, end);
|
|
19
|
+
return accEnd > pos && !isDigit(str[accEnd] ?? "") ? str.slice(pos, accEnd) : null;
|
|
20
|
+
}
|
|
21
|
+
function readAbsOctave(str, pos, end) {
|
|
22
|
+
if (pos < end && (str[pos] === "+" || str[pos] === "-"))
|
|
23
|
+
pos++;
|
|
24
|
+
const digitStart = pos;
|
|
25
|
+
while (pos < end && isDigit(str[pos]))
|
|
26
|
+
pos++;
|
|
27
|
+
return pos > digitStart ? pos : -1;
|
|
28
|
+
}
|
|
29
|
+
function finishRelative(str, pos, end, name, acc) {
|
|
30
|
+
let octave = 0;
|
|
31
|
+
let next = pos;
|
|
32
|
+
while (next < end && (str[next] === "," || str[next] === "'")) {
|
|
33
|
+
octave += str[next++] === "'" ? 1 : -1;
|
|
34
|
+
}
|
|
35
|
+
return { name, next, acc, octave: next > pos ? octave : null, absOctave: false };
|
|
36
|
+
}
|
|
37
|
+
export function parseNoteName(str, start = 0, end = str.length, diagnose = true) {
|
|
38
|
+
if (end - start === 0) {
|
|
39
|
+
return diagnose ? new ErrorDiagnostic("E_WRONG_NOTE_NAME", '函数 @note 的参数 [0]:"name" 不能为空', { start, end }) : null;
|
|
40
|
+
}
|
|
41
|
+
const ch = str[start];
|
|
42
|
+
// 分支 1:字母音名(使用绝对八度)
|
|
43
|
+
if (isLetterNote(ch)) {
|
|
44
|
+
let pos = start + 1;
|
|
45
|
+
let acc = null;
|
|
46
|
+
let octave = null;
|
|
47
|
+
const octEnd = readAbsOctave(str, pos, end);
|
|
48
|
+
if (octEnd > 0) {
|
|
49
|
+
octave = parseInt(str.slice(pos, octEnd), 10);
|
|
50
|
+
pos = octEnd;
|
|
51
|
+
acc = readTrailingAccidental(str, pos, end);
|
|
52
|
+
if (acc)
|
|
53
|
+
pos += acc.length;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const accEnd = readAccidental(str, pos, end);
|
|
57
|
+
if (accEnd > pos) {
|
|
58
|
+
acc = str.slice(pos, accEnd);
|
|
59
|
+
const oct2 = readAbsOctave(str, accEnd, end);
|
|
60
|
+
if (oct2 > 0) {
|
|
61
|
+
octave = parseInt(str.slice(accEnd, oct2), 10);
|
|
62
|
+
pos = oct2;
|
|
63
|
+
}
|
|
64
|
+
else
|
|
65
|
+
pos = accEnd;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return { name: ch, next: pos, acc, octave, absOctave: true };
|
|
69
|
+
}
|
|
70
|
+
// 分支 2:数字音名(使用相对八度)
|
|
71
|
+
if (isDigit(ch)) {
|
|
72
|
+
const acc = readTrailingAccidental(str, start + 1, end);
|
|
73
|
+
return finishRelative(str, start + 1 + (acc ? acc.length : 0), end, ch, acc);
|
|
74
|
+
}
|
|
75
|
+
// 分支 3:前置升降号 + 数字音名
|
|
76
|
+
if (isAccidentalChar(ch)) {
|
|
77
|
+
const accEnd = readAccidental(str, start, end);
|
|
78
|
+
if (accEnd >= end) {
|
|
79
|
+
return diagnose ? new ErrorDiagnostic("E_WRONG_NOTE_NAME", '函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但输入已结束', { start, end }) : null;
|
|
80
|
+
}
|
|
81
|
+
const noteCh = str[accEnd];
|
|
82
|
+
if (!isDigit(noteCh)) {
|
|
83
|
+
return diagnose ? new ErrorDiagnostic("E_WRONG_NOTE_NAME", '函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但发现字符 "{ch}"', { start: accEnd, end: accEnd + 1 }, { ch: noteCh }) : null;
|
|
84
|
+
}
|
|
85
|
+
return finishRelative(str, accEnd + 1, end, noteCh, str.slice(start, accEnd));
|
|
86
|
+
}
|
|
87
|
+
return diagnose ? new ErrorDiagnostic("E_WRONG_NOTE_NAME", '函数 @note 的参数 [0]:"name" 格式错误: 首字符 "{ch}" 不合法,期望 A-G (a-g)、0-9、X 或 Z 开头,或在开头直接使用升降号(#/b/n)', { start, end: start + 1 }, { ch }) : null;
|
|
88
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { preprocessSource } from "../../parser/preprocess.js";
|
|
3
3
|
import { readCall } from "../../parser/parse-utils/call-utils.js";
|
|
4
4
|
import { findClosingQuote, quote } from "../../parser/parse-utils/string-utils.js";
|
|
5
5
|
import { NoteNameMap, SHARP_NOTE_NAMES, FLAT_NOTE_NAMES, resolveLetterNameToJianpu, tonality2Midi } from "../../parser/parse-utils/note-utils.js";
|
|
6
|
-
import { parseNoteName } from "./
|
|
6
|
+
import { parseNoteName } from "./noteName.js";
|
|
7
7
|
const LETTERS = "CDEFGAB";
|
|
8
8
|
const mod = (value, base) => ((value % base) + base) % base;
|
|
9
9
|
const accidental = (offset) => offset < 0 ? "b".repeat(-offset) : "#".repeat(offset);
|
|
@@ -15,7 +15,7 @@ function targetPitch(note, operation) {
|
|
|
15
15
|
const absolute = operation === "to-absolute"
|
|
16
16
|
|| operation !== "to-relative" && LETTERS.includes(note.ast.name);
|
|
17
17
|
const midi = note.resolvedMidi + (operation === "semitone-up" ? 1 : operation === "semitone-down" ? -1 : 0);
|
|
18
|
-
const tonic = tonality2Midi(note.keySignature
|
|
18
|
+
const tonic = tonality2Midi(note.keySignature);
|
|
19
19
|
if (![midi, tonic, note.ast.octave, note.ast.jeOctaveOffset].every(Number.isSafeInteger))
|
|
20
20
|
fail(note.ast.sourceSpan);
|
|
21
21
|
const spelling = operation === "semitone-down" ? FLAT_NOTE_NAMES : SHARP_NOTE_NAMES;
|
|
@@ -145,8 +145,9 @@ export function rewriteNotes(source, notes, operation, ranges) {
|
|
|
145
145
|
const left = tokens[index], right = tokens[index + 1];
|
|
146
146
|
if (left.span.end !== right.span.start || left.text.endsWith("}"))
|
|
147
147
|
continue;
|
|
148
|
-
const
|
|
149
|
-
|
|
148
|
+
const merged = left.text + right.text;
|
|
149
|
+
const parsed = parseNoteName(merged, 0, merged.length, false);
|
|
150
|
+
if (!parsed || parsed.next !== left.text.length) {
|
|
150
151
|
if (left.text !== source.slice(left.span.start, left.span.end))
|
|
151
152
|
left.text += " ";
|
|
152
153
|
else
|
|
@@ -37,22 +37,37 @@ class OttavaFunction extends ASTFunctionNode {
|
|
|
37
37
|
},
|
|
38
38
|
],
|
|
39
39
|
};
|
|
40
|
-
static loweringFinalize
|
|
41
|
-
const
|
|
40
|
+
static loweringFinalize(result) {
|
|
41
|
+
const ottavasByTrack = new Map();
|
|
42
|
+
for (const attachment of result.attachments) {
|
|
43
|
+
if (!(attachment instanceof OttavaAttachment))
|
|
44
|
+
continue;
|
|
42
45
|
const [from, to] = attachment.endPoints;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
const entry = { start: from.t, end: to.t.clone().add(to.T), shift: attachment.ast.shift * 12 };
|
|
47
|
+
const list = ottavasByTrack.get(from.track);
|
|
48
|
+
if (list)
|
|
49
|
+
list.push(entry);
|
|
50
|
+
else
|
|
51
|
+
ottavasByTrack.set(from.track, [entry]);
|
|
52
|
+
}
|
|
53
|
+
if (ottavasByTrack.size === 0)
|
|
46
54
|
return;
|
|
55
|
+
for (const list of ottavasByTrack.values()) {
|
|
56
|
+
list.sort((left, right) => left.start.compare(right.start));
|
|
57
|
+
}
|
|
47
58
|
for (const nodes of result.astToTemporal.values()) {
|
|
48
59
|
for (const node of nodes) {
|
|
49
60
|
if (!node.playbackState)
|
|
50
61
|
continue;
|
|
51
62
|
const host = outerHost(node);
|
|
63
|
+
const ottavas = ottavasByTrack.get(host.track);
|
|
64
|
+
if (!ottavas)
|
|
65
|
+
continue;
|
|
52
66
|
let shift = 0;
|
|
53
67
|
for (const ottava of ottavas) {
|
|
54
|
-
if (host.
|
|
55
|
-
|
|
68
|
+
if (host.t.compare(ottava.start) < 0)
|
|
69
|
+
break;
|
|
70
|
+
if (host.t.compare(ottava.end) < 0) {
|
|
56
71
|
shift += ottava.shift;
|
|
57
72
|
}
|
|
58
73
|
}
|
|
@@ -64,7 +79,7 @@ class OttavaFunction extends ASTFunctionNode {
|
|
|
64
79
|
}
|
|
65
80
|
}
|
|
66
81
|
}
|
|
67
|
-
}
|
|
82
|
+
}
|
|
68
83
|
from;
|
|
69
84
|
to;
|
|
70
85
|
shift;
|
|
@@ -95,6 +110,7 @@ class OttavaFunction extends ASTFunctionNode {
|
|
|
95
110
|
throw new ErrorDiagnostic("E_OTTAVA_CROSS_TRACK", "@ottava 的两个端点必须位于同一音轨", this.sourceSpan);
|
|
96
111
|
}
|
|
97
112
|
ctx.addAttachment(new OttavaAttachment(this, head, tail));
|
|
113
|
+
ctx.deferFinalize(OttavaFunction.loweringFinalize);
|
|
98
114
|
return [];
|
|
99
115
|
}
|
|
100
116
|
toString(source) {
|
|
@@ -28,6 +28,7 @@ export declare class PageFunction extends ASTFunctionNode {
|
|
|
28
28
|
};
|
|
29
29
|
readonly config: PageConfig | null;
|
|
30
30
|
private readonly numbering;
|
|
31
|
+
private readonly barNumbering;
|
|
31
32
|
readonly font: string;
|
|
32
33
|
constructor(sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
|
|
33
34
|
loweringEnter(ctx: LoweringContext): never[];
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DEFAULT_PAGE_CONFIG, normalizePageConfig, } from "../../layout/page.js";
|
|
2
|
-
import {
|
|
2
|
+
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { quote } from "../../parser/parse-utils/string-utils.js";
|
|
4
|
+
import { registerBarNumbering } from "../bar/index.js";
|
|
4
5
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
5
6
|
const px = (value) => ({ value, unit: "px" });
|
|
6
7
|
/** 页码字号,同时也是放得下页码所需的最小下边距 */
|
|
@@ -50,10 +51,11 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
50
51
|
~~~jpfun
|
|
51
52
|
@page(
|
|
52
53
|
width=794px, height=1123px, top=48px, bottom=48px,
|
|
53
|
-
left=40px, right=40px, gap=1em,
|
|
54
|
+
left=40px, right=40px, gap=1em, fillRatio=0.5,
|
|
55
|
+
numbering="1/1", barNumbering="(1)"
|
|
54
56
|
)
|
|
55
57
|
~~~
|
|
56
|
-
|
|
58
|
+
页边距和行距须非负;左右边距之和须小于页宽,固定页高时上下边距之和须小于页高。`,
|
|
57
59
|
allowExtraArgs: false,
|
|
58
60
|
args: [
|
|
59
61
|
{ name: "width", description: "页面宽度,须大于左右页边距之和", type: "length", default: px(DEFAULT_PAGE_CONFIG.width) },
|
|
@@ -65,12 +67,14 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
65
67
|
{ name: "gap", description: "谱面行之间的最小间距", type: "length", default: { value: 1, unit: "em" } },
|
|
66
68
|
// 单个 1 取当前页;多个 1 中最后一个取总页数
|
|
67
69
|
{ name: "numbering", description: "页码格式,空值隐藏;`1` 显示当前页,`1/1` 显示当前页/总页数", type: "string", default: "" },
|
|
68
|
-
{ name: "
|
|
70
|
+
{ name: "barNumbering", description: "行首小节号格式,默认空值隐藏;`1` 显示当前小节号,`(1)` 显示带括号的小节号", type: "string", default: "" },
|
|
71
|
+
{ name: "font", description: "页码/小节号字体,空值沿用当前字体设置", type: "string", default: "" },
|
|
69
72
|
{ name: "fillRatio", description: "自然宽度达到该比例后撑满", type: "number", default: DEFAULT_PAGE_CONFIG.fillRatio },
|
|
70
73
|
],
|
|
71
74
|
};
|
|
72
75
|
config;
|
|
73
76
|
numbering = "";
|
|
77
|
+
barNumbering = "";
|
|
74
78
|
font;
|
|
75
79
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
76
80
|
super(sourceSpan, parent);
|
|
@@ -89,20 +93,21 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
89
93
|
const values = this.getArgValue(args, ctx);
|
|
90
94
|
const fillRatio = values.pop();
|
|
91
95
|
this.font = values.pop() || ctx.variables.font;
|
|
96
|
+
const barNumbering = values.pop();
|
|
92
97
|
const numbering = values.pop();
|
|
93
98
|
const [width, height, top, bottom, left, right, gap] = values.map(value => ctx.length2px(value));
|
|
94
99
|
if (!Number.isFinite(width) || width <= 0)
|
|
95
|
-
throw
|
|
100
|
+
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的 width 必须是正有限长度", sourceSpan);
|
|
96
101
|
if (!Number.isFinite(height))
|
|
97
|
-
throw
|
|
102
|
+
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的 height 必须是有限长度;小于等于 0 表示不自动分页", sourceSpan);
|
|
98
103
|
if ([top, bottom, left, right, gap].some(value => !Number.isFinite(value) || value < 0))
|
|
99
|
-
throw
|
|
104
|
+
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的边距和 gap 必须是非负有限长度", sourceSpan);
|
|
100
105
|
if (left + right >= width)
|
|
101
|
-
throw
|
|
106
|
+
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的左右边距没有留下可用内容宽度", sourceSpan);
|
|
102
107
|
if (height > 0 && top + bottom >= height)
|
|
103
|
-
throw
|
|
108
|
+
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的上下边距没有留下可用内容高度", sourceSpan);
|
|
104
109
|
if (numbering && bottom < PAGE_NUMBER_FONT_SIZE)
|
|
105
|
-
throw
|
|
110
|
+
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的 bottom 至少需要 {minBottom}px 才能放下页码", sourceSpan, { minBottom: PAGE_NUMBER_FONT_SIZE });
|
|
106
111
|
this.config = normalizePageConfig({
|
|
107
112
|
width,
|
|
108
113
|
height,
|
|
@@ -114,6 +119,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
114
119
|
fillRatio,
|
|
115
120
|
});
|
|
116
121
|
this.numbering = numbering;
|
|
122
|
+
this.barNumbering = barNumbering;
|
|
117
123
|
}
|
|
118
124
|
loweringEnter(ctx) {
|
|
119
125
|
if (!ctx || !this.config)
|
|
@@ -122,13 +128,16 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
122
128
|
if (this.numbering) {
|
|
123
129
|
ctx.addAttachment(new PageNumberAttachment(this.sourceSpan, this.numbering, this.config.marginBottom, { ...PAGE_NUMBER_STYLE, fontFamily: this.font }));
|
|
124
130
|
}
|
|
131
|
+
if (this.barNumbering) {
|
|
132
|
+
registerBarNumbering(ctx, this.barNumbering, this.font);
|
|
133
|
+
}
|
|
125
134
|
return [];
|
|
126
135
|
}
|
|
127
136
|
toString() {
|
|
128
137
|
if (!this.config)
|
|
129
138
|
return "@page()";
|
|
130
139
|
const { width, height, marginTop, marginBottom, marginLeft, marginRight, lineGap, fillRatio } = this.config;
|
|
131
|
-
return `@page(width=${width}px, height=${Number.isFinite(height) ? height : 0}px, top=${marginTop}px, bottom=${marginBottom}px, left=${marginLeft}px, right=${marginRight}px, gap=${lineGap}px, numbering=${quote(this.numbering)}, font=${quote(this.font)}, fillRatio=${fillRatio})`;
|
|
140
|
+
return `@page(width=${width}px, height=${Number.isFinite(height) ? height : 0}px, top=${marginTop}px, bottom=${marginBottom}px, left=${marginLeft}px, right=${marginRight}px, gap=${lineGap}px, numbering=${quote(this.numbering)}, barNumbering=${quote(this.barNumbering)}, font=${quote(this.font)}, fillRatio=${fillRatio})`;
|
|
132
141
|
}
|
|
133
142
|
}
|
|
134
143
|
export const PageNode = PageFunction;
|
|
@@ -18,13 +18,13 @@ class PageBreakFunction extends ASTFunctionNode {
|
|
|
18
18
|
default: true,
|
|
19
19
|
}],
|
|
20
20
|
};
|
|
21
|
-
static loweringFinalize = BrNode.loweringFinalize;
|
|
22
21
|
justify;
|
|
23
22
|
constructor(span, args, ctx, parent = null) {
|
|
24
23
|
super(span, parent);
|
|
25
24
|
[this.justify] = this.getArgValue(args, ctx);
|
|
26
25
|
}
|
|
27
|
-
loweringEnter() {
|
|
26
|
+
loweringEnter(ctx) {
|
|
27
|
+
ctx.deferFinalize(BrNode.loweringFinalize);
|
|
28
28
|
const node = new TemporalNodeBase();
|
|
29
29
|
node.ast = this;
|
|
30
30
|
node.mergeKey = -1;
|
|
@@ -56,7 +56,9 @@ class RestFunction extends ASTFunctionNode {
|
|
|
56
56
|
}
|
|
57
57
|
// 可以被 volta 这样的标记
|
|
58
58
|
labelable() { return this; }
|
|
59
|
-
loweringEnter() {
|
|
59
|
+
loweringEnter(ctx) {
|
|
60
|
+
// 先于拍号校验(默认 order=0)补入多小节休止内部的虚拟小节边界
|
|
61
|
+
ctx.deferFinalize(RestFunction.loweringFinalize, -10);
|
|
60
62
|
return [new RestTemporal(this)];
|
|
61
63
|
}
|
|
62
64
|
static loweringFinalize(result) {
|
|
@@ -28,11 +28,11 @@ class SetFunction extends ASTFunctionNode {
|
|
|
28
28
|
const def = ctx.functions.get(key.slice(0, dot))?.prototype.def;
|
|
29
29
|
const type = def && resolveArgType(def, argName, -1);
|
|
30
30
|
if (!def || !type) {
|
|
31
|
-
ctx.diagnostics.push(new WarningDiagnostic("W_SET_UNKNOWN_TARGET",
|
|
31
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_SET_UNKNOWN_TARGET", "@set 的参数 [{key}] 没有对应的函数或其参数,将被忽略", nameSpan, { key }));
|
|
32
32
|
return null;
|
|
33
33
|
}
|
|
34
34
|
if (type === "content" || type === "label") {
|
|
35
|
-
ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE_TYPE",
|
|
35
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE_TYPE", "@set 的参数 [{key}] 内容和标签不能作为默认值,将被忽略", nameSpan, { key }));
|
|
36
36
|
return null;
|
|
37
37
|
}
|
|
38
38
|
// 别名统一成主名,否则 getArgValue 按主名查不到
|
|
@@ -44,7 +44,7 @@ class SetFunction extends ASTFunctionNode {
|
|
|
44
44
|
for (const [key, value] of args) {
|
|
45
45
|
const arg = value;
|
|
46
46
|
if (typeof key !== "string") {
|
|
47
|
-
ctx.diagnostics.push(new WarningDiagnostic("W_SET_POSITIONAL_ARG",
|
|
47
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_SET_POSITIONAL_ARG", "函数 @set 不接收位置参数, 位置参数[{key}]将被忽略", arg.span, { key }));
|
|
48
48
|
continue;
|
|
49
49
|
}
|
|
50
50
|
const target = SetFunction.resolveTarget(ctx, key.toLowerCase(), arg.nameSpan ?? arg.span);
|
|
@@ -54,7 +54,7 @@ class SetFunction extends ASTFunctionNode {
|
|
|
54
54
|
// 按目标参数的声明类型固化,否则消费者只能拿到 raw text 各自再解析一遍
|
|
55
55
|
const v = ctx.parseArgWithType(arg.valueSpan, type, sourceSpan.start);
|
|
56
56
|
if (v === null) {
|
|
57
|
-
ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE",
|
|
57
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE", "函数 @set 的参数值解析失败, 参数[{key}]将被忽略", arg.valueSpan, { key }));
|
|
58
58
|
continue;
|
|
59
59
|
}
|
|
60
60
|
ctx.setVariable(k, v);
|
|
@@ -2,11 +2,14 @@ import { type TimeState } from "../temporal.js";
|
|
|
2
2
|
import type { PlaybackCursor, PlaybackEmitter, PlaybackFlowAction } from "../../playback/types.js";
|
|
3
3
|
import type { TextStyle } from "../../render/types.js";
|
|
4
4
|
import { type ASTFunctionClass } from "../ASTtypes.js";
|
|
5
|
-
import {
|
|
5
|
+
import { ornament } from "./ornament.js";
|
|
6
|
+
import { symbolBounds, type SymbolShape } from "./shape.js";
|
|
7
|
+
export type { OrnamentPattern } from "./ornament.js";
|
|
8
|
+
export type { SymbolShape };
|
|
6
9
|
/**
|
|
7
10
|
* 一个乐谱符号:固定图形加可选的播放语义
|
|
8
11
|
*
|
|
9
|
-
*
|
|
12
|
+
* 每个符号或符号族自成一个文件,新增符号就是新增对象字面量,可复用已有图形和播放能力
|
|
10
13
|
*/
|
|
11
14
|
export interface SymbolDefinition {
|
|
12
15
|
readonly name: string;
|
|
@@ -39,4 +42,20 @@ export interface SymbolDefinition {
|
|
|
39
42
|
/** 播放到达本符号所在列时决定去留;符号恒占一列,所以生效范围不由符号自己声明 */
|
|
40
43
|
readonly onVisit?: (cursor: PlaybackCursor, at: number) => PlaybackFlowAction | undefined;
|
|
41
44
|
}
|
|
45
|
+
export declare const symbols: Readonly<{
|
|
46
|
+
/** 内置符号的只读备份列表 */
|
|
47
|
+
core: readonly SymbolDefinition[];
|
|
48
|
+
/** 当前注册表的只读视图 */
|
|
49
|
+
table: ReadonlyMap<string, SymbolDefinition>;
|
|
50
|
+
/** 注册或同名覆盖一个或多个符号定义 */
|
|
51
|
+
register: (...definitions: readonly SymbolDefinition[]) => void;
|
|
52
|
+
/** 清空所有已注册符号(含内置符号) */
|
|
53
|
+
clear: () => void;
|
|
54
|
+
/** 清空并恢复到最初的 symbols.core 状态 */
|
|
55
|
+
reset: () => void;
|
|
56
|
+
/** 装饰音播放展开工具 */
|
|
57
|
+
ornament: typeof ornament;
|
|
58
|
+
/** 计算符号图元在自身坐标系中的固有包围盒 */
|
|
59
|
+
bounds: typeof symbolBounds;
|
|
60
|
+
}>;
|
|
42
61
|
export declare const SymbolNode: ASTFunctionClass;
|
|
@@ -1,51 +1,95 @@
|
|
|
1
1
|
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { DEFAULT_KEY, TemporalNodeBase } from "../temporal.js";
|
|
3
3
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
|
+
import { ornament } from "./ornament.js";
|
|
4
5
|
import { paintSymbol, symbolBounds } from "./shape.js";
|
|
5
6
|
import { accentSymbol } from "./symbols/accent.js";
|
|
7
|
+
import { codaSymbol } from "./symbols/coda.js";
|
|
6
8
|
import { dcSymbol } from "./symbols/dc.js";
|
|
7
9
|
import { dsSymbol } from "./symbols/ds.js";
|
|
8
10
|
import { dynamicSymbols } from "./symbols/dynamics.js";
|
|
9
11
|
import { fermataSymbol } from "./symbols/fermata.js";
|
|
10
12
|
import { fineSymbol } from "./symbols/fine.js";
|
|
13
|
+
import { harmonicSymbol } from "./symbols/harmonic.js";
|
|
14
|
+
import { marcatoSymbol } from "./symbols/marcato.js";
|
|
11
15
|
import { mordentSymbol } from "./symbols/mordent.js";
|
|
12
16
|
import { prallSymbol } from "./symbols/prall.js";
|
|
13
17
|
import { segnoSymbol } from "./symbols/segno.js";
|
|
18
|
+
import { staccatoSymbol } from "./symbols/staccato.js";
|
|
19
|
+
import { tenutoSymbol } from "./symbols/tenuto.js";
|
|
20
|
+
import { tocodaSymbol } from "./symbols/tocoda.js";
|
|
14
21
|
import { trSymbol } from "./symbols/tr.js";
|
|
22
|
+
import { iturnSymbol, turnSymbol } from "./symbols/turn.js";
|
|
23
|
+
import { wheelSymbol } from "./symbols/wheel.js";
|
|
15
24
|
const symbolTable = new Map();
|
|
16
|
-
const symbolPatterns =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
25
|
+
const symbolPatterns = new Map();
|
|
26
|
+
export const symbols = Object.freeze({
|
|
27
|
+
/** 内置符号的只读备份列表 */
|
|
28
|
+
core: Object.freeze([
|
|
29
|
+
trSymbol, fermataSymbol, prallSymbol, mordentSymbol, accentSymbol, wheelSymbol, harmonicSymbol,
|
|
30
|
+
tenutoSymbol, marcatoSymbol, staccatoSymbol, turnSymbol, iturnSymbol,
|
|
31
|
+
...dynamicSymbols, dcSymbol, dsSymbol, segnoSymbol, fineSymbol, codaSymbol, tocodaSymbol,
|
|
32
|
+
]),
|
|
33
|
+
/** 当前注册表的只读视图 */
|
|
34
|
+
table: symbolTable,
|
|
35
|
+
/** 注册或同名覆盖一个或多个符号定义 */
|
|
36
|
+
register: (...definitions) => {
|
|
37
|
+
const entries = definitions.map(definition => {
|
|
38
|
+
const name = definition.name.toLowerCase();
|
|
39
|
+
let pattern = definition.pattern;
|
|
40
|
+
if (pattern) {
|
|
41
|
+
const { regex } = pattern;
|
|
42
|
+
if (regex.global || regex.sticky) {
|
|
43
|
+
throw new Error(`Symbol pattern must not use g or y flags: ${name}`);
|
|
44
|
+
}
|
|
45
|
+
if (!regex.ignoreCase)
|
|
46
|
+
pattern = { ...pattern, regex: new RegExp(regex.source, `${regex.flags}i`) };
|
|
47
|
+
}
|
|
48
|
+
return { name, definition, pattern };
|
|
49
|
+
});
|
|
50
|
+
for (const { name, definition, pattern } of entries) {
|
|
51
|
+
symbolTable.set(name, definition);
|
|
52
|
+
if (pattern)
|
|
53
|
+
symbolPatterns.set(name, pattern);
|
|
54
|
+
else
|
|
55
|
+
symbolPatterns.delete(name);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
/** 清空所有已注册符号(含内置符号) */
|
|
59
|
+
clear: () => {
|
|
60
|
+
symbolTable.clear();
|
|
61
|
+
symbolPatterns.clear();
|
|
62
|
+
},
|
|
63
|
+
/** 清空并恢复到最初的 symbols.core 状态 */
|
|
64
|
+
reset: () => {
|
|
65
|
+
symbols.clear();
|
|
66
|
+
symbols.register(...symbols.core);
|
|
67
|
+
},
|
|
68
|
+
/** 装饰音播放展开工具 */
|
|
69
|
+
ornament,
|
|
70
|
+
/** 计算符号图元在自身坐标系中的固有包围盒 */
|
|
71
|
+
bounds: symbolBounds,
|
|
72
|
+
});
|
|
73
|
+
symbols.reset();
|
|
32
74
|
class SymbolFunction extends ASTFunctionNode {
|
|
33
75
|
static def = {
|
|
34
76
|
name: ["symbol", "$"],
|
|
35
77
|
description: "创建一个乐谱符号",
|
|
36
|
-
details
|
|
78
|
+
get details() {
|
|
79
|
+
return `\
|
|
37
80
|
~~~jpfun
|
|
38
81
|
1 ^ @symbol(p, size=0.5em)
|
|
39
82
|
~~~
|
|
40
83
|
**简写**:\`$name\`,如 \`1 ^ $p\`。名称读取到下一个空白或当前内容区间末尾,连续的标点也属于名称。
|
|
41
84
|
|
|
42
|
-
|
|
43
|
-
${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.description}`).join("\n")}
|
|
85
|
+
**已注册记号**
|
|
86
|
+
${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.description}`).join("\n")}`;
|
|
87
|
+
},
|
|
44
88
|
allowExtraArgs: false,
|
|
45
89
|
args: [
|
|
46
90
|
{
|
|
47
91
|
name: "name",
|
|
48
|
-
description: "
|
|
92
|
+
description: "已注册记号名称,见函数说明中的记号列表",
|
|
49
93
|
type: "string",
|
|
50
94
|
default: null
|
|
51
95
|
},
|
|
@@ -84,7 +128,7 @@ ${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.descriptio
|
|
|
84
128
|
let definition = symbolTable.get(normalized);
|
|
85
129
|
if (!definition) {
|
|
86
130
|
let matched;
|
|
87
|
-
for (const pattern of symbolPatterns) {
|
|
131
|
+
for (const pattern of symbolPatterns.values()) {
|
|
88
132
|
const match = pattern.regex.exec(normalized);
|
|
89
133
|
if (!match || match.index !== 0 || match[0] !== normalized)
|
|
90
134
|
continue;
|
|
@@ -95,10 +139,11 @@ ${[...symbolTable.values()].map(item => `- \`$${item.name}\`:${item.descriptio
|
|
|
95
139
|
definition = matched?.pattern.resolve(matched.match);
|
|
96
140
|
}
|
|
97
141
|
if (!definition) {
|
|
98
|
-
const message =
|
|
142
|
+
const message = "@symbol: 未知符号: ${name}";
|
|
143
|
+
const params = { name };
|
|
99
144
|
if (ctx.variables.strict)
|
|
100
|
-
throw new ErrorDiagnostic("E_UNKNOWN_SYMBOL", message, sourceSpan);
|
|
101
|
-
ctx.diagnostics.push(new WarningDiagnostic("W_UNKNOWN_SYMBOL", message, sourceSpan));
|
|
145
|
+
throw new ErrorDiagnostic("E_UNKNOWN_SYMBOL", message, sourceSpan, params);
|
|
146
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_UNKNOWN_SYMBOL", message, sourceSpan, params));
|
|
102
147
|
return;
|
|
103
148
|
}
|
|
104
149
|
this.definition = definition;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { PlaybackTransform } from "../../playback/types.js";
|
|
2
|
-
interface OrnamentPattern {
|
|
2
|
+
export interface OrnamentPattern {
|
|
3
3
|
/** 调内音级偏移序列,0 表示本音 */
|
|
4
4
|
readonly body: readonly number[];
|
|
5
5
|
/** 主体是否重复到填满宿主时值 */
|
|
@@ -12,4 +12,3 @@ interface OrnamentPattern {
|
|
|
12
12
|
* 派生音段继承轨道和来源,仍只声明音段,不直接发布 NoteOn/NoteOff。
|
|
13
13
|
*/
|
|
14
14
|
export declare function ornament(pattern: OrnamentPattern): PlaybackTransform;
|
|
15
|
-
export {};
|
|
@@ -10,6 +10,12 @@ import type { PaintStyle, Painter, PathCommand } from "../../render/types.js";
|
|
|
10
10
|
/** 符号的一个图元;坐标在符号自身坐标系里,缩放由布局决定 */
|
|
11
11
|
export interface SymbolShape {
|
|
12
12
|
readonly path?: readonly PathCommand[];
|
|
13
|
+
readonly line?: {
|
|
14
|
+
readonly x1: number;
|
|
15
|
+
readonly y1: number;
|
|
16
|
+
readonly x2: number;
|
|
17
|
+
readonly y2: number;
|
|
18
|
+
};
|
|
13
19
|
readonly circle?: {
|
|
14
20
|
readonly cx: number;
|
|
15
21
|
readonly cy: number;
|
|
@@ -22,9 +22,17 @@ export function symbolBounds(shapes) {
|
|
|
22
22
|
};
|
|
23
23
|
for (const shape of shapes) {
|
|
24
24
|
const dx = shape.dx ?? 0;
|
|
25
|
+
if (shape.line) {
|
|
26
|
+
const { x1, y1, x2, y2 } = shape.line;
|
|
27
|
+
const halfStroke = (shape.style.strokeWidth ?? 1) / 2;
|
|
28
|
+
merge(Math.min(x1, x2) + dx - halfStroke, Math.min(y1, y2) - halfStroke, Math.abs(x2 - x1) + halfStroke * 2, Math.abs(y2 - y1) + halfStroke * 2);
|
|
29
|
+
}
|
|
25
30
|
if (shape.circle) {
|
|
26
31
|
const { cx, cy, r } = shape.circle;
|
|
27
|
-
|
|
32
|
+
const stroke = shape.style.stroke && shape.style.stroke !== "none"
|
|
33
|
+
? (shape.style.strokeWidth ?? 1) / 2 : 0;
|
|
34
|
+
const outer = r + stroke;
|
|
35
|
+
merge(cx + dx - outer, cy - outer, 2 * outer, 2 * outer);
|
|
28
36
|
}
|
|
29
37
|
if (shape.path?.length) {
|
|
30
38
|
const rect = pathBoundsCache.get(shape.path) ?? pathBounds(shape.path);
|
|
@@ -41,10 +49,15 @@ export function paintSymbol(painter, shapes, bounds, box, scale) {
|
|
|
41
49
|
const originY = box.y - bounds.y * scale;
|
|
42
50
|
for (const shape of shapes) {
|
|
43
51
|
const style = shape.style;
|
|
44
|
-
const
|
|
52
|
+
const strokeWidth = style.strokeWidth ?? (shape.line || shape.circle ? 1 : undefined);
|
|
53
|
+
const scaled = strokeWidth === undefined
|
|
45
54
|
? style
|
|
46
|
-
: { ...style, strokeWidth:
|
|
55
|
+
: { ...style, strokeWidth: strokeWidth * scale };
|
|
47
56
|
const x = originX + (shape.dx ?? 0) * scale;
|
|
57
|
+
if (shape.line) {
|
|
58
|
+
const { x1, y1, x2, y2 } = shape.line;
|
|
59
|
+
painter.drawLine(x + x1 * scale, originY + y1 * scale, x + x2 * scale, originY + y2 * scale, scaled);
|
|
60
|
+
}
|
|
48
61
|
if (shape.path) {
|
|
49
62
|
painter.drawPath(shape.path, scaled, { x, y: originY, scaleX: scale, scaleY: scale });
|
|
50
63
|
}
|