jpfun 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +141 -11
- package/dist/converter/midi/convert.d.ts +50 -0
- package/dist/converter/midi/convert.js +700 -0
- package/dist/converter/midi/index.d.ts +5 -0
- package/dist/converter/midi/index.js +11 -0
- package/dist/converter/midi/json.d.ts +38 -0
- package/dist/converter/midi/json.js +1 -0
- package/dist/converter/midi/triplet.d.ts +30 -0
- package/dist/converter/midi/triplet.js +75 -0
- package/dist/converter/musicxml/dom.d.ts +25 -0
- package/dist/converter/musicxml/dom.js +65 -0
- package/dist/converter/musicxml/features.d.ts +51 -0
- package/dist/converter/musicxml/features.js +232 -0
- package/dist/converter/musicxml/index.d.ts +9 -0
- package/dist/converter/musicxml/index.js +1140 -0
- package/dist/converter/musicxml/model.d.ts +123 -0
- package/dist/converter/musicxml/model.js +1 -0
- package/dist/converter/source.d.ts +22 -0
- package/dist/converter/source.js +51 -0
- package/dist/core.d.ts +22 -0
- package/dist/core.js +22 -0
- package/dist/diagnostic.js +5 -2
- package/dist/fraction.d.ts +24 -0
- package/dist/fraction.js +132 -0
- package/dist/functions/ASTtypes.d.ts +51 -31
- package/dist/functions/ASTtypes.js +59 -38
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/arpeggio/index.d.ts +2 -0
- package/dist/functions/arpeggio/index.js +180 -0
- package/dist/functions/arpeggio/shape.d.ts +7 -0
- package/dist/functions/arpeggio/shape.js +122 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +62 -25
- package/dist/functions/beam/auto.d.ts +1 -1
- package/dist/functions/beam/auto.js +33 -16
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +4 -3
- package/dist/functions/beam/layout.js +43 -65
- package/dist/functions/box/index.js +37 -36
- package/dist/functions/br/index.js +10 -8
- package/dist/functions/dash/index.js +40 -7
- package/dist/functions/default.js +19 -8
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +45 -17
- package/dist/functions/dot/index.js +23 -25
- package/dist/functions/dyn/index.d.ts +2 -0
- package/dist/functions/dyn/index.js +242 -0
- package/dist/functions/grace/index.d.ts +8 -11
- package/dist/functions/grace/index.js +84 -79
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +496 -0
- package/dist/functions/key/index.js +98 -3
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +155 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +106 -79
- package/dist/functions/note/noteNameFSM.js +52 -63
- package/dist/functions/page/index.d.ts +11 -2
- package/dist/functions/page/index.js +65 -4
- package/dist/functions/program/index.d.ts +2 -0
- package/dist/functions/program/index.js +30 -0
- package/dist/functions/set/index.js +43 -32
- package/dist/functions/stack/index.js +26 -22
- package/dist/functions/symbol/index.d.ts +31 -0
- package/dist/functions/symbol/index.js +139 -0
- package/dist/functions/symbol/ornament.d.ts +15 -0
- package/dist/functions/symbol/ornament.js +73 -0
- package/dist/functions/symbol/shape.d.ts +24 -0
- package/dist/functions/symbol/shape.js +52 -0
- package/dist/functions/symbol/symbols/accent.d.ts +2 -0
- package/dist/functions/symbol/symbols/accent.js +24 -0
- package/dist/functions/symbol/symbols/dc.d.ts +4 -0
- package/dist/functions/symbol/symbols/dc.js +82 -0
- package/dist/functions/symbol/symbols/ds.d.ts +2 -0
- package/dist/functions/symbol/symbols/ds.js +93 -0
- package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
- package/dist/functions/symbol/symbols/dynamics.js +168 -0
- package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
- package/dist/functions/symbol/symbols/fermata.js +24 -0
- package/dist/functions/symbol/symbols/fine.d.ts +2 -0
- package/dist/functions/symbol/symbols/fine.js +12 -0
- package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +56 -0
- package/dist/functions/symbol/symbols/prall.d.ts +2 -0
- package/dist/functions/symbol/symbols/prall.js +42 -0
- package/dist/functions/symbol/symbols/segno.d.ts +4 -0
- package/dist/functions/symbol/symbols/segno.js +82 -0
- package/dist/functions/symbol/symbols/tr.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +71 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +57 -4
- package/dist/functions/temporal.d.ts +138 -0
- package/dist/functions/temporal.js +101 -0
- package/dist/functions/text/index.js +86 -27
- package/dist/functions/tie/index.js +136 -152
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +218 -0
- package/dist/functions/up/index.d.ts +1 -0
- package/dist/functions/up/index.js +258 -182
- package/dist/functions/voice/index.js +203 -109
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +230 -0
- package/dist/iife/core.d.ts +1 -0
- package/dist/iife/core.js +3 -0
- package/dist/iife/from-midi.d.ts +1 -0
- package/dist/iife/from-midi.js +15 -0
- package/dist/iife/from-musicxml.d.ts +1 -0
- package/dist/iife/from-musicxml.js +3 -0
- package/dist/iife/full.d.ts +1 -0
- package/dist/iife/full.js +3 -0
- package/dist/index.d.ts +3 -17
- package/dist/index.js +3 -17
- package/dist/jpfun.core.min.js +153 -0
- package/dist/jpfun.from-midi.min.js +9 -0
- package/dist/jpfun.from-musicxml.min.js +12 -0
- package/dist/jpfun.min.js +166 -0
- package/dist/layout/default.js +2 -3
- package/dist/layout/engine.d.ts +6 -12
- package/dist/layout/engine.js +115 -64
- package/dist/layout/model.d.ts +12 -5
- package/dist/layout/model.js +31 -18
- package/dist/layout/page.js +5 -4
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +71 -20
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +20 -20
- package/dist/lowering/loweringContext.js +153 -101
- package/dist/lowering/track.d.ts +5 -9
- package/dist/lowering/track.js +7 -2
- package/dist/lowering/types.d.ts +26 -112
- package/dist/lowering/types.js +1 -92
- package/dist/parser/parse-utils/call-utils.d.ts +0 -2
- package/dist/parser/parse-utils/call-utils.js +0 -6
- package/dist/parser/parse-utils/note-utils.d.ts +8 -0
- package/dist/parser/parse-utils/note-utils.js +35 -13
- package/dist/parser/parse-utils/string-utils.d.ts +10 -0
- package/dist/parser/parse-utils/string-utils.js +24 -0
- package/dist/parser/parserContext.d.ts +11 -9
- package/dist/parser/parserContext.js +65 -55
- package/dist/parser/preprocess.js +17 -11
- package/dist/pipeline.d.ts +6 -5
- package/dist/pipeline.js +4 -5
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +414 -0
- package/dist/playback/event.d.ts +105 -0
- package/dist/playback/event.js +105 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +129 -0
- package/dist/playback/types.js +3 -0
- package/dist/render/canvas.d.ts +4 -4
- package/dist/render/canvas.js +20 -5
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/dist/render/paint.d.ts +8 -0
- package/dist/render/paint.js +101 -0
- package/dist/render/svg.d.ts +3 -3
- package/dist/render/svg.js +10 -8
- package/dist/render/text.d.ts +22 -0
- package/dist/render/text.js +42 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +132 -13
- package/package.json +19 -6
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
-
const LETTER_NOTE_START_RE = /^[A-
|
|
2
|
+
const LETTER_NOTE_START_RE = /^[A-GXZ]/;
|
|
3
3
|
const NUM_NOTE_START_RE = /^[0-9]/;
|
|
4
4
|
const SHARP_RE = /^[#bn]+/;
|
|
5
5
|
const ABS_OCTAVE_RE = /^[+-]?\d+/;
|
|
@@ -23,7 +23,7 @@ export function parseNoteName(str, start = 0, end = str.length) {
|
|
|
23
23
|
else if (NUM_NOTE_START_RE.test(ch)) {
|
|
24
24
|
name = ch;
|
|
25
25
|
absOctave = false;
|
|
26
|
-
state =
|
|
26
|
+
state = 3;
|
|
27
27
|
}
|
|
28
28
|
else {
|
|
29
29
|
const match = str.slice(pos, end).match(SHARP_RE);
|
|
@@ -31,26 +31,26 @@ export function parseNoteName(str, start = 0, end = str.length) {
|
|
|
31
31
|
acc = match[0];
|
|
32
32
|
pos += acc.length - 1;
|
|
33
33
|
absOctave = false;
|
|
34
|
-
state =
|
|
34
|
+
state = 2;
|
|
35
35
|
}
|
|
36
36
|
else
|
|
37
|
-
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 首字符 "${ch}" 不合法,期望 A-G (a-g)
|
|
37
|
+
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 首字符 "${ch}" 不合法,期望 A-G (a-g)、0-9、X 或 Z 开头,或在开头直接使用升降号(#/b/n)`, { start: pos, end: pos + 1 });
|
|
38
38
|
}
|
|
39
39
|
break;
|
|
40
|
-
case 1:
|
|
40
|
+
case 1: // [字母模式] 已获得音名
|
|
41
41
|
const subs = str.slice(pos, end);
|
|
42
42
|
match = subs.match(ABS_OCTAVE_RE);
|
|
43
43
|
if (match) {
|
|
44
44
|
octave = parseInt(match[0], 10);
|
|
45
45
|
pos += match[0].length - 1;
|
|
46
|
-
state =
|
|
46
|
+
state = 4;
|
|
47
47
|
break;
|
|
48
48
|
}
|
|
49
49
|
match = subs.match(SHARP_RE);
|
|
50
50
|
if (match) {
|
|
51
51
|
acc = match[0];
|
|
52
52
|
pos += acc.length - 1;
|
|
53
|
-
state =
|
|
53
|
+
state = 5;
|
|
54
54
|
break;
|
|
55
55
|
}
|
|
56
56
|
return {
|
|
@@ -60,87 +60,82 @@ export function parseNoteName(str, start = 0, end = str.length) {
|
|
|
60
60
|
octave: null,
|
|
61
61
|
absOctave,
|
|
62
62
|
};
|
|
63
|
-
case 2:
|
|
64
|
-
|
|
63
|
+
case 2: // [数字模式] 已获得升降号
|
|
64
|
+
if (NUM_NOTE_START_RE.test(ch)) {
|
|
65
|
+
name = ch;
|
|
66
|
+
state = 6;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
else
|
|
70
|
+
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但发现字符 "${ch}"`, { start: pos, end: pos + 1 });
|
|
71
|
+
case 3: // [数字模式] 已获得音名
|
|
72
|
+
match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
|
|
65
73
|
if (match) {
|
|
66
|
-
|
|
74
|
+
const octave_str = match[0];
|
|
75
|
+
octave = relativeOctaveFromString(octave_str);
|
|
67
76
|
return {
|
|
68
77
|
name: name,
|
|
69
|
-
next: pos +
|
|
78
|
+
next: pos + octave_str.length,
|
|
70
79
|
acc,
|
|
71
|
-
octave,
|
|
80
|
+
octave: octave,
|
|
72
81
|
absOctave,
|
|
73
82
|
};
|
|
74
83
|
}
|
|
84
|
+
match = str.slice(pos, end).match(SHARP_RE);
|
|
85
|
+
if (match && !NUM_NOTE_START_RE.test(str[pos + match[0].length] ?? "")) {
|
|
86
|
+
const accidental = match[0];
|
|
87
|
+
acc = accidental;
|
|
88
|
+
pos += accidental.length - 1;
|
|
89
|
+
state = 6;
|
|
90
|
+
break;
|
|
91
|
+
}
|
|
75
92
|
return {
|
|
76
93
|
name: name,
|
|
77
94
|
next: pos,
|
|
78
|
-
acc
|
|
95
|
+
acc,
|
|
79
96
|
octave: octave,
|
|
80
97
|
absOctave,
|
|
81
98
|
};
|
|
82
|
-
case
|
|
83
|
-
match = str.slice(pos, end).match(
|
|
84
|
-
if (match) {
|
|
85
|
-
|
|
99
|
+
case 4: // [字母模式] 已获得音名和八度
|
|
100
|
+
match = str.slice(pos, end).match(SHARP_RE);
|
|
101
|
+
if (match && !NUM_NOTE_START_RE.test(str[pos + match[0].length] ?? "")) {
|
|
102
|
+
const accidental = match[0];
|
|
103
|
+
acc = accidental;
|
|
86
104
|
return {
|
|
87
105
|
name: name,
|
|
88
|
-
next: pos +
|
|
106
|
+
next: pos + accidental.length,
|
|
89
107
|
acc,
|
|
90
108
|
octave,
|
|
91
109
|
absOctave,
|
|
92
110
|
};
|
|
93
111
|
}
|
|
94
|
-
return {
|
|
95
|
-
name: name,
|
|
96
|
-
next: pos,
|
|
97
|
-
acc: acc,
|
|
98
|
-
octave: null,
|
|
99
|
-
absOctave,
|
|
100
|
-
};
|
|
101
|
-
case 4:
|
|
102
|
-
match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
|
|
103
|
-
if (match) {
|
|
104
|
-
const octave_str = match[0];
|
|
105
|
-
octave = relativeOctaveFromString(octave_str);
|
|
106
|
-
pos += octave_str.length - 1;
|
|
107
|
-
state = 5;
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
match = str.slice(pos, end).match(SHARP_RE);
|
|
111
|
-
if (match) {
|
|
112
|
-
acc = match[0];
|
|
113
|
-
pos += acc.length - 1;
|
|
114
|
-
state = 6;
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
112
|
return {
|
|
118
113
|
name: name,
|
|
119
114
|
next: pos,
|
|
120
115
|
acc: null,
|
|
121
|
-
octave
|
|
116
|
+
octave,
|
|
122
117
|
absOctave,
|
|
123
118
|
};
|
|
124
|
-
case 5:
|
|
125
|
-
match = str.slice(pos, end).match(
|
|
119
|
+
case 5: // [字母模式] 已获得音名和半音
|
|
120
|
+
match = str.slice(pos, end).match(ABS_OCTAVE_RE);
|
|
126
121
|
if (match) {
|
|
127
|
-
|
|
122
|
+
octave = parseInt(match[0], 10);
|
|
128
123
|
return {
|
|
129
124
|
name: name,
|
|
130
|
-
next: pos +
|
|
125
|
+
next: pos + match[0].length,
|
|
131
126
|
acc,
|
|
132
|
-
octave
|
|
127
|
+
octave,
|
|
133
128
|
absOctave,
|
|
134
129
|
};
|
|
135
130
|
}
|
|
136
131
|
return {
|
|
137
132
|
name: name,
|
|
138
133
|
next: pos,
|
|
139
|
-
acc:
|
|
140
|
-
octave:
|
|
134
|
+
acc: acc,
|
|
135
|
+
octave: null,
|
|
141
136
|
absOctave,
|
|
142
137
|
};
|
|
143
|
-
case 6:
|
|
138
|
+
case 6: // [数字模式] 已获得升降号和音名
|
|
144
139
|
match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
|
|
145
140
|
if (match) {
|
|
146
141
|
const octave_str = match[0];
|
|
@@ -148,32 +143,26 @@ export function parseNoteName(str, start = 0, end = str.length) {
|
|
|
148
143
|
return {
|
|
149
144
|
name: name,
|
|
150
145
|
next: pos + octave_str.length,
|
|
151
|
-
acc
|
|
152
|
-
octave
|
|
146
|
+
acc,
|
|
147
|
+
octave,
|
|
153
148
|
absOctave,
|
|
154
149
|
};
|
|
155
150
|
}
|
|
156
151
|
return {
|
|
157
152
|
name: name,
|
|
158
153
|
next: pos,
|
|
159
|
-
acc
|
|
160
|
-
octave
|
|
154
|
+
acc,
|
|
155
|
+
octave,
|
|
161
156
|
absOctave,
|
|
162
157
|
};
|
|
163
|
-
case 7:
|
|
164
|
-
if (NUM_NOTE_START_RE.test(ch)) {
|
|
165
|
-
name = ch;
|
|
166
|
-
state = 6;
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
else
|
|
170
|
-
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但发现字符 "${ch}"`, { start: pos, end: pos + 1 });
|
|
171
158
|
default:
|
|
172
159
|
return Diagnostic.error.Bug("音符名称解析状态机进入了未定义的状态", { start: pos, end });
|
|
173
160
|
}
|
|
174
161
|
}
|
|
162
|
+
if (name === null)
|
|
163
|
+
return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但输入已结束`, { start, end });
|
|
175
164
|
return {
|
|
176
|
-
name
|
|
165
|
+
name,
|
|
177
166
|
next: end,
|
|
178
167
|
acc,
|
|
179
168
|
octave,
|
|
@@ -1,20 +1,29 @@
|
|
|
1
1
|
import type { LoweringContext } from "../../lowering/loweringContext.js";
|
|
2
2
|
import type { PageConfig } from "../../layout/types.js";
|
|
3
3
|
import { ASTFunctionNode, type ASTFunctionClass, type ASTNodeBase, type FunctionArgs, type LengthValue, type ParserContext, type SourceSpan } from "../ASTtypes.js";
|
|
4
|
+
/** 页码字号,同时也是放得下页码所需的最小下边距 */
|
|
5
|
+
export declare const PAGE_NUMBER_FONT_SIZE = 16;
|
|
4
6
|
export declare class PageFunction extends ASTFunctionNode {
|
|
5
7
|
static def: {
|
|
6
8
|
name: string[];
|
|
7
9
|
description: string;
|
|
8
10
|
example: string;
|
|
9
11
|
allowExtraArgs: boolean;
|
|
10
|
-
args: {
|
|
12
|
+
args: ({
|
|
11
13
|
name: string;
|
|
12
14
|
type: "length";
|
|
13
15
|
default: LengthValue;
|
|
14
|
-
}
|
|
16
|
+
} | {
|
|
17
|
+
name: string;
|
|
18
|
+
type: "string";
|
|
19
|
+
default: string;
|
|
20
|
+
})[];
|
|
15
21
|
};
|
|
16
22
|
readonly config: PageConfig | null;
|
|
23
|
+
private readonly numbering;
|
|
24
|
+
readonly font: string;
|
|
17
25
|
constructor(sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
|
|
18
26
|
loweringEnter(ctx: LoweringContext): never[];
|
|
27
|
+
toString(): string;
|
|
19
28
|
}
|
|
20
29
|
export declare const PageNode: ASTFunctionClass;
|
|
@@ -1,12 +1,52 @@
|
|
|
1
1
|
import { DEFAULT_PAGE_CONFIG, normalizePageConfig, } from "../../layout/page.js";
|
|
2
2
|
import { Diagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { quote } from "../../parser/parse-utils/string-utils.js";
|
|
3
4
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
5
|
const px = (value) => ({ value, unit: "px" });
|
|
6
|
+
/** 页码字号,同时也是放得下页码所需的最小下边距 */
|
|
7
|
+
export const PAGE_NUMBER_FONT_SIZE = 16;
|
|
8
|
+
const PAGE_NUMBER_STYLE = { fontSize: PAGE_NUMBER_FONT_SIZE, textAlign: "center", fill: "#606060" };
|
|
9
|
+
/** 单个 1 取当前页;多个 1 中最后一个取总页数,其余取当前页 */
|
|
10
|
+
function formatPageNumber(pattern, current, total) {
|
|
11
|
+
const last = pattern.lastIndexOf("1");
|
|
12
|
+
const hasTotal = pattern.indexOf("1") !== last;
|
|
13
|
+
return pattern.replace(/1/g, (_, index) => String(hasTotal && index === last ? total : current));
|
|
14
|
+
}
|
|
15
|
+
/** 浮在每页下边距带里的页码,不属于任何谱面行,因此不申报 Track 占用 */
|
|
16
|
+
class PageNumberAttachment {
|
|
17
|
+
sourceSpan;
|
|
18
|
+
pattern;
|
|
19
|
+
marginBottom;
|
|
20
|
+
style;
|
|
21
|
+
layer = "foreground";
|
|
22
|
+
constructor(sourceSpan, pattern, marginBottom, style) {
|
|
23
|
+
this.sourceSpan = sourceSpan;
|
|
24
|
+
this.pattern = pattern;
|
|
25
|
+
this.marginBottom = marginBottom;
|
|
26
|
+
this.style = style;
|
|
27
|
+
}
|
|
28
|
+
createGeometry(context) {
|
|
29
|
+
const style = this.style;
|
|
30
|
+
const marks = context.pages.map((page, index) => {
|
|
31
|
+
const text = formatPageNumber(this.pattern, index + 1, context.pages.length);
|
|
32
|
+
const { w, h, baseline } = context.textMeasurer.measureText(text, style);
|
|
33
|
+
const center = page.y + page.h - this.marginBottom / 2;
|
|
34
|
+
return { text, x: page.x + page.w / 2, y: center - h / 2 + baseline, w, h, baseline };
|
|
35
|
+
});
|
|
36
|
+
return {
|
|
37
|
+
regions: marks.map(({ x, y, w, h, baseline }) => ({ x: x - w / 2, y: y - baseline, w, h })),
|
|
38
|
+
paint(painter) {
|
|
39
|
+
for (const mark of marks)
|
|
40
|
+
painter.drawText(mark.text, mark.x, mark.y, style);
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
}
|
|
5
45
|
export class PageFunction extends ASTFunctionNode {
|
|
6
46
|
static def = {
|
|
7
47
|
name: ["page"],
|
|
8
|
-
description: "
|
|
9
|
-
example: "@page(width=794px, height=1123px, top=48px, bottom=48px, left=40px, right=40px, gap=1em)",
|
|
48
|
+
description: "设置文档页面尺寸、边距、最小谱面行间距和页码",
|
|
49
|
+
example: "@page(width=794px, height=1123px, top=48px, bottom=48px, left=40px, right=40px, gap=1em, numbering=\"1/1\")",
|
|
10
50
|
allowExtraArgs: false,
|
|
11
51
|
args: [
|
|
12
52
|
{ name: "width", type: "length", default: px(DEFAULT_PAGE_CONFIG.width) },
|
|
@@ -16,11 +56,17 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
16
56
|
{ name: "left", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginLeft) },
|
|
17
57
|
{ name: "right", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginRight) },
|
|
18
58
|
{ name: "gap", type: "length", default: { value: 1, unit: "em" } },
|
|
59
|
+
// 单个 1 取当前页;多个 1 中最后一个取总页数
|
|
60
|
+
{ name: "numbering", type: "string", default: "" },
|
|
61
|
+
{ name: "font", type: "string", default: "" },
|
|
19
62
|
],
|
|
20
63
|
};
|
|
21
64
|
config;
|
|
65
|
+
numbering = "";
|
|
66
|
+
font;
|
|
22
67
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
23
68
|
super(sourceSpan, parent);
|
|
69
|
+
this.font = ctx.variables.font;
|
|
24
70
|
if (ctx.scopeDepth !== 0) {
|
|
25
71
|
ctx.diagnostics.push(new WarningDiagnostic("W_PAGE_NOT_TOP_LEVEL", "@page 只能在文档顶层声明;当前声明已忽略", sourceSpan));
|
|
26
72
|
this.config = null;
|
|
@@ -33,6 +79,8 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
33
79
|
}
|
|
34
80
|
ctx.documentDeclarations["page"] = true;
|
|
35
81
|
const values = this.getArgValue(args, ctx);
|
|
82
|
+
this.font = values.pop() || ctx.variables.font;
|
|
83
|
+
const numbering = values.pop();
|
|
36
84
|
const [width, height, top, bottom, left, right, gap] = values.map(value => ctx.length2px(value));
|
|
37
85
|
if (!Number.isFinite(width) || width <= 0)
|
|
38
86
|
throw Diagnostic.error.InvalidPageConfig("@page 的 width 必须是正有限长度", sourceSpan);
|
|
@@ -44,6 +92,8 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
44
92
|
throw Diagnostic.error.InvalidPageConfig("@page 的左右边距没有留下可用内容宽度", sourceSpan);
|
|
45
93
|
if (height > 0 && top + bottom >= height)
|
|
46
94
|
throw Diagnostic.error.InvalidPageConfig("@page 的上下边距没有留下可用内容高度", sourceSpan);
|
|
95
|
+
if (numbering && bottom < PAGE_NUMBER_FONT_SIZE)
|
|
96
|
+
throw Diagnostic.error.InvalidPageConfig(`@page 的 bottom 至少需要 ${PAGE_NUMBER_FONT_SIZE}px 才能放下页码`, sourceSpan);
|
|
47
97
|
this.config = normalizePageConfig({
|
|
48
98
|
width,
|
|
49
99
|
height,
|
|
@@ -53,11 +103,22 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
53
103
|
marginRight: right,
|
|
54
104
|
lineGap: gap,
|
|
55
105
|
});
|
|
106
|
+
this.numbering = numbering;
|
|
56
107
|
}
|
|
57
108
|
loweringEnter(ctx) {
|
|
58
|
-
if (ctx
|
|
59
|
-
|
|
109
|
+
if (!ctx || !this.config)
|
|
110
|
+
return [];
|
|
111
|
+
ctx.setPageConfig(this.config);
|
|
112
|
+
if (this.numbering) {
|
|
113
|
+
ctx.addAttachment(new PageNumberAttachment(this.sourceSpan, this.numbering, this.config.marginBottom, { ...PAGE_NUMBER_STYLE, fontFamily: this.font }));
|
|
114
|
+
}
|
|
60
115
|
return [];
|
|
61
116
|
}
|
|
117
|
+
toString() {
|
|
118
|
+
if (!this.config)
|
|
119
|
+
return "@page()";
|
|
120
|
+
const { width, height, marginTop, marginBottom, marginLeft, marginRight, lineGap } = this.config;
|
|
121
|
+
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)})`;
|
|
122
|
+
}
|
|
62
123
|
}
|
|
63
124
|
export const PageNode = PageFunction;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { TemporalNodeBase } from "../temporal.js";
|
|
3
|
+
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
|
+
class ProgramFunction extends ASTFunctionNode {
|
|
5
|
+
static def = {
|
|
6
|
+
name: ["program", "instrument"],
|
|
7
|
+
description: "设置当前音轨的 MIDI 音色",
|
|
8
|
+
example: `@program(40) 将后续音符切换到 MIDI program 40`,
|
|
9
|
+
allowExtraArgs: false,
|
|
10
|
+
args: [{ name: "program", type: "number", default: null }],
|
|
11
|
+
};
|
|
12
|
+
program;
|
|
13
|
+
constructor(span, args, ctx, parent = null) {
|
|
14
|
+
super(span, parent);
|
|
15
|
+
const [program] = this.getArgValue(args, ctx);
|
|
16
|
+
if (!Number.isSafeInteger(program) || program < 0 || program > 127) {
|
|
17
|
+
throw new ErrorDiagnostic("E_PROGRAM_INVALID", "@program 必须是 0..127 的整数", span);
|
|
18
|
+
}
|
|
19
|
+
this.program = program;
|
|
20
|
+
}
|
|
21
|
+
loweringEnter() {
|
|
22
|
+
const node = new TemporalNodeBase();
|
|
23
|
+
node.ast = this;
|
|
24
|
+
node.playbackState = { program: this.program };
|
|
25
|
+
node.onTimeState = (state) => { state.program = this.program; };
|
|
26
|
+
return [node];
|
|
27
|
+
}
|
|
28
|
+
toString() { return `@program(${this.program})`; }
|
|
29
|
+
}
|
|
30
|
+
export const ProgramNode = ProgramFunction;
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
-
import {
|
|
1
|
+
import { ASTFunctionNode, primaryName, resolveArgType } from "../ASTtypes.js";
|
|
2
|
+
import { WarningDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { quote } from "../../parser/parse-utils/string-utils.js";
|
|
4
|
+
import { ParserContext } from "../../parser/parserContext.js";
|
|
3
5
|
class SetFunction extends ASTFunctionNode {
|
|
4
6
|
static def = {
|
|
5
7
|
name: ["set"],
|
|
6
8
|
description: "设置局部默认值",
|
|
7
|
-
example:
|
|
8
|
-
|
|
9
|
+
example: `当前有以下系统配置:
|
|
10
|
+
@set(fontSize=20) 则当前块内默认字体变为20px
|
|
11
|
+
@set(strict=true) 开启严格模式
|
|
12
|
+
还可以设置其他函数的默认值: {函数名}.{属性}=值,例:
|
|
9
13
|
@set(note.octave=5, note.color=#f00)
|
|
10
14
|
|
|
11
15
|
注意:@set 作用域仅在其所处 '{}' 内、其出现之后。离开当前作用域会恢复为之前的设置。此设置和时序无关,类似于局部变量。
|
|
@@ -13,43 +17,50 @@ class SetFunction extends ASTFunctionNode {
|
|
|
13
17
|
allowExtraArgs: true,
|
|
14
18
|
args: []
|
|
15
19
|
};
|
|
20
|
+
/** 值的类型由目标参数决定:`函数名.参数名` 查该函数的声明,无点号的是内置变量 */
|
|
21
|
+
static resolveTarget(ctx, key, nameSpan) {
|
|
22
|
+
const dot = key.lastIndexOf(".");
|
|
23
|
+
if (dot < 0)
|
|
24
|
+
return [key, Object.hasOwn(ParserContext.systemVariables, key) ? ParserContext.systemVariables[key].type : "string"];
|
|
25
|
+
const argName = key.slice(dot + 1);
|
|
26
|
+
const def = ctx.functions.get(key.slice(0, dot))?.prototype.def;
|
|
27
|
+
const type = def && resolveArgType(def, argName, -1);
|
|
28
|
+
if (!def || !type) {
|
|
29
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_SET_UNKNOWN_TARGET", `@set 的参数 [${key}] 没有对应的函数或其参数,将被忽略`, nameSpan));
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
if (type === "content" || type === "label") {
|
|
33
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE_TYPE", `@set 的参数 [${key}] 内容和标签不能作为默认值,将被忽略`, nameSpan));
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
// 别名统一成主名,否则 getArgValue 按主名查不到
|
|
37
|
+
return [`${primaryName(def)}.${argName}`.toLowerCase(), type];
|
|
38
|
+
}
|
|
16
39
|
args = new Map();
|
|
17
40
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
18
41
|
super(sourceSpan, parent);
|
|
19
42
|
for (const [key, value] of args) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE", `函数 @set 的参数值解析失败, 参数[${key}]将被忽略`, value));
|
|
25
|
-
continue;
|
|
26
|
-
}
|
|
27
|
-
let k = key.toLowerCase(); // 统一小写处理
|
|
28
|
-
try {
|
|
29
|
-
// ctx内置的setter可能报错
|
|
30
|
-
ctx.variables[k] = v;
|
|
31
|
-
this.args.set(k, v);
|
|
32
|
-
if (k === "fontsize")
|
|
33
|
-
ctx.fontSize = v;
|
|
34
|
-
else if (k === "strict")
|
|
35
|
-
ctx.strict = v;
|
|
36
|
-
}
|
|
37
|
-
catch (e) {
|
|
38
|
-
if (e instanceof Diagnostic) {
|
|
39
|
-
e.span = value;
|
|
40
|
-
if (e.code[0] === "E")
|
|
41
|
-
throw e;
|
|
42
|
-
ctx.diagnostics.push(e);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
43
|
+
const arg = value;
|
|
44
|
+
if (typeof key !== "string") {
|
|
45
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_SET_POSITIONAL_ARG", `函数 @set 不接收位置参数, 位置参数[${key}]将被忽略`, arg.span));
|
|
46
|
+
continue;
|
|
45
47
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
const target = SetFunction.resolveTarget(ctx, key.toLowerCase(), arg.nameSpan ?? arg.span);
|
|
49
|
+
if (target === null)
|
|
50
|
+
continue;
|
|
51
|
+
const [k, type] = target;
|
|
52
|
+
// 按目标参数的声明类型固化,否则消费者只能拿到 raw text 各自再解析一遍
|
|
53
|
+
const v = ctx.parseArgWithType(arg.valueSpan, type, sourceSpan.start);
|
|
54
|
+
if (v === null) {
|
|
55
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_SET_INVALID_VALUE", `函数 @set 的参数值解析失败, 参数[${key}]将被忽略`, arg.valueSpan));
|
|
56
|
+
continue;
|
|
48
57
|
}
|
|
58
|
+
ctx.setVariable(k, v);
|
|
59
|
+
this.args.set(k, v);
|
|
49
60
|
}
|
|
50
61
|
}
|
|
51
62
|
toString() {
|
|
52
|
-
return `@set(${Array.from(this.args
|
|
63
|
+
return `@set(${Array.from(this.args, ([key, value]) => `${key}=${typeof value === "string" ? quote(value) : value}`).join(", ")})`;
|
|
53
64
|
}
|
|
54
65
|
}
|
|
55
66
|
export const SetNode = SetFunction;
|
|
@@ -71,31 +71,34 @@ class StackFunction extends ASTFunctionNode {
|
|
|
71
71
|
continue;
|
|
72
72
|
break;
|
|
73
73
|
}
|
|
74
|
-
let
|
|
75
|
-
if (
|
|
74
|
+
let leftNode = left >= 0 ? ctx.nodes[left] : null;
|
|
75
|
+
if (leftNode === null) {
|
|
76
76
|
throw new ErrorDiagnostic("E_STACK_NO_TARGET", "@stack语法糖错误: 左边没有找到可叠加的目标", n.span);
|
|
77
77
|
}
|
|
78
78
|
/** 左操作数在 ctx.nodes 中的起点;只有这一段会被 stack 吞并,更早的节点必须保留 */
|
|
79
79
|
let replaceFrom = left;
|
|
80
80
|
// 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
|
|
81
|
-
if (
|
|
82
|
-
const
|
|
81
|
+
if (leftNode instanceof ASTLabelNode) {
|
|
82
|
+
const labelNode = leftNode;
|
|
83
|
+
let tgt = labelNode.target;
|
|
84
|
+
// 在本层这个节点位于哪里(标签节点可能被wrap)
|
|
85
|
+
while (tgt && !ctx.nodes.includes(tgt))
|
|
86
|
+
tgt = tgt.parent;
|
|
83
87
|
for (let j = left - 1; j >= 0; j--) {
|
|
84
88
|
if (ctx.nodes[j] === tgt) {
|
|
85
|
-
|
|
89
|
+
leftNode = new ASTBraceNode({
|
|
86
90
|
start: tgt.sourceSpan.start,
|
|
87
|
-
end:
|
|
91
|
+
end: labelNode.sourceSpan.end,
|
|
88
92
|
}, ctx.nodes.slice(j, left + 1), null);
|
|
89
93
|
replaceFrom = j;
|
|
90
94
|
break;
|
|
91
95
|
}
|
|
92
96
|
}
|
|
93
97
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
98
|
+
// 左操作数收敛成一个 stack,后续合并都在它身上进行
|
|
99
|
+
const host = leftNode instanceof StackFunction
|
|
100
|
+
? leftNode
|
|
101
|
+
: new StackFunction(n.span, new Map(), ctx).addContent(leftNode);
|
|
99
102
|
// 找到下一个非文本节点 通过全量后续解析的方式进行 还是有些trick
|
|
100
103
|
const storage = ctx.nodes;
|
|
101
104
|
ctx.nodes = [];
|
|
@@ -105,9 +108,9 @@ class StackFunction extends ASTFunctionNode {
|
|
|
105
108
|
const right = ctx.nodes[i];
|
|
106
109
|
if (right instanceof ASTTextNode)
|
|
107
110
|
continue;
|
|
108
|
-
|
|
111
|
+
host.addContent(right);
|
|
109
112
|
storage.length = replaceFrom;
|
|
110
|
-
storage.push(
|
|
113
|
+
storage.push(host);
|
|
111
114
|
while (++i < ctx.nodes.length)
|
|
112
115
|
storage.push(ctx.nodes[i]);
|
|
113
116
|
ctx.nodes = storage;
|
|
@@ -131,27 +134,28 @@ class StackFunction extends ASTFunctionNode {
|
|
|
131
134
|
this.addContent(value);
|
|
132
135
|
continue;
|
|
133
136
|
}
|
|
134
|
-
const c = ctx.parseArgWithType(value.
|
|
137
|
+
const c = ctx.parseArgWithType(value.valueSpan, "content", span.start);
|
|
135
138
|
if (c !== null) {
|
|
136
139
|
this.addContent(c);
|
|
137
140
|
}
|
|
138
141
|
}
|
|
139
142
|
}
|
|
143
|
+
/** 遇到另一个 stack 就展平;返回存活的那个,调用者必须改用返回值 */
|
|
140
144
|
addContent(node) {
|
|
141
145
|
if (node instanceof StackFunction)
|
|
142
|
-
this.combine(node);
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
146
|
+
return this.combine(node);
|
|
147
|
+
this.contents.push(node);
|
|
148
|
+
node.parent = this;
|
|
149
|
+
const s = node.sourceSpan;
|
|
150
|
+
this.sourceSpan.start = Math.min(this.sourceSpan.start, s.start);
|
|
151
|
+
this.sourceSpan.end = Math.max(this.sourceSpan.end, s.end);
|
|
152
|
+
return this;
|
|
150
153
|
}
|
|
151
154
|
toString(source) {
|
|
152
155
|
const contentStrs = this.contents.map(c => c.toString(source)).join(',\n');
|
|
153
156
|
return `@stack(\n ${contentStrs.split('\n').join('\n ')}\n)`;
|
|
154
157
|
}
|
|
158
|
+
// stack 不实现 labelable、不进候选表,所以被吞并的空壳不会被 @tie() 取到,无需像 up 那样撤销登记
|
|
155
159
|
combine(ano) {
|
|
156
160
|
this.sourceSpan.start = Math.min(this.sourceSpan.start, ano.sourceSpan.start);
|
|
157
161
|
this.sourceSpan.end = Math.max(this.sourceSpan.end, ano.sourceSpan.end);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type TimeState } from "../temporal.js";
|
|
2
|
+
import type { PlaybackCursor, PlaybackEmitter, PlaybackFlowAction } from "../../playback/types.js";
|
|
3
|
+
import { type ASTFunctionClass } from "../ASTtypes.js";
|
|
4
|
+
import { type SymbolShape } from "./shape.js";
|
|
5
|
+
/**
|
|
6
|
+
* 一个乐谱符号:固定图形加可选的播放语义
|
|
7
|
+
*
|
|
8
|
+
* 每个符号自成一个文件、彼此没有依赖,新增一个符号就是新增一个对象字面量
|
|
9
|
+
*/
|
|
10
|
+
export interface SymbolDefinition {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly description: string;
|
|
13
|
+
/** 固定图形;文字记号改用 text */
|
|
14
|
+
readonly shapes?: readonly SymbolShape[];
|
|
15
|
+
/** 交给宿主字体绘制的文字记号,与 shapes 二选一 */
|
|
16
|
+
readonly text?: {
|
|
17
|
+
readonly content: string;
|
|
18
|
+
readonly fontFamily: string;
|
|
19
|
+
};
|
|
20
|
+
/** 符号高度 = size × weight,用来修正宽扁字形的视觉重量;默认 1 */
|
|
21
|
+
readonly weight?: number;
|
|
22
|
+
/** 写入本位置之后持续生效的状态,例如力度记号的 velocity */
|
|
23
|
+
readonly onTimeState?: (state: TimeState) => void;
|
|
24
|
+
/** 发布系统控制,或注册对同一折叠序列后续音符的局部变换 */
|
|
25
|
+
readonly emitPlayback?: (emitter: PlaybackEmitter) => void;
|
|
26
|
+
/** 贴在所在列上的标记,供 D.S. 这类跳转回查 */
|
|
27
|
+
readonly playbackMarks?: () => readonly string[];
|
|
28
|
+
/** 播放到达本符号所在列时决定去留;符号恒占一列,所以生效范围不由符号自己声明 */
|
|
29
|
+
readonly onVisit?: (cursor: PlaybackCursor, at: number) => PlaybackFlowAction | undefined;
|
|
30
|
+
}
|
|
31
|
+
export declare const SymbolNode: ASTFunctionClass;
|