jpfun 0.2.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/README.md +69 -11
- 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 +39 -10
- package/dist/functions/beam/auto.js +32 -15
- 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 +9 -7
- package/dist/functions/dash/index.js +33 -1
- package/dist/functions/default.js +14 -7
- package/dist/functions/div/index.d.ts +16 -0
- package/dist/functions/div/index.js +45 -17
- package/dist/functions/dot/index.js +11 -3
- package/dist/functions/grace/index.d.ts +8 -11
- package/dist/functions/grace/index.js +82 -76
- package/dist/functions/head/index.d.ts +2 -0
- package/dist/functions/head/index.js +492 -0
- package/dist/functions/key/index.js +97 -3
- 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 +45 -65
- package/dist/functions/page/index.d.ts +9 -2
- package/dist/functions/page/index.js +50 -4
- package/dist/functions/set/index.js +37 -29
- package/dist/functions/stack/index.js +26 -22
- 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 -2
- package/dist/functions/text/index.js +76 -24
- package/dist/functions/tie/index.js +102 -127
- package/dist/functions/tuplet/index.d.ts +2 -0
- package/dist/functions/tuplet/index.js +211 -0
- package/dist/functions/up/index.d.ts +1 -0
- package/dist/functions/up/index.js +245 -171
- package/dist/functions/voice/index.js +86 -68
- 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 +65 -59
- 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 +18 -20
- package/dist/lowering/loweringContext.js +132 -94
- package/dist/lowering/track.d.ts +1 -1
- package/dist/lowering/track.js +1 -1
- package/dist/lowering/types.d.ts +49 -12
- package/dist/lowering/types.js +9 -3
- 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 +93 -10
- package/package.json +10 -6
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ornament } from "../ornament.js";
|
|
2
|
+
export const trSymbol = {
|
|
3
|
+
name: "tr",
|
|
4
|
+
description: "颤音:本音与调内上方二度交替",
|
|
5
|
+
shapes: [
|
|
6
|
+
{
|
|
7
|
+
path: [
|
|
8
|
+
{ op: "M", x: 3.17992, y: 5.46383 },
|
|
9
|
+
{ op: "L", x: 3.61827, y: 3.91711 },
|
|
10
|
+
{ op: "C", cx1: 3.10024, cy1: 3.49862, cx2: 2.52947, cy2: 3.29304, x: 1.90541, y: 3.29304 },
|
|
11
|
+
{ op: "C", cx1: 1.50051, cy1: 3.29304, cx2: 1.18833, cy2: 3.40589, x: 0.95611, y: 3.63812 },
|
|
12
|
+
{ op: "C", cx1: 0.72361, cy1: 3.87061, cx2: 0.59743, cy2: 4.14281, x: 0.57105, y: 4.46152 },
|
|
13
|
+
{ op: "C", cx1: 0.57105, cy1: 4.91944, cx2: 0.71707, cy2: 5.25147, x: 1.00913, y: 5.47036 },
|
|
14
|
+
{ op: "C", cx1: 1.30118, cy1: 5.68954, cx2: 1.61988, cy2: 5.79586, x: 1.95843, y: 5.79586 },
|
|
15
|
+
{ op: "C", cx1: 1.9916, cy1: 5.79586, cx2: 2.03157, cy2: 5.8157, x: 2.06475, y: 5.84888 },
|
|
16
|
+
{ op: "C", cx1: 2.24394, cy1: 5.82904, cx2: 2.61568, cy2: 5.69632, x: 3.17992, y: 5.46383 },
|
|
17
|
+
{ op: "L", x: 3.17992, y: 5.46383 },
|
|
18
|
+
{ op: "L", x: 3.17992, y: 5.46383 },
|
|
19
|
+
{ op: "Z" },
|
|
20
|
+
{ op: "M", x: 6.579, y: 9.87204 },
|
|
21
|
+
{ op: "L", x: 8.40472, y: 3.75096 },
|
|
22
|
+
{ op: "C", cx1: 8.40472, cy1: 3.59162, cx2: 8.35167, cy2: 3.51193, x: 8.23884, y: 3.51193 },
|
|
23
|
+
{ op: "C", cx1: 7.98647, cy1: 3.51193, cx2: 7.4554, cy2: 3.77761, x: 6.63883, y: 4.3152 },
|
|
24
|
+
{ op: "C", cx1: 5.82223, cy1: 4.84655, cx2: 5.37735, cy2: 5.15845, x: 5.30448, y: 5.25145 },
|
|
25
|
+
{ op: "C", cx1: 5.33764, cy1: 5.21147, cx2: 5.39068, cy2: 5.15845, x: 5.46383, y: 5.0853 },
|
|
26
|
+
{ op: "C", cx1: 6.13441, cy1: 6.06127, cx2: 6.47294, cy2: 6.98417, x: 6.47294, y: 7.86061 },
|
|
27
|
+
{ op: "C", cx1: 6.41313, cy1: 8.53088, cx2: 6.18062, cy2: 9.12181, x: 5.7692, y: 9.61968 },
|
|
28
|
+
{ op: "C", cx1: 5.3643, cy1: 10.11758, cx2: 4.85308, cy2: 10.3634, x: 4.24233, y: 10.3634 },
|
|
29
|
+
{ op: "C", cx1: 3.71778, cy1: 10.3634, cx2: 3.25306, cy2: 10.23719, x: 2.83483, y: 9.99138 },
|
|
30
|
+
{ op: "C", cx1: 2.42314, cy1: 9.74586, cx2: 2.20396, cy2: 9.39396, x: 2.17759, y: 8.92271 },
|
|
31
|
+
{ op: "C", cx1: 2.21077, cy1: 8.80986, cx2: 2.23061, cy2: 8.66384, x: 2.23061, y: 8.48436 },
|
|
32
|
+
{ op: "L", x: 2.85469, y: 6.0944 },
|
|
33
|
+
{ op: "C", cx1: 2.58903, cy1: 6.2339, cx2: 2.21729, cy2: 6.30702, x: 1.73924, y: 6.30702 },
|
|
34
|
+
{ op: "C", cx1: 1.22829, cy1: 6.2804, cx2: 0.81007, cy2: 6.1012, x: 0.48458, y: 5.76917 },
|
|
35
|
+
{ op: "C", cx1: 0.15935, cy1: 5.43065, cx2: 0, cy2: 4.97926, x: 0, y: 4.4082 },
|
|
36
|
+
{ op: "C", cx1: 0.03318, cy1: 3.89045, cx2: 0.20586, cy2: 3.46543, x: 0.53107, y: 3.13369 },
|
|
37
|
+
{ op: "C", cx1: 0.8563, cy1: 2.80166, cx2: 1.32782, cy2: 2.63579, x: 1.95843, y: 2.63579 },
|
|
38
|
+
{ op: "C", cx1: 2.55585, cy1: 2.63579, cx2: 3.17992, cy2: 2.87481, x: 3.83716, y: 3.34606 },
|
|
39
|
+
{ op: "L", x: 4.89277, y: 0 },
|
|
40
|
+
{ op: "L", x: 6.85119, y: 0 },
|
|
41
|
+
{ op: "L", x: 5.41053, y: 4.51453 },
|
|
42
|
+
{ op: "C", cx1: 6.28694, cy1: 3.83064, cx2: 7.02389, cy2: 3.34606, x: 7.62128, y: 3.06054 },
|
|
43
|
+
{ op: "C", cx1: 8.21899, cy1: 2.77502, cx2: 8.86943, cy2: 2.63579, x: 9.57321, y: 2.63579 },
|
|
44
|
+
{ op: "C", cx1: 10.05773, cy1: 2.63579, cx2: 10.30358, cy2: 2.83484, x: 10.30358, y: 3.23974 },
|
|
45
|
+
{ op: "C", cx1: 10.30358, cy1: 3.44559, cx2: 10.17738, cy2: 3.89046, x: 10.09093, y: 4.18931 },
|
|
46
|
+
{ op: "C", cx1: 10.7283, cy1: 3.15354, cx2: 11.45195, cy2: 2.63579, x: 12.26199, y: 2.63579 },
|
|
47
|
+
{ op: "C", cx1: 12.6272, cy1: 2.63579, cx2: 12.91925, cy2: 2.76849, x: 13.14497, y: 3.03417 },
|
|
48
|
+
{ op: "C", cx1: 13.37717, cy1: 3.29957, cx2: 13.49001, cy2: 3.61175, x: 13.49001, y: 3.97014 },
|
|
49
|
+
{ op: "C", cx1: 13.49001, cy1: 4.62739, cx2: 13.09844, cy2: 5.03228, x: 12.53419, y: 5.03228 },
|
|
50
|
+
{ op: "C", cx1: 12.16899, cy1: 5.03228, cx2: 11.85709, cy2: 4.79327, x: 11.80382, y: 4.29536 },
|
|
51
|
+
{ op: "C", cx1: 11.89029, cy1: 3.91031, cx2: 11.98982, cy2: 3.55191, x: 11.98982, y: 3.34606 },
|
|
52
|
+
{ op: "C", cx1: 11.98982, cy1: 3.27291, cx2: 11.94329, cy2: 3.23973, x: 11.85709, y: 3.23973 },
|
|
53
|
+
{ op: "C", cx1: 11.74403, cy1: 3.23973, cx2: 11.53162, cy2: 3.34606, x: 11.20637, y: 3.56522 },
|
|
54
|
+
{ op: "C", cx1: 10.90752, cy1: 3.7708, cx2: 10.61547, cy2: 4.11615, x: 10.31666, y: 4.61405 },
|
|
55
|
+
{ op: "C", cx1: 10.01808, cy1: 5.11195, cx2: 9.77253, cy2: 5.69632, x: 9.57321, y: 6.36662 },
|
|
56
|
+
{ op: "L", x: 8.45798, y: 9.87203 },
|
|
57
|
+
{ op: "L", x: 6.57899, y: 9.87203 },
|
|
58
|
+
{ op: "L", x: 6.57899, y: 9.87203 },
|
|
59
|
+
{ op: "L", x: 6.579, y: 9.87204 },
|
|
60
|
+
{ op: "Z" },
|
|
61
|
+
{ op: "M", x: 5.05893, y: 5.62971 },
|
|
62
|
+
{ op: "L", x: 3.94348, y: 8.92274 },
|
|
63
|
+
{ op: "C", cx1: 3.94348, cy1: 9.0288, cx2: 3.92364, cy2: 9.12179, x: 3.89045, y: 9.19495 },
|
|
64
|
+
{ op: "C", cx1: 3.89045, cy1: 9.48047, cx2: 4.06285, cy2: 9.65287, x: 4.40821, y: 9.70617 },
|
|
65
|
+
{ op: "C", cx1: 5.404, cy1: 9.22134, cx2: 5.9019, cy2: 8.51787, x: 5.9019, y: 7.58841 },
|
|
66
|
+
{ op: "C", cx1: 5.9019, cy1: 6.99751, cx2: 5.62318, cy2: 6.34681, x: 5.05893, y: 5.62972 },
|
|
67
|
+
{ op: "L", x: 5.05893, y: 5.62972 },
|
|
68
|
+
{ op: "L", x: 5.05893, y: 5.62971 },
|
|
69
|
+
{ op: "Z" },
|
|
70
|
+
],
|
|
71
|
+
style: { fill: "#000" },
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
emitPlayback: emitter => emitter.affectFollowing(ornament({ body: [0, 1], repeat: true })),
|
|
75
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PathCommand } from "../../render/types.js";
|
|
2
|
+
/** 已按字号固化的四分音符字形 */
|
|
3
|
+
export interface PreparedQuarterNote {
|
|
4
|
+
w: number;
|
|
5
|
+
h: number;
|
|
6
|
+
/** 符干中线到字形左边界的距离;它是这个字形的竖笔,对齐点取这里 */
|
|
7
|
+
stemCenterX: number;
|
|
8
|
+
commands: readonly PathCommand[];
|
|
9
|
+
/** 传给 PathTransform 的等比缩放,倾斜的符头不能被非等比拉伸 */
|
|
10
|
+
scale: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function prepareQuarterNote(size: number): PreparedQuarterNote;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// 轮廓直接照抄成品字模,坐标单位是 em;y=0 是符干顶端,y=HEIGHT 是符头底端(对齐文字基线)
|
|
2
|
+
const GLYPH_EM = 11;
|
|
3
|
+
const WIDTH = 3.864;
|
|
4
|
+
const HEIGHT = 10.88;
|
|
5
|
+
const STEM_LEFT = 3.304;
|
|
6
|
+
const QUARTER_NOTE_COMMANDS = [
|
|
7
|
+
{ op: "M", x: 3.864, y: 8.5 },
|
|
8
|
+
{ op: "Q", cx: 3.864, cy: 9.228, x: 3.5, y: 9.76 },
|
|
9
|
+
{ op: "Q", cx: 3.136, cy: 10.292, x: 2.555, y: 10.586 },
|
|
10
|
+
{ op: "Q", cx: 1.974, cy: 10.88, x: 1.302, y: 10.88 },
|
|
11
|
+
{ op: "Q", cx: 0.826, cy: 10.88, x: 0.413, y: 10.621 },
|
|
12
|
+
{ op: "Q", cx: 0, cy: 10.362, x: 0, y: 9.844 },
|
|
13
|
+
{ op: "Q", cx: 0, cy: 9.34, x: 0.364, y: 8.899 },
|
|
14
|
+
{ op: "Q", cx: 0.728, cy: 8.458, x: 1.288, y: 8.185 },
|
|
15
|
+
{ op: "Q", cx: 1.848, cy: 7.912, x: 2.422, y: 7.912 },
|
|
16
|
+
{ op: "Q", cx: 2.968, cy: 7.912, x: 3.304, y: 8.094 },
|
|
17
|
+
{ op: "L", x: 3.304, y: 0 },
|
|
18
|
+
{ op: "L", x: 3.864, y: 0 },
|
|
19
|
+
{ op: "Z" },
|
|
20
|
+
];
|
|
21
|
+
export function prepareQuarterNote(size) {
|
|
22
|
+
const scale = size / GLYPH_EM;
|
|
23
|
+
return {
|
|
24
|
+
w: WIDTH * scale,
|
|
25
|
+
h: HEIGHT * scale,
|
|
26
|
+
stemCenterX: (STEM_LEFT + WIDTH) / 2 * scale,
|
|
27
|
+
commands: QUARTER_NOTE_COMMANDS,
|
|
28
|
+
scale,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { TemporalNodeBase } from "../../lowering/types.js";
|
|
2
2
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
3
|
+
import { prepareQuarterNote } from "./glyph.js";
|
|
3
4
|
class TempoFunction extends ASTFunctionNode {
|
|
4
5
|
static def = {
|
|
5
6
|
name: ["tempo"],
|
|
@@ -12,12 +13,23 @@ class TempoFunction extends ASTFunctionNode {
|
|
|
12
13
|
type: "number",
|
|
13
14
|
default: null,
|
|
14
15
|
},
|
|
16
|
+
{
|
|
17
|
+
name: "size",
|
|
18
|
+
type: "length",
|
|
19
|
+
default: {
|
|
20
|
+
value: 1,
|
|
21
|
+
unit: "em",
|
|
22
|
+
},
|
|
23
|
+
},
|
|
15
24
|
],
|
|
16
25
|
};
|
|
17
26
|
bpm;
|
|
27
|
+
size;
|
|
18
28
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
19
29
|
super(sourceSpan, parent);
|
|
20
|
-
[
|
|
30
|
+
const [bpm, size] = this.getArgValue(args, ctx);
|
|
31
|
+
this.bpm = bpm;
|
|
32
|
+
this.size = ctx.length2px(size);
|
|
21
33
|
}
|
|
22
34
|
loweringEnter() {
|
|
23
35
|
return [new TempoTemporalNode(this)];
|
|
@@ -26,11 +38,50 @@ class TempoFunction extends ASTFunctionNode {
|
|
|
26
38
|
}
|
|
27
39
|
export const TempoNode = TempoFunction;
|
|
28
40
|
class TempoTemporalNode extends TemporalNodeBase {
|
|
41
|
+
/** 字号和 bpm 在解析时已经固化,字形、文本、样式随之不再变 */
|
|
42
|
+
glyph;
|
|
43
|
+
text;
|
|
44
|
+
style;
|
|
45
|
+
glyphY = 0;
|
|
46
|
+
textX = 0;
|
|
47
|
+
textBaselineY = 0;
|
|
29
48
|
constructor(ast) {
|
|
30
49
|
super();
|
|
31
50
|
this.ast = ast;
|
|
51
|
+
this.glyph = prepareQuarterNote(ast.size);
|
|
52
|
+
this.text = `= ${ast.bpm}`;
|
|
53
|
+
this.style = { fontSize: ast.size, fill: "#000" };
|
|
54
|
+
// 非法值不入时间状态,也不告知播放系统,效果是保持上一个速度
|
|
55
|
+
if (Number.isFinite(ast.bpm) && ast.bpm > 0)
|
|
56
|
+
this.playbackState = { bpm: ast.bpm };
|
|
57
|
+
this.initLayoutBox();
|
|
32
58
|
}
|
|
33
59
|
onTimeState(state) {
|
|
34
|
-
|
|
60
|
+
const bpm = this.playbackState?.bpm;
|
|
61
|
+
if (bpm !== undefined)
|
|
62
|
+
state.bpm = bpm;
|
|
63
|
+
}
|
|
64
|
+
prepareLayout(context) {
|
|
65
|
+
const glyph = this.glyph;
|
|
66
|
+
const metrics = context.textMeasurer.measureText(this.text, this.style);
|
|
67
|
+
// 符头底端坐在文字基线上;符干比文字高时整块文字下移
|
|
68
|
+
const textTop = Math.max(0, glyph.h - metrics.baseline);
|
|
69
|
+
this.textX = glyph.w + this.ast.size * 0.26;
|
|
70
|
+
this.textBaselineY = textTop + metrics.baseline;
|
|
71
|
+
this.glyphY = this.textBaselineY - glyph.h;
|
|
72
|
+
this.box.w = this.textX + metrics.w;
|
|
73
|
+
this.box.h = textTop + metrics.h;
|
|
74
|
+
this.box.anchor = glyph.stemCenterX;
|
|
75
|
+
// Tempo 与同轨文字按等号所在的文字行对齐;符干只向上扩展占用,不参与对齐轴。
|
|
76
|
+
this.box.visualAxis = textTop + metrics.h / 2;
|
|
77
|
+
}
|
|
78
|
+
paint(painter) {
|
|
79
|
+
painter.drawPath(this.glyph.commands, { fill: "#000" }, {
|
|
80
|
+
x: this.box.x,
|
|
81
|
+
y: this.box.y + this.glyphY,
|
|
82
|
+
scaleX: this.glyph.scale,
|
|
83
|
+
scaleY: this.glyph.scale,
|
|
84
|
+
});
|
|
85
|
+
painter.drawText(this.text, this.box.x + this.textX, this.box.y + this.textBaselineY, this.style);
|
|
35
86
|
}
|
|
36
87
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
2
|
+
import { ErrorDiagnostic } from "../../diagnostic.js";
|
|
3
|
+
import { findClosingQuote, removeQuote } from "../../parser/parse-utils/string-utils.js";
|
|
2
4
|
import { DEFAULT_KEY, TemporalNodeBase } from "../../lowering/types.js";
|
|
3
5
|
class TextFunction extends ASTFunctionNode {
|
|
4
6
|
static def = {
|
|
5
7
|
name: ["text"],
|
|
6
|
-
description: "
|
|
7
|
-
example: `@text(进入主题)
|
|
8
|
+
description: "文本标记。括号内可直接换行,支持 left、center、right 对齐",
|
|
9
|
+
example: `@text(进入主题)
|
|
10
|
+
语法糖:直接写双引号字符串,如 "进入主题";可跨行,引号未配对则不生效`,
|
|
8
11
|
allowExtraArgs: false,
|
|
9
12
|
args: [
|
|
10
13
|
{
|
|
@@ -18,48 +21,97 @@ class TextFunction extends ASTFunctionNode {
|
|
|
18
21
|
value: 1,
|
|
19
22
|
unit: "em",
|
|
20
23
|
},
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "lineheight",
|
|
27
|
+
type: "number",
|
|
28
|
+
default: 1.25, // 相对自身字号的倍数
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
name: "align",
|
|
32
|
+
type: "string",
|
|
33
|
+
default: "left",
|
|
21
34
|
}
|
|
22
35
|
],
|
|
23
36
|
};
|
|
24
|
-
|
|
37
|
+
static deSugarAtom(source, start, end) {
|
|
38
|
+
if (source[start] !== '"')
|
|
39
|
+
return null;
|
|
40
|
+
const close = findClosingQuote(source, start, end);
|
|
41
|
+
if (close < 0)
|
|
42
|
+
return null; // 未闭合就不去糖,与预处理“配对才算字符串”一致
|
|
43
|
+
const node = {
|
|
44
|
+
kind: "call",
|
|
45
|
+
typed: true,
|
|
46
|
+
name: "text",
|
|
47
|
+
args: new Map([[0, removeQuote(source.slice(start, close + 1))]]),
|
|
48
|
+
span: { start, end: close + 1 },
|
|
49
|
+
syntaxKind: "string",
|
|
50
|
+
};
|
|
51
|
+
return { next: close + 1, node };
|
|
52
|
+
}
|
|
53
|
+
lines;
|
|
25
54
|
size;
|
|
55
|
+
lineAdvance;
|
|
56
|
+
align;
|
|
26
57
|
constructor(sourceSpan, args, ctx, parent = null) {
|
|
27
58
|
super(sourceSpan, parent);
|
|
28
|
-
const [
|
|
29
|
-
|
|
30
|
-
|
|
59
|
+
const [text, size, lineHeight, align] = this.getArgValue(args, ctx);
|
|
60
|
+
if (align !== "left" && align !== "center" && align !== "right") {
|
|
61
|
+
throw new ErrorDiagnostic("E_TEXT_INVALID_ALIGN", "@text 的 align 参数必须是 'left'、'center' 或 'right'", sourceSpan);
|
|
62
|
+
}
|
|
63
|
+
this.lines = text.split(/\r?\n/);
|
|
64
|
+
this.size = ctx.length2px(size);
|
|
65
|
+
this.lineAdvance = lineHeight * this.size;
|
|
66
|
+
this.align = align;
|
|
31
67
|
}
|
|
32
68
|
loweringEnter() { return [new TextTemporalNode(this)]; }
|
|
33
|
-
toString() {
|
|
69
|
+
toString() {
|
|
70
|
+
const lineHeight = this.size === 0 ? 1.25 : this.lineAdvance / this.size;
|
|
71
|
+
return `@text(${JSON.stringify(this.lines.join("\n"))}, size=${this.size}px, lineheight=${lineHeight}, align=${this.align})`;
|
|
72
|
+
}
|
|
34
73
|
}
|
|
35
74
|
export const TextNode = TextFunction;
|
|
36
75
|
class TextTemporalNode extends TemporalNodeBase {
|
|
76
|
+
style;
|
|
37
77
|
textBaselineY = 0;
|
|
38
78
|
constructor(ast) {
|
|
39
79
|
super();
|
|
40
80
|
this.ast = ast;
|
|
41
|
-
this.T = 0;
|
|
42
81
|
this.mergeKey = DEFAULT_KEY;
|
|
82
|
+
this.style = { fontSize: ast.size, fill: "#000", textAlign: ast.align };
|
|
43
83
|
this.initLayoutBox();
|
|
44
84
|
}
|
|
45
85
|
prepareLayout(context) {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this.box.
|
|
54
|
-
this.box.
|
|
55
|
-
this.
|
|
56
|
-
|
|
57
|
-
|
|
86
|
+
const { lines, align, lineAdvance } = this.ast;
|
|
87
|
+
const measure = (text) => context.textMeasurer.measureText(text, this.style);
|
|
88
|
+
const first = measure(lines[0]);
|
|
89
|
+
let width = first.w;
|
|
90
|
+
for (let i = 1; i < lines.length; i++)
|
|
91
|
+
width = Math.max(width, measure(lines[i]).w);
|
|
92
|
+
this.box.w = width;
|
|
93
|
+
this.box.h = (lines.length - 1) * lineAdvance + first.h;
|
|
94
|
+
this.box.visualAxis = this.box.h / 2;
|
|
95
|
+
this.textBaselineY = first.baseline;
|
|
96
|
+
if (align === "center")
|
|
97
|
+
this.box.anchor = width / 2;
|
|
98
|
+
else if (align === "right") {
|
|
99
|
+
const lastChar = [...lines[0]].at(-1);
|
|
100
|
+
this.box.anchor = width - (lastChar ? measure(lastChar).w / 2 : 0);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// 左对齐时锚点落在首字符中心,单字标记才能像音符一样对准宿主
|
|
104
|
+
const firstChar = [...lines[0]][0];
|
|
105
|
+
this.box.anchor = firstChar ? measure(firstChar).w / 2 : 0;
|
|
106
|
+
}
|
|
58
107
|
}
|
|
59
108
|
paint(painter) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
109
|
+
const { lines, align, lineAdvance } = this.ast;
|
|
110
|
+
const x = this.box.x + (align === "center" ? this.box.w / 2 : align === "right" ? this.box.w : 0);
|
|
111
|
+
let y = this.box.y + this.textBaselineY;
|
|
112
|
+
for (const line of lines) {
|
|
113
|
+
painter.drawText(line, x, y, this.style);
|
|
114
|
+
y += lineAdvance;
|
|
115
|
+
}
|
|
64
116
|
}
|
|
65
117
|
}
|