jpfun 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +200 -200
- package/README.md +191 -133
- 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 +24 -7
- package/dist/functions/ASTtypes.js +35 -16
- package/dist/functions/adjust/index.d.ts +2 -0
- package/dist/functions/adjust/index.js +193 -0
- package/dist/functions/bar/index.d.ts +8 -0
- package/dist/functions/bar/index.js +48 -19
- package/dist/functions/beam/auto.js +35 -18
- package/dist/functions/beam/index.js +10 -6
- package/dist/functions/beam/layout.d.ts +3 -2
- package/dist/functions/beam/layout.js +42 -64
- package/dist/functions/box/index.js +35 -34
- package/dist/functions/br/index.js +14 -12
- package/dist/functions/dash/index.js +37 -5
- package/dist/functions/default.js +14 -7
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +51 -23
- package/dist/functions/dot/index.js +16 -8
- package/dist/functions/grace/index.d.ts +8 -18
- package/dist/functions/grace/index.js +98 -99
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +492 -0
- package/dist/functions/key/index.js +98 -5
- package/dist/functions/meter/index.d.ts +2 -0
- package/dist/functions/meter/index.js +154 -0
- package/dist/functions/note/accidentals.d.ts +24 -3
- package/dist/functions/note/accidentals.js +31 -1
- package/dist/functions/note/index.js +60 -80
- package/dist/functions/page/index.d.ts +9 -2
- package/dist/functions/page/index.js +50 -4
- package/dist/functions/set/index.js +42 -34
- package/dist/functions/stack/index.js +39 -34
- package/dist/functions/symbol/index.d.ts +21 -0
- package/dist/functions/symbol/index.js +94 -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 +22 -0
- package/dist/functions/symbol/shape.js +48 -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/f.d.ts +2 -0
- package/dist/functions/symbol/symbols/f.js +55 -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/mordent.d.ts +2 -0
- package/dist/functions/symbol/symbols/mordent.js +43 -0
- package/dist/functions/symbol/symbols/p.d.ts +2 -0
- package/dist/functions/symbol/symbols/p.js +61 -0
- package/dist/functions/symbol/symbols/tr.d.ts +2 -0
- package/dist/functions/symbol/symbols/tr.js +75 -0
- package/dist/functions/tempo/glyph.d.ts +12 -0
- package/dist/functions/tempo/glyph.js +30 -0
- package/dist/functions/tempo/index.js +53 -3
- package/dist/functions/text/index.js +78 -26
- package/dist/functions/tie/index.js +103 -128
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +211 -0
- package/dist/functions/{over → up}/index.d.ts +1 -0
- package/dist/functions/up/index.js +370 -0
- package/dist/functions/voice/index.js +125 -108
- package/dist/functions/volta/index.d.ts +2 -0
- package/dist/functions/volta/index.js +227 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/jpfun.min.js +143 -0
- package/dist/layout/engine.d.ts +4 -11
- package/dist/layout/engine.js +100 -66
- package/dist/layout/model.d.ts +8 -1
- package/dist/layout/model.js +16 -6
- package/dist/layout/path.d.ts +19 -0
- package/dist/layout/path.js +101 -0
- package/dist/layout/types.d.ts +51 -18
- package/dist/layout/types.js +3 -1
- package/dist/lowering/loweringContext.d.ts +19 -23
- package/dist/lowering/loweringContext.js +179 -187
- package/dist/lowering/track.d.ts +33 -10
- package/dist/lowering/track.js +6 -6
- package/dist/lowering/types.d.ts +57 -43
- package/dist/lowering/types.js +20 -4
- 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 +8 -0
- package/dist/parser/parse-utils/string-utils.js +20 -0
- package/dist/parser/parserContext.d.ts +8 -5
- package/dist/parser/parserContext.js +38 -33
- package/dist/parser/preprocess.js +17 -11
- package/dist/playback/compile.d.ts +18 -0
- package/dist/playback/compile.js +401 -0
- package/dist/playback/event.d.ts +91 -0
- package/dist/playback/event.js +87 -0
- package/dist/playback/time.d.ts +14 -0
- package/dist/playback/time.js +81 -0
- package/dist/playback/types.d.ts +128 -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 +17 -0
- package/dist/render/text.js +35 -0
- package/dist/render/types.d.ts +1 -1
- package/grammar.md +217 -122
- package/package.json +14 -9
- package/dist/functions/over/index.js +0 -307
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
2
|
+
import { Fraction } from "../../fraction.js";
|
|
3
|
+
import { ANCHOR_KEY, TemporalNodeBase } from "../../lowering/types.js";
|
|
4
|
+
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
5
|
+
import { JIANPU_NUMBER_FONT } from "../../render/text.js";
|
|
6
|
+
class MeterFunction extends ASTFunctionNode {
|
|
7
|
+
static def = {
|
|
8
|
+
name: ["meter"],
|
|
9
|
+
description: "设置拍号",
|
|
10
|
+
example: `@meter(6, 8) 设置 6/8 拍号`,
|
|
11
|
+
allowExtraArgs: false,
|
|
12
|
+
args: [
|
|
13
|
+
{
|
|
14
|
+
name: "num",
|
|
15
|
+
type: "number",
|
|
16
|
+
default: null
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
name: "den",
|
|
20
|
+
type: "number",
|
|
21
|
+
default: null
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "size",
|
|
25
|
+
type: "length",
|
|
26
|
+
default: { value: 0.7, unit: "em" },
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
};
|
|
30
|
+
numerator;
|
|
31
|
+
denominator;
|
|
32
|
+
measureDuration;
|
|
33
|
+
size;
|
|
34
|
+
strict;
|
|
35
|
+
constructor(span, args, ctx, parent = null) {
|
|
36
|
+
super(span, parent);
|
|
37
|
+
let [num, den, size] = this.getArgValue(args, ctx);
|
|
38
|
+
if (!Number.isSafeInteger(num) || !Number.isSafeInteger(num * 4) || num <= 0
|
|
39
|
+
|| !Number.isSafeInteger(den) || den <= 0) {
|
|
40
|
+
const message = `@meter 的分子和分母必须是正整数`;
|
|
41
|
+
if (ctx.strict)
|
|
42
|
+
throw new ErrorDiagnostic("E_METER_INVALID", message, span);
|
|
43
|
+
ctx.diagnostics.push(new WarningDiagnostic("W_METER_INVALID", `${message},已回落为 4/4`, span));
|
|
44
|
+
num = den = 4;
|
|
45
|
+
}
|
|
46
|
+
this.numerator = num;
|
|
47
|
+
this.denominator = den;
|
|
48
|
+
// QN 以四分音符为 1;拍号不修改音符自身时值
|
|
49
|
+
this.measureDuration = new Fraction(num * 4, den);
|
|
50
|
+
this.size = ctx.length2px(size);
|
|
51
|
+
this.strict = ctx.strict;
|
|
52
|
+
}
|
|
53
|
+
loweringEnter() {
|
|
54
|
+
return [new MeterTemporal(this)];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 校验小节拍数
|
|
58
|
+
* 第一小节特殊规则:只允许不满拍,但不能超拍
|
|
59
|
+
*/
|
|
60
|
+
static loweringFinalize = (result) => {
|
|
61
|
+
const measureStart = new Fraction();
|
|
62
|
+
const measureLength = new Fraction();
|
|
63
|
+
let active = null;
|
|
64
|
+
let validationStarted = false;
|
|
65
|
+
// 诊断范围从上一边界之后延伸到当前关闭边界
|
|
66
|
+
let sourceStart = 0;
|
|
67
|
+
const closeMeasure = (end, sourceEnd) => {
|
|
68
|
+
if (!active || end.equals(measureStart))
|
|
69
|
+
return;
|
|
70
|
+
// Fraction 运算会原地修改接收者,不能直接对时间线字段做减法
|
|
71
|
+
measureLength.copyFrom(end).sub(measureStart);
|
|
72
|
+
if (!measureLength.equals(active.measureDuration)
|
|
73
|
+
&& (validationStarted || measureLength.compare(active.measureDuration) > 0)) {
|
|
74
|
+
const message = `小节时长为 ${measureLength} QN,不满足 ${active.numerator}/${active.denominator} 拍号`;
|
|
75
|
+
const span = { start: sourceStart, end: sourceEnd };
|
|
76
|
+
if (active.strict)
|
|
77
|
+
throw new ErrorDiagnostic("E_METER_MISMATCH", message, span);
|
|
78
|
+
result.diagnostics.push(new WarningDiagnostic("W_METER_MISMATCH", message, span));
|
|
79
|
+
}
|
|
80
|
+
measureStart.copyFrom(end);
|
|
81
|
+
};
|
|
82
|
+
for (const column of result.columns) {
|
|
83
|
+
const time = column[0].t;
|
|
84
|
+
const meter = column.find(node => node instanceof MeterTemporal);
|
|
85
|
+
if (meter) {
|
|
86
|
+
closeMeasure(time, meter.ast.sourceSpan.start);
|
|
87
|
+
active = meter.ast;
|
|
88
|
+
measureStart.copyFrom(time);
|
|
89
|
+
sourceStart = meter.ast.sourceSpan.end;
|
|
90
|
+
}
|
|
91
|
+
const anchor = column.find(node => node.mergeKey === ANCHOR_KEY);
|
|
92
|
+
if (anchor) {
|
|
93
|
+
closeMeasure(time, anchor.ast.sourceSpan.end);
|
|
94
|
+
sourceStart = anchor.ast.sourceSpan.end;
|
|
95
|
+
validationStarted = true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (active) {
|
|
99
|
+
const span = result.columns.at(-1)?.at(-1)?.ast.sourceSpan ?? active.sourceSpan;
|
|
100
|
+
closeMeasure(result.duration, span.end);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
toString() {
|
|
104
|
+
return `@meter(${this.numerator}, ${this.denominator})`;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export const MeterNode = MeterFunction;
|
|
108
|
+
class MeterTemporal extends TemporalNodeBase {
|
|
109
|
+
style;
|
|
110
|
+
numeratorBaseline = 0;
|
|
111
|
+
denominatorBaseline = 0;
|
|
112
|
+
lineY = 0;
|
|
113
|
+
lineHeight = 0;
|
|
114
|
+
constructor(ast) {
|
|
115
|
+
super();
|
|
116
|
+
this.ast = ast;
|
|
117
|
+
this.style = {
|
|
118
|
+
fontSize: ast.size,
|
|
119
|
+
fontFamily: JIANPU_NUMBER_FONT,
|
|
120
|
+
textAlign: "center",
|
|
121
|
+
fill: "#000",
|
|
122
|
+
};
|
|
123
|
+
this.initLayoutBox();
|
|
124
|
+
}
|
|
125
|
+
emitPlayback(emitter) {
|
|
126
|
+
emitter.emit({
|
|
127
|
+
kind: "time-signature",
|
|
128
|
+
at: emitter.start,
|
|
129
|
+
numerator: this.ast.numerator,
|
|
130
|
+
denominator: this.ast.denominator,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
prepareLayout(context) {
|
|
134
|
+
const num = context.textMeasurer.measureText(String(this.ast.numerator), this.style);
|
|
135
|
+
const den = context.textMeasurer.measureText(String(this.ast.denominator), this.style);
|
|
136
|
+
const gap = this.ast.size * 0.08;
|
|
137
|
+
const padding = this.ast.size * 0.08;
|
|
138
|
+
this.lineHeight = Math.max(1, this.ast.size * 0.06);
|
|
139
|
+
this.numeratorBaseline = num.baseline;
|
|
140
|
+
this.lineY = num.h + gap;
|
|
141
|
+
this.denominatorBaseline = this.lineY + this.lineHeight + gap + den.baseline;
|
|
142
|
+
this.box.w = Math.max(num.w, den.w) + padding * 2;
|
|
143
|
+
this.box.h = this.lineY + this.lineHeight + gap + den.h;
|
|
144
|
+
this.box.anchor = this.box.w / 2;
|
|
145
|
+
// 分数线是拍号的视觉中心
|
|
146
|
+
this.box.visualAxis = this.lineY + this.lineHeight / 2;
|
|
147
|
+
}
|
|
148
|
+
paint(painter) {
|
|
149
|
+
const center = this.box.x + this.box.anchor;
|
|
150
|
+
painter.drawText(String(this.ast.numerator), center, this.box.y + this.numeratorBaseline, this.style);
|
|
151
|
+
painter.drawRect(this.box.x, this.box.y + this.lineY, this.box.w, this.lineHeight, { fill: "#000" });
|
|
152
|
+
painter.drawText(String(this.ast.denominator), center, this.box.y + this.denominatorBaseline, this.style);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PathCommand } from "../../render/types.js";
|
|
1
|
+
import type { Painter, PathCommand } from "../../render/types.js";
|
|
2
2
|
export interface PreparedAccidental {
|
|
3
3
|
w: number;
|
|
4
4
|
h: number;
|
|
@@ -6,5 +6,26 @@ export interface PreparedAccidental {
|
|
|
6
6
|
strokeWidth: number;
|
|
7
7
|
commands: readonly PathCommand[];
|
|
8
8
|
}
|
|
9
|
-
/**
|
|
10
|
-
export
|
|
9
|
+
/** 已经定位到宿主盒局部坐标的升降号 */
|
|
10
|
+
export interface PlacedAccidental {
|
|
11
|
+
shape: PreparedAccidental;
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 把升降号串按同一基线依次排在 x 右侧
|
|
17
|
+
* @param size 宿主数字的字号
|
|
18
|
+
* @param baselineY 宿主文字基线在宿主盒局部坐标中的 y
|
|
19
|
+
* @param gap 相邻升降号之间的间隔;末尾也留一份,宿主据此得到 right
|
|
20
|
+
* @returns right 是下一个字形的起点,top 是最高处(可能为负,宿主自行决定是否下移)
|
|
21
|
+
*/
|
|
22
|
+
export declare function placeAccidentals(symbols: string, size: number, x: number, baselineY: number, gap: number): {
|
|
23
|
+
placed: PlacedAccidental[];
|
|
24
|
+
right: number;
|
|
25
|
+
top: number;
|
|
26
|
+
};
|
|
27
|
+
/** commands 是 0..1 归一化坐标,必须配回字形自己的 w/h 才不会画歪 */
|
|
28
|
+
export declare function paintAccidental(painter: Painter, { shape, x, y }: PlacedAccidental, origin: {
|
|
29
|
+
x: number;
|
|
30
|
+
y: number;
|
|
31
|
+
}, color: string): void;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
/** 字形相对数字字号的缩放,线宽则取与数字笔画相当的固定比例 */
|
|
2
2
|
const GLYPH_SCALE = 0.72;
|
|
3
3
|
const STROKE_RATIO = 0.06;
|
|
4
|
+
/** 升降号写在宿主基线之上多少,贴住数字左上角 */
|
|
5
|
+
const RAISE_RATIO = 0.42;
|
|
4
6
|
const ACCIDENTALS = {
|
|
5
7
|
"#": {
|
|
6
8
|
w: 0.46,
|
|
@@ -45,7 +47,7 @@ const ACCIDENTALS = {
|
|
|
45
47
|
},
|
|
46
48
|
};
|
|
47
49
|
/** size 是数字字号,升降号自己决定缩小多少 */
|
|
48
|
-
|
|
50
|
+
function prepareAccidental(symbol, size) {
|
|
49
51
|
const definition = ACCIDENTALS[symbol];
|
|
50
52
|
if (!definition)
|
|
51
53
|
return null;
|
|
@@ -58,3 +60,31 @@ export function prepareAccidental(symbol, size) {
|
|
|
58
60
|
commands: definition.commands,
|
|
59
61
|
};
|
|
60
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* 把升降号串按同一基线依次排在 x 右侧
|
|
65
|
+
* @param size 宿主数字的字号
|
|
66
|
+
* @param baselineY 宿主文字基线在宿主盒局部坐标中的 y
|
|
67
|
+
* @param gap 相邻升降号之间的间隔;末尾也留一份,宿主据此得到 right
|
|
68
|
+
* @returns right 是下一个字形的起点,top 是最高处(可能为负,宿主自行决定是否下移)
|
|
69
|
+
*/
|
|
70
|
+
export function placeAccidentals(symbols, size, x, baselineY, gap) {
|
|
71
|
+
const placed = [];
|
|
72
|
+
const raise = size * RAISE_RATIO;
|
|
73
|
+
let right = x;
|
|
74
|
+
let top = 0;
|
|
75
|
+
for (const symbol of symbols) {
|
|
76
|
+
const shape = prepareAccidental(symbol, size);
|
|
77
|
+
if (!shape)
|
|
78
|
+
continue;
|
|
79
|
+
const y = baselineY - shape.baseline - raise;
|
|
80
|
+
placed.push({ shape, x: right, y });
|
|
81
|
+
if (y < top)
|
|
82
|
+
top = y;
|
|
83
|
+
right += shape.w + gap;
|
|
84
|
+
}
|
|
85
|
+
return { placed, right, top };
|
|
86
|
+
}
|
|
87
|
+
/** commands 是 0..1 归一化坐标,必须配回字形自己的 w/h 才不会画歪 */
|
|
88
|
+
export function paintAccidental(painter, { shape, x, y }, origin, color) {
|
|
89
|
+
painter.drawPath(shape.commands, { stroke: color, strokeWidth: shape.strokeWidth }, { x: origin.x + x, y: origin.y + y, scaleX: shape.w, scaleY: shape.h });
|
|
90
|
+
}
|
|
@@ -1,25 +1,26 @@
|
|
|
1
1
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
2
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { parseNoteName } from "./noteNameFSM.js";
|
|
4
|
-
import {
|
|
4
|
+
import { JIANPU_NUMBER_FONT } from "../../render/text.js";
|
|
5
|
+
import { paintAccidental, placeAccidentals } from "./accidentals.js";
|
|
5
6
|
class NoteFunction extends ASTFunctionNode {
|
|
6
7
|
static def = {
|
|
7
8
|
name: ["note", "n"],
|
|
8
9
|
description: "创建音符",
|
|
9
|
-
example: `@note(name, acc, octave, color)
|
|
10
|
-
参数说明:
|
|
11
|
-
- name: [必填]音符名,可以是大写字母或者数字,不允许小写字母(会和降号冲突)。此参数写法有语法糖,见下。
|
|
12
|
-
其中 0 是休止符,8 是隐形占位,9 是只打拍不发音的节拍记号(显示为 X),这三者都不显示升降号和八度点。
|
|
13
|
-
- acc: [可选]的额外升降号字符串,例如 "##" 表示再升两个半音,"b" 表示再降一个半音。
|
|
14
|
-
- octave: [可选]八度,类型为数字。如果 name 是字母,则此项代表绝对八度;如果是数字,则此项代表相对八度。
|
|
15
|
-
|
|
16
|
-
语法糖:[音名][升降号][八度] 音名和升降号可以交换
|
|
17
|
-
例:A3# === @note(A3#) === @note(A, #, 3)
|
|
18
|
-
也支持 A99##bn 的写法。可以设置 note.octave 改变默认的绝对八度
|
|
19
|
-
|
|
20
|
-
支持 数字音名, 此时 octave 为相对八度
|
|
21
|
-
支持使用数字音名时使用相对八度,如 "1,," 代表在当前基准音(由上下文属性"1=?"决定)的基础上降低两倍八度,"1'" 代表在当前基准音的基础上提高一个八度
|
|
22
|
-
例:1#' === @note(1#') === @note(1, #, 1)。此时支持升降号写音名前面,如 #1'
|
|
10
|
+
example: `@note(name, acc, octave, color)
|
|
11
|
+
参数说明:
|
|
12
|
+
- name: [必填]音符名,可以是大写字母或者数字,不允许小写字母(会和降号冲突)。此参数写法有语法糖,见下。
|
|
13
|
+
其中 0 是休止符,8 是隐形占位,9 是只打拍不发音的节拍记号(显示为 X),这三者都不显示升降号和八度点。
|
|
14
|
+
- acc: [可选]的额外升降号字符串,例如 "##" 表示再升两个半音,"b" 表示再降一个半音。
|
|
15
|
+
- octave: [可选]八度,类型为数字。如果 name 是字母,则此项代表绝对八度;如果是数字,则此项代表相对八度。
|
|
16
|
+
|
|
17
|
+
语法糖:[音名][升降号][八度] 音名和升降号可以交换
|
|
18
|
+
例:A3# === @note(A3#) === @note(A, #, 3)
|
|
19
|
+
也支持 A99##bn 的写法。可以设置 note.octave 改变默认的绝对八度
|
|
20
|
+
|
|
21
|
+
支持 数字音名, 此时 octave 为相对八度
|
|
22
|
+
支持使用数字音名时使用相对八度,如 "1,," 代表在当前基准音(由上下文属性"1=?"决定)的基础上降低两倍八度,"1'" 代表在当前基准音的基础上提高一个八度
|
|
23
|
+
例:1#' === @note(1#') === @note(1, #, 1)。此时支持升降号写音名前面,如 #1'
|
|
23
24
|
`,
|
|
24
25
|
allowExtraArgs: false,
|
|
25
26
|
args: [
|
|
@@ -107,28 +108,21 @@ class NoteFunction extends ASTFunctionNode {
|
|
|
107
108
|
}
|
|
108
109
|
}
|
|
109
110
|
export const NoteNode = NoteFunction;
|
|
110
|
-
import { TemporalNodeBase } from "../../lowering/types.js";
|
|
111
|
-
import { resolveLetterNameToJianpu, resolveNoteMidi } from "../../parser/parse-utils/note-utils.js";
|
|
111
|
+
import { DEFAULT_KEY, DEFAULT_VELOCITY, TemporalNodeBase } from "../../lowering/types.js";
|
|
112
|
+
import { createDiatonicTranspose, resolveLetterNameToJianpu, resolveNoteMidi, } from "../../parser/parse-utils/note-utils.js";
|
|
112
113
|
// 8 是隐形占位,9 是只打拍不发音的节拍记号,空串表示不绘制
|
|
113
114
|
const NOTE_GLYPH = { "8": "", "9": "X" };
|
|
114
|
-
function createNumberStyle(ast) {
|
|
115
|
-
return {
|
|
116
|
-
fontSize: ast.size,
|
|
117
|
-
fontFamily: '"Cascadia Mono", Consolas, "Liberation Mono", "Noto Sans Mono", monospace',
|
|
118
|
-
fontWeight: 400,
|
|
119
|
-
textAlign: "center",
|
|
120
|
-
fill: ast.color,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
115
|
class NoteTemporalNode extends TemporalNodeBase {
|
|
124
116
|
// 时间固化后的参数
|
|
125
|
-
activeBpm = null; // 用于播放的时候的速度
|
|
126
117
|
resolvedMidi = null; // MIDI音高 name是数字则需要在onTimeState中基于当前调性偏移 播放的音高
|
|
118
|
+
transposeDiatonic;
|
|
127
119
|
name; // 数字
|
|
128
120
|
acc; // 升降号
|
|
129
121
|
octave; // 绝对值为点的个数
|
|
130
122
|
/** prepareLayout 生成的局部升降号位置 */
|
|
131
123
|
accidentals = [];
|
|
124
|
+
/** 字号和颜色在解析时已经固化,样式随之不再变 */
|
|
125
|
+
numberStyle;
|
|
132
126
|
/** 上八度点属于主体,下八度点通过通用装饰流排在其他下方符号之后 */
|
|
133
127
|
upperOctaveDotY = [];
|
|
134
128
|
lowerOctaveDotY = [];
|
|
@@ -137,39 +131,25 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
137
131
|
constructor(ast) {
|
|
138
132
|
super();
|
|
139
133
|
this.ast = ast;
|
|
140
|
-
this.T
|
|
141
|
-
this.
|
|
142
|
-
this.type = 2 /* ColType.DEFAULT */;
|
|
134
|
+
this.T.set(1);
|
|
135
|
+
this.mergeKey = DEFAULT_KEY;
|
|
143
136
|
this.name = ast.name;
|
|
144
137
|
this.acc = ast.acc;
|
|
145
138
|
this.octave = ast.octave;
|
|
139
|
+
this.numberStyle = {
|
|
140
|
+
fontSize: ast.size,
|
|
141
|
+
fontFamily: JIANPU_NUMBER_FONT,
|
|
142
|
+
fontWeight: 400,
|
|
143
|
+
textAlign: "center",
|
|
144
|
+
fill: ast.color,
|
|
145
|
+
};
|
|
146
146
|
this.initLayoutBox();
|
|
147
147
|
}
|
|
148
148
|
prepareLayout(context) {
|
|
149
|
-
this.accidentals.length = 0;
|
|
150
149
|
this.upperOctaveDotY.length = 0;
|
|
151
150
|
this.lowerOctaveDotY.length = 0;
|
|
152
151
|
const { size, color } = this.ast;
|
|
153
|
-
const
|
|
154
|
-
const mainMetrics = context.textMeasurer.measureText("0", numberStyle);
|
|
155
|
-
// 升降号贴近数字左上角,但不参与数字中心 anchor 的计算
|
|
156
|
-
// 缩小右侧间隔会在全局 anchor 不变时把升降号向右移动
|
|
157
|
-
const accidentalGap = size * 0.01;
|
|
158
|
-
const accidentalRaise = size * 0.42;
|
|
159
|
-
const accidentalShapes = [];
|
|
160
|
-
// 把每个升降字符转换为 note 私有的局部路径
|
|
161
|
-
for (const accidental of this.acc) {
|
|
162
|
-
const shape = prepareAccidental(accidental, size);
|
|
163
|
-
if (shape)
|
|
164
|
-
accidentalShapes.push(shape);
|
|
165
|
-
}
|
|
166
|
-
let accidentalWidth = 0;
|
|
167
|
-
// 汇总升降号区域宽度,为数字单元保留固定起点
|
|
168
|
-
for (const shape of accidentalShapes) {
|
|
169
|
-
if (accidentalWidth > 0)
|
|
170
|
-
accidentalWidth += accidentalGap;
|
|
171
|
-
accidentalWidth += shape.w;
|
|
172
|
-
}
|
|
152
|
+
const mainMetrics = context.textMeasurer.measureText("0", this.numberStyle);
|
|
173
153
|
const dotCount = Math.abs(this.octave);
|
|
174
154
|
this.octaveDotRadius = size * 0.065;
|
|
175
155
|
const dotGap = size * 0.08;
|
|
@@ -177,14 +157,17 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
177
157
|
const octaveSpace = dotCount === 0
|
|
178
158
|
? 0
|
|
179
159
|
: dotCount * this.octaveDotRadius * 2 + (dotCount - 1) * dotGap + dotGap;
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
160
|
+
const mainY = this.octave > 0 ? octaveSpace : 0;
|
|
161
|
+
this.numberY = mainY + mainMetrics.baseline;
|
|
162
|
+
// 升降号贴近数字左上角,按同一 baseline 排在数字左侧;
|
|
163
|
+
// 缩小右侧间隔会在全局 anchor 不变时把升降号向右移动
|
|
164
|
+
const accidentalGap = size * 0.01;
|
|
165
|
+
const { placed, right: mainX } = placeAccidentals(this.acc, size, 0, this.numberY, accidentalGap);
|
|
166
|
+
this.accidentals = placed;
|
|
183
167
|
this.box.w = mainX + mainMetrics.w;
|
|
184
|
-
this.box.h =
|
|
168
|
+
this.box.h = mainY + mainMetrics.h;
|
|
185
169
|
this.box.anchor = mainX + mainMetrics.w / 2;
|
|
186
170
|
this.box.visualAxis = mainY + mainMetrics.h / 2;
|
|
187
|
-
this.numberY = mainY + mainMetrics.baseline;
|
|
188
171
|
// 升降号和附点参与完整盒布局,但不计入数字主体范围,减时线因此只画在数字下
|
|
189
172
|
this.ports["body.left"] = { x: this.box.anchor - mainMetrics.w / 2, y: this.box.visualAxis };
|
|
190
173
|
this.ports["body.right"] = { x: this.box.anchor + mainMetrics.w / 2, y: this.box.visualAxis };
|
|
@@ -193,21 +176,10 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
193
176
|
x: this.box.w,
|
|
194
177
|
y: mainY + mainMetrics.h * 0.64,
|
|
195
178
|
};
|
|
196
|
-
// 升降号位于完整盒的左侧区域,但不会进入核心有效范围
|
|
197
|
-
let accidentalX = 0;
|
|
198
|
-
// 所有升降号按同一数字 baseline 排列在数字左侧
|
|
199
|
-
for (const shape of accidentalShapes) {
|
|
200
|
-
this.accidentals.push({
|
|
201
|
-
shape,
|
|
202
|
-
x: accidentalX,
|
|
203
|
-
y: this.numberY - shape.baseline - accidentalRaise,
|
|
204
|
-
});
|
|
205
|
-
accidentalX += shape.w + accidentalGap;
|
|
206
|
-
}
|
|
207
179
|
if (this.octave > 0) {
|
|
208
180
|
// 上八度点从靠近数字的位置向上依次排列
|
|
209
181
|
for (let i = 0; i < dotCount; i++) {
|
|
210
|
-
this.upperOctaveDotY.push(
|
|
182
|
+
this.upperOctaveDotY.push(mainY - dotGap - this.octaveDotRadius - i * dotStep);
|
|
211
183
|
}
|
|
212
184
|
return;
|
|
213
185
|
}
|
|
@@ -242,18 +214,11 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
242
214
|
}
|
|
243
215
|
paint(painter) {
|
|
244
216
|
const color = this.ast.color;
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
painter.drawPath(accidental.shape.commands, { stroke: color, strokeWidth: accidental.shape.strokeWidth }, {
|
|
248
|
-
x: this.box.x + accidental.x,
|
|
249
|
-
y: this.box.y + accidental.y,
|
|
250
|
-
scaleX: accidental.shape.w,
|
|
251
|
-
scaleY: accidental.shape.h,
|
|
252
|
-
});
|
|
253
|
-
}
|
|
217
|
+
for (const accidental of this.accidentals)
|
|
218
|
+
paintAccidental(painter, accidental, this.box, color);
|
|
254
219
|
const glyph = NOTE_GLYPH[this.name] ?? this.name;
|
|
255
220
|
if (glyph) {
|
|
256
|
-
painter.drawText(glyph, this.box.x + this.box.anchor, this.box.y + this.numberY, numberStyle);
|
|
221
|
+
painter.drawText(glyph, this.box.x + this.box.anchor, this.box.y + this.numberY, this.numberStyle);
|
|
257
222
|
}
|
|
258
223
|
// 上八度点属于主体绘制,不参与下方装饰流
|
|
259
224
|
for (const y of this.upperOctaveDotY) {
|
|
@@ -261,9 +226,10 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
261
226
|
}
|
|
262
227
|
}
|
|
263
228
|
onTimeState(state) {
|
|
264
|
-
const keySignature =
|
|
265
|
-
this.
|
|
229
|
+
const keySignature = state.keySignature;
|
|
230
|
+
this.playbackState = { bpm: state.bpm, velocity: state.velocity };
|
|
266
231
|
this.resolvedMidi = resolveNoteMidi(this.name, this.acc, this.octave, keySignature);
|
|
232
|
+
this.transposeDiatonic = createDiatonicTranspose(this.name, this.acc, this.octave, keySignature);
|
|
267
233
|
// 数字音名本身就是简谱显示形式,直接保留原始数字和相对八度
|
|
268
234
|
if (this.name >= "0" && this.name <= "9") {
|
|
269
235
|
if (this.name === "0" || this.name >= "8") {
|
|
@@ -281,4 +247,18 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
281
247
|
}
|
|
282
248
|
}
|
|
283
249
|
}
|
|
250
|
+
emitPlayback(emitter) {
|
|
251
|
+
if (this.resolvedMidi === null || emitter.end.compare(emitter.start) <= 0)
|
|
252
|
+
return;
|
|
253
|
+
const noteId = emitter.nextNoteId();
|
|
254
|
+
emitter.emit({
|
|
255
|
+
kind: "note-on",
|
|
256
|
+
at: emitter.start,
|
|
257
|
+
noteId,
|
|
258
|
+
midi: this.resolvedMidi,
|
|
259
|
+
velocity: this.playbackState?.velocity ?? DEFAULT_VELOCITY,
|
|
260
|
+
transpose: this.transposeDiatonic,
|
|
261
|
+
});
|
|
262
|
+
emitter.emit({ kind: "note-off", at: emitter.end, noteId });
|
|
263
|
+
}
|
|
284
264
|
}
|
|
@@ -1,19 +1,26 @@
|
|
|
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;
|
|
17
24
|
constructor(sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
|
|
18
25
|
loweringEnter(ctx: LoweringContext): never[];
|
|
19
26
|
}
|
|
@@ -2,11 +2,46 @@ import { DEFAULT_PAGE_CONFIG, normalizePageConfig, } from "../../layout/page.js"
|
|
|
2
2
|
import { Diagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
3
3
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
4
|
const px = (value) => ({ value, unit: "px" });
|
|
5
|
+
/** 页码字号,同时也是放得下页码所需的最小下边距 */
|
|
6
|
+
export const PAGE_NUMBER_FONT_SIZE = 16;
|
|
7
|
+
const PAGE_NUMBER_STYLE = { fontSize: PAGE_NUMBER_FONT_SIZE, textAlign: "center", fill: "#606060" };
|
|
8
|
+
/** 模式串里的 1 是计数符号,最后一个取总页数,其余取当前页 */
|
|
9
|
+
function formatPageNumber(pattern, current, total) {
|
|
10
|
+
const last = pattern.lastIndexOf("1");
|
|
11
|
+
return pattern.replace(/1/g, (_, index) => String(index === last ? total : current));
|
|
12
|
+
}
|
|
13
|
+
/** 浮在每页下边距带里的页码,不属于任何谱面行,因此不申报 Track 占用 */
|
|
14
|
+
class PageNumberAttachment {
|
|
15
|
+
sourceSpan;
|
|
16
|
+
pattern;
|
|
17
|
+
marginBottom;
|
|
18
|
+
layer = "foreground";
|
|
19
|
+
constructor(sourceSpan, pattern, marginBottom) {
|
|
20
|
+
this.sourceSpan = sourceSpan;
|
|
21
|
+
this.pattern = pattern;
|
|
22
|
+
this.marginBottom = marginBottom;
|
|
23
|
+
}
|
|
24
|
+
createGeometry(context) {
|
|
25
|
+
const marks = context.pages.map((page, index) => {
|
|
26
|
+
const text = formatPageNumber(this.pattern, index + 1, context.pages.length);
|
|
27
|
+
const { w, h, baseline } = context.textMeasurer.measureText(text, PAGE_NUMBER_STYLE);
|
|
28
|
+
const center = page.y + page.h - this.marginBottom / 2;
|
|
29
|
+
return { text, x: page.x + page.w / 2, y: center - h / 2 + baseline, w, h, baseline };
|
|
30
|
+
});
|
|
31
|
+
return {
|
|
32
|
+
regions: marks.map(({ x, y, w, h, baseline }) => ({ x: x - w / 2, y: y - baseline, w, h })),
|
|
33
|
+
paint(painter) {
|
|
34
|
+
for (const mark of marks)
|
|
35
|
+
painter.drawText(mark.text, mark.x, mark.y, PAGE_NUMBER_STYLE);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
5
40
|
export class PageFunction extends ASTFunctionNode {
|
|
6
41
|
static def = {
|
|
7
42
|
name: ["page"],
|
|
8
|
-
description: "
|
|
9
|
-
example: "@page(width=794px, height=1123px, top=48px, bottom=48px, left=40px, right=40px, gap=1em)",
|
|
43
|
+
description: "设置文档页面尺寸、边距、最小谱面行间距和页码",
|
|
44
|
+
example: "@page(width=794px, height=1123px, top=48px, bottom=48px, left=40px, right=40px, gap=1em, numbering=\"1/1\")",
|
|
10
45
|
allowExtraArgs: false,
|
|
11
46
|
args: [
|
|
12
47
|
{ name: "width", type: "length", default: px(DEFAULT_PAGE_CONFIG.width) },
|
|
@@ -16,9 +51,12 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
16
51
|
{ name: "left", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginLeft) },
|
|
17
52
|
{ name: "right", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginRight) },
|
|
18
53
|
{ name: "gap", type: "length", default: { value: 1, unit: "em" } },
|
|
54
|
+
// 模式串里的 1 是计数符号,最后一个取总页数
|
|
55
|
+
{ name: "numbering", type: "string", default: "" },
|
|
19
56
|
],
|
|
20
57
|
};
|
|
21
58
|
config;
|
|
59
|
+
numbering = "";
|
|
22
60
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
23
61
|
super(sourceSpan, parent);
|
|
24
62
|
if (ctx.scopeDepth !== 0) {
|
|
@@ -33,6 +71,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
33
71
|
}
|
|
34
72
|
ctx.documentDeclarations["page"] = true;
|
|
35
73
|
const values = this.getArgValue(args, ctx);
|
|
74
|
+
const numbering = values.pop();
|
|
36
75
|
const [width, height, top, bottom, left, right, gap] = values.map(value => ctx.length2px(value));
|
|
37
76
|
if (!Number.isFinite(width) || width <= 0)
|
|
38
77
|
throw Diagnostic.error.InvalidPageConfig("@page 的 width 必须是正有限长度", sourceSpan);
|
|
@@ -44,6 +83,8 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
44
83
|
throw Diagnostic.error.InvalidPageConfig("@page 的左右边距没有留下可用内容宽度", sourceSpan);
|
|
45
84
|
if (height > 0 && top + bottom >= height)
|
|
46
85
|
throw Diagnostic.error.InvalidPageConfig("@page 的上下边距没有留下可用内容高度", sourceSpan);
|
|
86
|
+
if (numbering && bottom < PAGE_NUMBER_FONT_SIZE)
|
|
87
|
+
throw Diagnostic.error.InvalidPageConfig(`@page 的 bottom 至少需要 ${PAGE_NUMBER_FONT_SIZE}px 才能放下页码`, sourceSpan);
|
|
47
88
|
this.config = normalizePageConfig({
|
|
48
89
|
width,
|
|
49
90
|
height,
|
|
@@ -53,10 +94,15 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
53
94
|
marginRight: right,
|
|
54
95
|
lineGap: gap,
|
|
55
96
|
});
|
|
97
|
+
this.numbering = numbering;
|
|
56
98
|
}
|
|
57
99
|
loweringEnter(ctx) {
|
|
58
|
-
if (ctx
|
|
59
|
-
|
|
100
|
+
if (!ctx || !this.config)
|
|
101
|
+
return [];
|
|
102
|
+
ctx.setPageConfig(this.config);
|
|
103
|
+
if (this.numbering) {
|
|
104
|
+
ctx.addAttachment(new PageNumberAttachment(this.sourceSpan, this.numbering, this.config.marginBottom));
|
|
105
|
+
}
|
|
60
106
|
return [];
|
|
61
107
|
}
|
|
62
108
|
}
|