jpfun 0.4.1 → 0.6.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 +53 -113
- package/dist/converter/midi/convert.js +14 -7
- package/dist/converter/musicxml/features.d.ts +57 -16
- package/dist/converter/musicxml/features.js +204 -42
- package/dist/converter/musicxml/index.d.ts +7 -2
- package/dist/converter/musicxml/index.js +256 -268
- package/dist/converter/musicxml/model.d.ts +36 -7
- package/dist/converter/musicxml/resolve.d.ts +10 -0
- package/dist/converter/musicxml/resolve.js +152 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/fraction.js +7 -1
- package/dist/functions/ASTtypes.d.ts +6 -3
- package/dist/functions/ASTtypes.js +30 -23
- package/dist/functions/adjust/index.js +10 -50
- package/dist/functions/arpeggio/index.js +40 -25
- package/dist/functions/beam/auto.js +2 -11
- package/dist/functions/box/index.js +132 -82
- package/dist/functions/dash/index.js +20 -11
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +1 -1
- package/dist/functions/div/index.js +9 -10
- package/dist/functions/dot/index.js +4 -5
- package/dist/functions/dyn/index.js +28 -32
- package/dist/functions/grace/index.d.ts +23 -8
- package/dist/functions/grace/index.js +138 -119
- package/dist/functions/head/index.js +11 -4
- package/dist/functions/key/index.js +1 -1
- package/dist/functions/meter/index.js +40 -7
- package/dist/functions/meter/shape.d.ts +3 -0
- package/dist/functions/meter/shape.js +43 -0
- package/dist/functions/note/index.d.ts +6 -1
- package/dist/functions/note/index.js +60 -57
- package/dist/functions/note/source-transform.d.ts +21 -0
- package/dist/functions/note/source-transform.js +164 -0
- package/dist/functions/page/index.d.ts +5 -0
- package/dist/functions/page/index.js +8 -5
- package/dist/functions/rest/index.d.ts +2 -0
- package/dist/functions/rest/index.js +233 -0
- package/dist/functions/set/index.js +5 -1
- package/dist/functions/symbol/index.d.ts +2 -0
- package/dist/functions/symbol/index.js +1 -1
- package/dist/functions/symbol/ornament.d.ts +3 -3
- package/dist/functions/symbol/ornament.js +21 -35
- package/dist/functions/symbol/symbols/accent.js +5 -5
- package/dist/functions/symbol/symbols/fermata.js +5 -4
- package/dist/functions/temporal.d.ts +21 -7
- package/dist/functions/temporal.js +14 -4
- package/dist/functions/tie/index.js +36 -42
- package/dist/functions/tuplet/index.js +20 -24
- package/dist/functions/up/index.js +54 -34
- package/dist/functions/voice/connections.d.ts +19 -0
- package/dist/functions/voice/connections.js +88 -0
- package/dist/functions/voice/index.js +248 -102
- package/dist/functions/volta/index.js +7 -12
- package/dist/jpfun.core.min.js +61 -41
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +70 -50
- package/dist/layout/engine.d.ts +13 -1
- package/dist/layout/engine.js +458 -129
- package/dist/layout/model.js +22 -66
- package/dist/layout/page.js +3 -1
- package/dist/layout/range.d.ts +26 -0
- package/dist/layout/range.js +75 -0
- package/dist/layout/types.d.ts +63 -49
- package/dist/lowering/loweringContext.d.ts +1 -1
- package/dist/lowering/loweringContext.js +34 -4
- package/dist/lowering/types.d.ts +5 -0
- package/dist/parser/parse-utils/length-utils.js +2 -0
- package/dist/parser/parse-utils/note-utils.d.ts +2 -0
- package/dist/parser/parse-utils/note-utils.js +6 -2
- package/dist/parser/parserContext.js +5 -9
- package/dist/parser/preprocess.js +4 -6
- package/dist/playback/compile.d.ts +2 -9
- package/dist/playback/compile.js +297 -186
- package/dist/playback/event.d.ts +8 -16
- package/dist/playback/event.js +13 -3
- package/dist/playback/types.d.ts +76 -18
- package/dist/playback/types.js +1 -0
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/paint.js +1 -1
- package/dist/render/recording.d.ts +1 -0
- package/dist/render/recording.js +21 -0
- package/dist/render/translate.d.ts +14 -0
- package/dist/render/translate.js +48 -0
- package/grammar.md +200 -250
- package/package.json +4 -4
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { symbolBounds } from "../symbol/shape.js";
|
|
2
|
+
export const sanbanShapes = [
|
|
3
|
+
{
|
|
4
|
+
path: [
|
|
5
|
+
{ op: "M", x: 1, y: 34 },
|
|
6
|
+
{ op: "C", cx1: 23, cy1: 36, cx2: 57, cy2: 28, x: 82, y: 23 },
|
|
7
|
+
{ op: "C", cx1: 91, cy1: 21, cx2: 98, cy2: 24, x: 102, y: 29 },
|
|
8
|
+
{ op: "Q", cx: 106, cy: 34, x: 98, y: 34 },
|
|
9
|
+
{ op: "C", cx1: 68, cy1: 30, cx2: 37, cy2: 39, x: 13, y: 43 },
|
|
10
|
+
{ op: "Q", cx: 7, cy: 44, x: 1, y: 39 },
|
|
11
|
+
{ op: "Q", cx: -3, cy: 35, x: 1, y: 34 },
|
|
12
|
+
{ op: "Z" },
|
|
13
|
+
],
|
|
14
|
+
style: { fill: "#000" },
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
path: [
|
|
18
|
+
{ op: "M", x: 29, y: 5 },
|
|
19
|
+
{ op: "Q", cx: 27, cy: 3, x: 31, y: 4 },
|
|
20
|
+
{ op: "Q", cx: 44, cy: 7, x: 42, y: 12 },
|
|
21
|
+
{ op: "C", cx1: 39, cy1: 26, cx2: 43, cy2: 43, x: 42, y: 60 },
|
|
22
|
+
{ op: "Q", cx: 41, cy: 69, x: 38, y: 62 },
|
|
23
|
+
{ op: "C", cx1: 34, cy1: 47, cx2: 35, cy2: 20, x: 29, y: 5 },
|
|
24
|
+
{ op: "Z" },
|
|
25
|
+
],
|
|
26
|
+
style: { fill: "#000" },
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
path: [
|
|
30
|
+
{ op: "M", x: 59, y: 1 },
|
|
31
|
+
{ op: "Q", cx: 56, cy: -2, x: 61, y: 0 },
|
|
32
|
+
{ op: "Q", cx: 76, cy: 4, x: 74, y: 9 },
|
|
33
|
+
{ op: "C", cx1: 71, cy1: 17, cx2: 74, cy2: 32, x: 69, y: 48 },
|
|
34
|
+
{ op: "C", cx1: 64, cy1: 66, cx2: 55, cy2: 77, x: 40, y: 84 },
|
|
35
|
+
{ op: "Q", cx: 37, cy: 85, x: 40, y: 82 },
|
|
36
|
+
{ op: "C", cx1: 57, cy1: 66, cx2: 63, cy2: 47, x: 64, y: 29 },
|
|
37
|
+
{ op: "C", cx1: 65, cy1: 17, cx2: 63, cy2: 7, x: 59, y: 1 },
|
|
38
|
+
{ op: "Z" },
|
|
39
|
+
],
|
|
40
|
+
style: { fill: "#000" },
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
export const sanbanBounds = symbolBounds(sanbanShapes);
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
import { ASTFunctionClass } from "../ASTtypes.js";
|
|
1
|
+
import { SourceSpan, ASTFunctionClass } from "../ASTtypes.js";
|
|
2
|
+
import type { LoweringResult } from "../../lowering/types.js";
|
|
3
|
+
import { type NoteTransform, type SourceEdit } from "./source-transform.js";
|
|
4
|
+
export type { NoteTransform, SourceEdit } from "./source-transform.js";
|
|
5
|
+
/** 根据同一源码的 lowering 生成局部修改;传入 ranges 时只处理完整落入任一选区的音符 */
|
|
6
|
+
export declare function transformNotes(source: string, lowering: LoweringResult, operation: NoteTransform, ranges?: readonly SourceSpan[]): SourceEdit[];
|
|
2
7
|
export declare const NoteNode: ASTFunctionClass;
|
|
@@ -2,6 +2,19 @@ import { ASTTextNode, ASTFunctionNode } from "../ASTtypes.js";
|
|
|
2
2
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { parseNoteName } from "./noteNameFSM.js";
|
|
4
4
|
import { paintAccidental, placeAccidentals } from "./accidentals.js";
|
|
5
|
+
import { rewriteNotes } from "./source-transform.js";
|
|
6
|
+
/** 根据同一源码的 lowering 生成局部修改;传入 ranges 时只处理完整落入任一选区的音符 */
|
|
7
|
+
export function transformNotes(source, lowering, operation, ranges) {
|
|
8
|
+
const error = lowering.diagnostics.find(item => item instanceof ErrorDiagnostic);
|
|
9
|
+
if (error)
|
|
10
|
+
throw error;
|
|
11
|
+
const notes = [...lowering.astToTemporal.values()].flat()
|
|
12
|
+
.filter((node) => node instanceof NoteTemporalNode);
|
|
13
|
+
const invalid = lowering.diagnostics.find(item => notes.some(note => item.span.start < note.ast.sourceSpan.end && item.span.end > note.ast.sourceSpan.start));
|
|
14
|
+
if (invalid)
|
|
15
|
+
throw invalid;
|
|
16
|
+
return rewriteNotes(source, notes, operation, ranges);
|
|
17
|
+
}
|
|
5
18
|
const JE_OCTAVE_OFFSET = "note.jeOctaveOffset";
|
|
6
19
|
class NoteFunction extends ASTFunctionNode {
|
|
7
20
|
static def = {
|
|
@@ -12,8 +25,9 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
12
25
|
@note(A, acc=#, octave=3, color=#c33)
|
|
13
26
|
~~~
|
|
14
27
|
**简写**:数字音名按“升降号、音名、相对八度”书写;字母音名按“音名、升降号、绝对八度”书写。\`1,,\` 比当前调性基准低两个八度,\`1'\` 高一个八度。
|
|
15
|
-
-
|
|
16
|
-
-
|
|
28
|
+
- 简写的升降号完全由自身决定,省略时为空,不继承 \`@set(note.acc=...)\`;显式调用省略 \`acc\` 时仍继承默认值
|
|
29
|
+
- \`A#3\` 等价于 \`@note(A, #, 3)\`,默认 \`acc\` 为空时也等价于 \`@note(A#3)\`
|
|
30
|
+
- \`#1'\` 等价于 \`@note(1, #, 1)\`,默认 \`acc\` 为空时也等价于 \`@note("#1'")\`
|
|
17
31
|
- 支持组合升降号,如 \`A##bn99\`;无歧义时也接受后置写法,如 \`A3#\`、\`6#,,\`
|
|
18
32
|
- 升降号后紧跟数字时归入下一音符:\`6#3\` 解析为 \`6\` 和 \`#3\``,
|
|
19
33
|
allowExtraArgs: false,
|
|
@@ -75,8 +89,7 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
75
89
|
argMap.set("name", parseResult.name);
|
|
76
90
|
if (parseResult.octave !== null)
|
|
77
91
|
argMap.set("octave", parseResult.octave);
|
|
78
|
-
|
|
79
|
-
argMap.set("acc", parseResult.acc);
|
|
92
|
+
argMap.set("acc", parseResult.acc ?? ""); // 必须覆盖不然会用到 set 的默认值
|
|
80
93
|
const node = {
|
|
81
94
|
kind: "call",
|
|
82
95
|
typed: true,
|
|
@@ -110,8 +123,11 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
110
123
|
font;
|
|
111
124
|
// JE 八度偏移量
|
|
112
125
|
jeOctaveOffset;
|
|
126
|
+
// 源码改写时判断是否需要覆盖默认升降号
|
|
127
|
+
hasDefaultAcc;
|
|
113
128
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
114
129
|
super(sourceSpan, parent);
|
|
130
|
+
this.hasDefaultAcc = Boolean(ctx.variables["note.acc"]);
|
|
115
131
|
[this.name, this.acc, this.octave, this.color] = this.getArgValue(args, ctx);
|
|
116
132
|
this.font = ctx.variables.numberfont;
|
|
117
133
|
this.size = ctx.variables.fontsize;
|
|
@@ -144,7 +160,7 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
144
160
|
this.acc = parseResult.acc + this.acc;
|
|
145
161
|
}
|
|
146
162
|
toString() {
|
|
147
|
-
return `@n(${this.name}, ${this.acc}, ${this.octave}, ${this.color})`;
|
|
163
|
+
return `@n(${this.name}, ${this.acc || '""'}, ${this.octave}, ${this.color})`;
|
|
148
164
|
}
|
|
149
165
|
}
|
|
150
166
|
export const NoteNode = NoteFunction;
|
|
@@ -155,6 +171,7 @@ const NOTE_GLYPH = { "8": "", "9": "X" };
|
|
|
155
171
|
class NoteTemporalNode extends TemporalNodeBase {
|
|
156
172
|
// 时间固化后的参数
|
|
157
173
|
resolvedMidi = null; // MIDI音高 name是数字则需要在onTimeState中基于当前调性偏移 播放的音高
|
|
174
|
+
keySignature = "C4"; // 用于转调时记录大环境
|
|
158
175
|
transposeDiatonic;
|
|
159
176
|
name; // 数字
|
|
160
177
|
acc; // 升降号
|
|
@@ -163,10 +180,6 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
163
180
|
accidentals = [];
|
|
164
181
|
/** 字号和颜色在解析时已经固化,样式随之不再变 */
|
|
165
182
|
numberStyle;
|
|
166
|
-
/** 上八度点属于主体,下八度点通过通用装饰流排在其他下方符号之后 */
|
|
167
|
-
upperOctaveDotY = [];
|
|
168
|
-
lowerOctaveDotY = [];
|
|
169
|
-
octaveDotRadius = 0;
|
|
170
183
|
numberY = 0;
|
|
171
184
|
constructor(ast) {
|
|
172
185
|
super();
|
|
@@ -186,68 +199,57 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
186
199
|
this.initLayoutBox();
|
|
187
200
|
}
|
|
188
201
|
prepareLayout(context) {
|
|
189
|
-
this.upperOctaveDotY.length = 0;
|
|
190
|
-
this.lowerOctaveDotY.length = 0;
|
|
191
202
|
const { size, color } = this.ast;
|
|
192
203
|
const mainMetrics = context.textMeasurer.measureText("0", this.numberStyle);
|
|
193
|
-
|
|
194
|
-
this.octaveDotRadius = size * 0.065;
|
|
195
|
-
const dotGap = size * 0.08;
|
|
196
|
-
const dotStep = this.octaveDotRadius * 2 + dotGap;
|
|
197
|
-
const octaveSpace = dotCount === 0
|
|
198
|
-
? 0
|
|
199
|
-
: dotCount * this.octaveDotRadius * 2 + (dotCount - 1) * dotGap + dotGap;
|
|
200
|
-
const mainY = this.octave > 0 ? octaveSpace : 0;
|
|
201
|
-
this.numberY = mainY + mainMetrics.baseline;
|
|
204
|
+
this.numberY = mainMetrics.baseline;
|
|
202
205
|
// 升降号贴近数字左上角,按同一 baseline 排在数字左侧;
|
|
203
206
|
// 缩小右侧间隔会在全局 anchor 不变时把升降号向右移动
|
|
204
207
|
const accidentalGap = size * 0.01;
|
|
205
208
|
const { placed, right: mainX } = placeAccidentals(this.acc, size, 0, this.numberY, accidentalGap);
|
|
206
209
|
this.accidentals = placed;
|
|
207
210
|
this.box.w = mainX + mainMetrics.w;
|
|
208
|
-
this.box.h =
|
|
211
|
+
this.box.h = mainMetrics.h;
|
|
209
212
|
this.box.anchor = mainX + mainMetrics.w / 2;
|
|
210
|
-
this.box.visualAxis =
|
|
213
|
+
this.box.visualAxis = mainMetrics.h / 2;
|
|
211
214
|
// 升降号和附点参与完整盒布局,但不计入数字主体范围,减时线因此只画在数字下
|
|
212
215
|
this.ports["body.left"] = { x: this.box.anchor - mainMetrics.w / 2, y: this.box.visualAxis };
|
|
213
216
|
this.ports["body.right"] = { x: this.box.anchor + mainMetrics.w / 2, y: this.box.visualAxis };
|
|
217
|
+
this.ports["shoulder"] = { x: this.box.anchor, y: 0 };
|
|
218
|
+
// 下方装饰从数字基线附近起排,不把 em 盒底部的留白算作可见正文。
|
|
219
|
+
this.ports["decoration.below"] = { x: this.box.anchor, y: this.numberY + size * 0.04 };
|
|
214
220
|
// 覆盖 dot 的默认“右边界 + 视觉轴”,让附点贴合数字字形的视觉位置
|
|
215
221
|
this.ports["dot"] = {
|
|
216
222
|
x: this.box.w,
|
|
217
|
-
y:
|
|
223
|
+
y: mainMetrics.h * 0.64,
|
|
218
224
|
};
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
for (let i = 0; i < dotCount; i++) {
|
|
222
|
-
this.upperOctaveDotY.push(mainY - dotGap - this.octaveDotRadius - i * dotStep);
|
|
223
|
-
}
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
if (this.octave >= 0)
|
|
225
|
+
const dotCount = Math.abs(this.octave);
|
|
226
|
+
if (dotCount === 0)
|
|
227
227
|
return;
|
|
228
|
-
const
|
|
229
|
-
const
|
|
230
|
-
|
|
228
|
+
const above = this.octave > 0;
|
|
229
|
+
const dotRadius = size * 0.075;
|
|
230
|
+
const dotGap = size * 0.08;
|
|
231
|
+
const dotStep = dotRadius * 2 + dotGap;
|
|
232
|
+
const dotStyle = { fill: color };
|
|
233
|
+
let dotY = 0;
|
|
234
|
+
const height = dotCount * dotRadius * 2 + (dotCount - 1) * dotGap;
|
|
235
|
+
const dots = {
|
|
236
|
+
[above ? "above" : "below"]: {
|
|
231
237
|
// order 只表达通用的由近到远顺序
|
|
232
238
|
// 时值类装饰可以选择较小 order,下八度点选择较大 order
|
|
233
239
|
order: 100,
|
|
234
240
|
gap: dotGap,
|
|
235
|
-
height
|
|
236
|
-
place: y => {
|
|
237
|
-
// 下八度点从引擎分配的外层区域顶部向下排列
|
|
238
|
-
for (let i = 0; i < dotCount; i++) {
|
|
239
|
-
this.lowerOctaveDotY.push(y + this.octaveDotRadius + i * dotStep);
|
|
240
|
-
}
|
|
241
|
-
},
|
|
241
|
+
height,
|
|
242
|
+
place: (y) => { dotY = y; },
|
|
242
243
|
},
|
|
243
244
|
paint: painter => {
|
|
244
|
-
//
|
|
245
|
-
for (
|
|
246
|
-
|
|
245
|
+
// 每列八度点共用一个装饰,上方从靠近数字的位置向外绘制
|
|
246
|
+
for (let i = 0; i < dotCount; i++) {
|
|
247
|
+
const y = above ? dotY + height - dotRadius - i * dotStep : dotY + dotRadius + i * dotStep;
|
|
248
|
+
painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, dotRadius, dotStyle);
|
|
247
249
|
}
|
|
248
250
|
},
|
|
249
251
|
};
|
|
250
|
-
this.decorations.push(
|
|
252
|
+
this.decorations.push(dots);
|
|
251
253
|
}
|
|
252
254
|
finalizeLayout() {
|
|
253
255
|
this.ports["lyric"] = { x: this.box.anchor, y: this.box.h };
|
|
@@ -260,13 +262,10 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
260
262
|
if (glyph) {
|
|
261
263
|
painter.drawText(glyph, this.box.x + this.box.anchor, this.box.y + this.numberY, this.numberStyle);
|
|
262
264
|
}
|
|
263
|
-
// 上八度点属于主体绘制,不参与下方装饰流
|
|
264
|
-
for (const y of this.upperOctaveDotY) {
|
|
265
|
-
painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, this.octaveDotRadius, { fill: color });
|
|
266
|
-
}
|
|
267
265
|
}
|
|
266
|
+
/** 固化记谱位置的音高、力度和音色,并保留基于该调性的相对移调能力 */
|
|
268
267
|
onTimeState(state) {
|
|
269
|
-
const keySignature = state.keySignature;
|
|
268
|
+
const keySignature = this.keySignature = state.keySignature;
|
|
270
269
|
this.playbackState = { bpm: state.bpm, velocity: state.velocity, program: state.program };
|
|
271
270
|
this.resolvedMidi = resolveNoteMidi(this.name, this.acc, this.octave, keySignature);
|
|
272
271
|
this.transposeDiatonic = createDiatonicTranspose(this.name, this.acc, this.octave, keySignature);
|
|
@@ -287,20 +286,24 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
287
286
|
}
|
|
288
287
|
}
|
|
289
288
|
}
|
|
289
|
+
/** 发布完整音段或无声区间,实际 NoteOn/NoteOff 留到声音展开完成后生成 */
|
|
290
290
|
emitPlayback(emitter) {
|
|
291
291
|
const percussion = this.ast.name === "9";
|
|
292
292
|
const midi = percussion ? 37 : this.resolvedMidi;
|
|
293
|
-
if (
|
|
293
|
+
if (emitter.end.compare(emitter.start) <= 0)
|
|
294
|
+
return;
|
|
295
|
+
// 休止符和占位符也要保留结构边界,速度效果与后续延长才能覆盖这段时间。
|
|
296
|
+
if (midi === null) {
|
|
297
|
+
emitter.span({ start: emitter.start, end: emitter.end });
|
|
294
298
|
return;
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
299
|
+
}
|
|
300
|
+
// 这里只提供发声属性;轨道、来源和最终音符身份由核心统一补齐。
|
|
301
|
+
emitter.note({
|
|
302
|
+
start: emitter.start,
|
|
303
|
+
end: emitter.end,
|
|
300
304
|
midi,
|
|
301
305
|
velocity: this.playbackState?.velocity ?? DEFAULT_VELOCITY,
|
|
302
306
|
...(percussion ? { percussion: true } : { transpose: this.transposeDiatonic }),
|
|
303
307
|
});
|
|
304
|
-
emitter.emit({ kind: "note-off", at: emitter.end, noteId });
|
|
305
308
|
}
|
|
306
309
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ASTNodeBase } from "../ASTtypes.js";
|
|
2
|
+
import type { SourceSpan } from "../../parser/types.js";
|
|
3
|
+
export type NoteTransform = "to-absolute" | "to-relative" | "semitone-up" | "semitone-down";
|
|
4
|
+
export interface SourceEdit {
|
|
5
|
+
span: SourceSpan;
|
|
6
|
+
text: string;
|
|
7
|
+
}
|
|
8
|
+
interface NoteSource {
|
|
9
|
+
ast: ASTNodeBase & {
|
|
10
|
+
name: string;
|
|
11
|
+
acc: string;
|
|
12
|
+
octave: number;
|
|
13
|
+
hasDefaultAcc: boolean;
|
|
14
|
+
jeOctaveOffset: number;
|
|
15
|
+
};
|
|
16
|
+
resolvedMidi: number | null;
|
|
17
|
+
keySignature: string;
|
|
18
|
+
}
|
|
19
|
+
/** edits 始终引用原文;参数只改值,默认参数只在当前调用覆盖,避免影响其他音符 */
|
|
20
|
+
export declare function rewriteNotes(source: string, notes: readonly NoteSource[], operation: NoteTransform, ranges?: readonly SourceSpan[]): SourceEdit[];
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { preprocessSource } from "../../parser/preprocess.js";
|
|
3
|
+
import { readCall } from "../../parser/parse-utils/call-utils.js";
|
|
4
|
+
import { findClosingQuote, quote } from "../../parser/parse-utils/string-utils.js";
|
|
5
|
+
import { NoteNameMap, SHARP_NOTE_NAMES, FLAT_NOTE_NAMES, resolveLetterNameToJianpu, tonality2Midi } from "../../parser/parse-utils/note-utils.js";
|
|
6
|
+
import { parseNoteName } from "./noteNameFSM.js";
|
|
7
|
+
const LETTERS = "CDEFGAB";
|
|
8
|
+
const mod = (value, base) => ((value % base) + base) % base;
|
|
9
|
+
const accidental = (offset) => offset < 0 ? "b".repeat(-offset) : "#".repeat(offset);
|
|
10
|
+
function fail(span) {
|
|
11
|
+
throw new ErrorDiagnostic("E_NOTE_TRANSFORM", "无法安全改写此音符的音高", span);
|
|
12
|
+
}
|
|
13
|
+
/** 音高计算只读固化事实;互转保留调内级数,半音操作采用最少升降号的拼写 */
|
|
14
|
+
function targetPitch(note, operation) {
|
|
15
|
+
const absolute = operation === "to-absolute"
|
|
16
|
+
|| operation !== "to-relative" && LETTERS.includes(note.ast.name);
|
|
17
|
+
const midi = note.resolvedMidi + (operation === "semitone-up" ? 1 : operation === "semitone-down" ? -1 : 0);
|
|
18
|
+
const tonic = tonality2Midi(note.keySignature, 4);
|
|
19
|
+
if (![midi, tonic, note.ast.octave, note.ast.jeOctaveOffset].every(Number.isSafeInteger))
|
|
20
|
+
fail(note.ast.sourceSpan);
|
|
21
|
+
const spelling = operation === "semitone-down" ? FLAT_NOTE_NAMES : SHARP_NOTE_NAMES;
|
|
22
|
+
let name, acc, octave;
|
|
23
|
+
if (operation === "to-absolute") {
|
|
24
|
+
const tonicLetter = LETTERS.indexOf(note.keySignature[0]);
|
|
25
|
+
const base = tonicLetter < 0 ? LETTERS.indexOf(SHARP_NOTE_NAMES[mod(tonic, 12)][0]) : tonicLetter;
|
|
26
|
+
const degree = Number(note.ast.name) - 1;
|
|
27
|
+
name = LETTERS[(base + degree) % 7];
|
|
28
|
+
const tonicAcc = mod(tonic - NoteNameMap[LETTERS[base]] + 6, 12) - 6;
|
|
29
|
+
octave = Math.floor((tonic - tonicAcc) / 12) - 1 + Math.floor((base + degree) / 7)
|
|
30
|
+
+ note.ast.octave + note.ast.jeOctaveOffset;
|
|
31
|
+
acc = accidental(midi - ((octave + 1) * 12 + NoteNameMap[name]));
|
|
32
|
+
}
|
|
33
|
+
else if (operation === "to-relative") {
|
|
34
|
+
const ast = note.ast;
|
|
35
|
+
const key = LETTERS.includes(note.keySignature[0]) ? note.keySignature
|
|
36
|
+
: SHARP_NOTE_NAMES[mod(tonic, 12)] + (Math.floor(tonic / 12) - 1);
|
|
37
|
+
const relative = resolveLetterNameToJianpu(ast.name, ast.acc, ast.octave + ast.jeOctaveOffset, key);
|
|
38
|
+
if (!relative)
|
|
39
|
+
fail(ast.sourceSpan);
|
|
40
|
+
({ renderName: name, renderAcc: acc, renderOctave: octave } = relative);
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const value = absolute ? midi : midi - tonic;
|
|
44
|
+
const pitch = spelling[mod(value, 12)];
|
|
45
|
+
name = absolute ? pitch[0] : String(LETTERS.indexOf(pitch[0]) + 1);
|
|
46
|
+
acc = pitch.slice(1);
|
|
47
|
+
octave = Math.floor(value / 12) - (absolute ? 1 : 0);
|
|
48
|
+
}
|
|
49
|
+
return { name, acc, octave: octave - note.ast.jeOctaveOffset, absolute };
|
|
50
|
+
}
|
|
51
|
+
/** 仅在函数参数的顶层保护低八度逗号;已有大括号内的简写无需再包装 */
|
|
52
|
+
function needsBraces(masked, ast) {
|
|
53
|
+
for (let parent = ast.parent; parent; parent = parent.parent) {
|
|
54
|
+
const call = readCall(masked, parent.sourceSpan.start, parent.sourceSpan.end);
|
|
55
|
+
if (!call)
|
|
56
|
+
continue;
|
|
57
|
+
const arg = call.args.find(item => item.valueSpan.start <= ast.sourceSpan.start
|
|
58
|
+
&& item.valueSpan.end >= ast.sourceSpan.end);
|
|
59
|
+
if (!arg)
|
|
60
|
+
continue;
|
|
61
|
+
let depth = 0;
|
|
62
|
+
for (let position = arg.valueSpan.start; position < ast.sourceSpan.start; position++) {
|
|
63
|
+
if (masked[position] === '"')
|
|
64
|
+
position = findClosingQuote(masked, position, arg.valueSpan.end);
|
|
65
|
+
else if (masked[position] === "{")
|
|
66
|
+
depth++;
|
|
67
|
+
else if (masked[position] === "}")
|
|
68
|
+
depth--;
|
|
69
|
+
}
|
|
70
|
+
return depth === 0;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
/** edits 始终引用原文;参数只改值,默认参数只在当前调用覆盖,避免影响其他音符 */
|
|
75
|
+
export function rewriteNotes(source, notes, operation, ranges) {
|
|
76
|
+
const { maskedSource: masked } = preprocessSource(source);
|
|
77
|
+
const edits = [];
|
|
78
|
+
const tokens = [];
|
|
79
|
+
function replace(span, text) {
|
|
80
|
+
if (source.slice(span.start, span.end) === text)
|
|
81
|
+
return;
|
|
82
|
+
if (source.slice(span.start, span.end) !== masked.slice(span.start, span.end))
|
|
83
|
+
fail(span);
|
|
84
|
+
edits.push({ span, text });
|
|
85
|
+
}
|
|
86
|
+
for (const note of notes) {
|
|
87
|
+
const { ast } = note;
|
|
88
|
+
const span = ast.sourceSpan;
|
|
89
|
+
const call = readCall(masked, span.start, span.end);
|
|
90
|
+
const explicit = call?.span.end === span.end && call.closeParenSpan;
|
|
91
|
+
const already = operation === "to-absolute" && LETTERS.includes(ast.name)
|
|
92
|
+
|| operation === "to-relative" && /^[1-7]$/.test(ast.name);
|
|
93
|
+
if (!explicit)
|
|
94
|
+
tokens.push({ span, text: source.slice(span.start, span.end) });
|
|
95
|
+
if (ranges && !ranges.some(range => range.start <= span.start && range.end >= span.end))
|
|
96
|
+
continue;
|
|
97
|
+
if (note.resolvedMidi === null || already)
|
|
98
|
+
continue;
|
|
99
|
+
const pitch = targetPitch(note, operation);
|
|
100
|
+
const octaveText = pitch.absolute ? String(pitch.octave)
|
|
101
|
+
: pitch.octave < 0 ? ",".repeat(-pitch.octave) : "'".repeat(pitch.octave);
|
|
102
|
+
const nameText = (acc, octave) => pitch.absolute
|
|
103
|
+
? pitch.name + acc + octave : acc + pitch.name + octave;
|
|
104
|
+
if (!explicit) {
|
|
105
|
+
let text = nameText(pitch.acc, octaveText);
|
|
106
|
+
if (text.includes(",") && needsBraces(masked, ast))
|
|
107
|
+
text = `{${text}}`;
|
|
108
|
+
tokens[tokens.length - 1].text = text;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const slots = new Map();
|
|
112
|
+
call.args.forEach((arg, index) => {
|
|
113
|
+
if (arg.valueSpan.start === arg.valueSpan.end)
|
|
114
|
+
return;
|
|
115
|
+
slots.set(arg.nameSpan ? masked.slice(arg.nameSpan.start, arg.nameSpan.end).toLowerCase() : index, arg.valueSpan);
|
|
116
|
+
});
|
|
117
|
+
const nameSlot = slots.get("name") ?? slots.get(0);
|
|
118
|
+
const accSlot = slots.get("acc") ?? slots.get(1);
|
|
119
|
+
const octaveSlot = slots.get("octave") ?? slots.get(2);
|
|
120
|
+
const additions = [];
|
|
121
|
+
function write(slot, name, value) {
|
|
122
|
+
const text = !value || value.includes(",")
|
|
123
|
+
|| slot && source[slot.start] === '"' ? quote(value) : value;
|
|
124
|
+
if (slot)
|
|
125
|
+
replace(slot, text);
|
|
126
|
+
else
|
|
127
|
+
additions.push(`${name}=${text}`);
|
|
128
|
+
}
|
|
129
|
+
write(nameSlot, "name", nameText(accSlot ? "" : pitch.acc, octaveSlot ? "" : octaveText));
|
|
130
|
+
if (accSlot)
|
|
131
|
+
write(accSlot, "acc", pitch.acc);
|
|
132
|
+
else if (ast.hasDefaultAcc)
|
|
133
|
+
additions.push('acc=""');
|
|
134
|
+
if (octaveSlot)
|
|
135
|
+
replace(octaveSlot, String(pitch.octave));
|
|
136
|
+
if (additions.length) {
|
|
137
|
+
const at = call.closeParenSpan.start;
|
|
138
|
+
const prefix = masked.slice(call.openParenSpan.end, at).trimEnd();
|
|
139
|
+
replace({ start: at, end: at }, (prefix ? prefix.endsWith(",") ? " " : ", " : "") + additions.join(", "));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// 相邻原子改写后仍须是两个音符,例如 C4D4 -> C4 2,不能合并成 C42。
|
|
143
|
+
tokens.sort((left, right) => left.span.start - right.span.start);
|
|
144
|
+
for (let index = 0; index < tokens.length - 1; index++) {
|
|
145
|
+
const left = tokens[index], right = tokens[index + 1];
|
|
146
|
+
if (left.span.end !== right.span.start || left.text.endsWith("}"))
|
|
147
|
+
continue;
|
|
148
|
+
const parsed = parseNoteName(left.text + right.text);
|
|
149
|
+
if (parsed instanceof Diagnostic || parsed.next !== left.text.length) {
|
|
150
|
+
if (left.text !== source.slice(left.span.start, left.span.end))
|
|
151
|
+
left.text += " ";
|
|
152
|
+
else
|
|
153
|
+
right.text = " " + right.text;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const token of tokens)
|
|
157
|
+
replace(token.span, token.text);
|
|
158
|
+
edits.sort((left, right) => left.span.start - right.span.start);
|
|
159
|
+
for (let index = 1; index < edits.length; index++) {
|
|
160
|
+
if (edits[index - 1].span.end > edits[index].span.start)
|
|
161
|
+
fail(edits[index].span);
|
|
162
|
+
}
|
|
163
|
+
return edits;
|
|
164
|
+
}
|
|
@@ -19,6 +19,11 @@ export declare class PageFunction extends ASTFunctionNode {
|
|
|
19
19
|
description: string;
|
|
20
20
|
type: "string";
|
|
21
21
|
default: string;
|
|
22
|
+
} | {
|
|
23
|
+
name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
type: "number";
|
|
26
|
+
default: number;
|
|
22
27
|
})[];
|
|
23
28
|
};
|
|
24
29
|
readonly config: PageConfig | null;
|
|
@@ -45,15 +45,15 @@ class PageNumberAttachment {
|
|
|
45
45
|
export class PageFunction extends ASTFunctionNode {
|
|
46
46
|
static def = {
|
|
47
47
|
name: ["page"],
|
|
48
|
-
description: "
|
|
48
|
+
description: "设置文档页面",
|
|
49
49
|
details: `\
|
|
50
50
|
~~~jpfun
|
|
51
51
|
@page(
|
|
52
52
|
width=794px, height=1123px, top=48px, bottom=48px,
|
|
53
|
-
left=40px, right=40px, gap=1em, numbering="1/1"
|
|
53
|
+
left=40px, right=40px, gap=1em, numbering="1/1", fillRatio=0.5
|
|
54
54
|
)
|
|
55
55
|
~~~
|
|
56
|
-
尺寸接受 \`px\` 或 \`em
|
|
56
|
+
尺寸接受 \`px\` 或 \`em\`。页边距和行距须非负;左右边距之和须小于页宽,固定页高时上下边距之和须小于页高。`,
|
|
57
57
|
allowExtraArgs: false,
|
|
58
58
|
args: [
|
|
59
59
|
{ name: "width", description: "页面宽度,须大于左右页边距之和", type: "length", default: px(DEFAULT_PAGE_CONFIG.width) },
|
|
@@ -66,6 +66,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
66
66
|
// 单个 1 取当前页;多个 1 中最后一个取总页数
|
|
67
67
|
{ name: "numbering", description: "页码格式,空值隐藏;`1` 显示当前页,`1/1` 显示当前页/总页数", type: "string", default: "" },
|
|
68
68
|
{ name: "font", description: "页码字体,空值沿用当前字体设置", type: "string", default: "" },
|
|
69
|
+
{ name: "fillRatio", description: "自然宽度达到该比例后撑满", type: "number", default: DEFAULT_PAGE_CONFIG.fillRatio },
|
|
69
70
|
],
|
|
70
71
|
};
|
|
71
72
|
config;
|
|
@@ -86,6 +87,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
86
87
|
}
|
|
87
88
|
ctx.documentDeclarations["page"] = true;
|
|
88
89
|
const values = this.getArgValue(args, ctx);
|
|
90
|
+
const fillRatio = values.pop();
|
|
89
91
|
this.font = values.pop() || ctx.variables.font;
|
|
90
92
|
const numbering = values.pop();
|
|
91
93
|
const [width, height, top, bottom, left, right, gap] = values.map(value => ctx.length2px(value));
|
|
@@ -109,6 +111,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
109
111
|
marginLeft: left,
|
|
110
112
|
marginRight: right,
|
|
111
113
|
lineGap: gap,
|
|
114
|
+
fillRatio,
|
|
112
115
|
});
|
|
113
116
|
this.numbering = numbering;
|
|
114
117
|
}
|
|
@@ -124,8 +127,8 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
124
127
|
toString() {
|
|
125
128
|
if (!this.config)
|
|
126
129
|
return "@page()";
|
|
127
|
-
const { width, height, marginTop, marginBottom, marginLeft, marginRight, lineGap } = this.config;
|
|
128
|
-
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)})`;
|
|
130
|
+
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})`;
|
|
129
132
|
}
|
|
130
133
|
}
|
|
131
134
|
export const PageNode = PageFunction;
|