jpfun 0.1.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 +201 -0
- package/README.md +134 -0
- package/dist/diagnostic.d.ts +27 -0
- package/dist/diagnostic.js +141 -0
- package/dist/functions/ASTtypes.d.ts +115 -0
- package/dist/functions/ASTtypes.js +187 -0
- package/dist/functions/bar/index.d.ts +2 -0
- package/dist/functions/bar/index.js +180 -0
- package/dist/functions/beam/auto.d.ts +4 -0
- package/dist/functions/beam/auto.js +202 -0
- package/dist/functions/beam/index.d.ts +2 -0
- package/dist/functions/beam/index.js +54 -0
- package/dist/functions/beam/layout.d.ts +9 -0
- package/dist/functions/beam/layout.js +233 -0
- package/dist/functions/box/index.d.ts +2 -0
- package/dist/functions/box/index.js +202 -0
- package/dist/functions/br/index.d.ts +2 -0
- package/dist/functions/br/index.js +83 -0
- package/dist/functions/dash/index.d.ts +2 -0
- package/dist/functions/dash/index.js +61 -0
- package/dist/functions/default.d.ts +2 -0
- package/dist/functions/default.js +32 -0
- package/dist/functions/div/index.d.ts +50 -0
- package/dist/functions/div/index.js +190 -0
- package/dist/functions/dot/index.d.ts +2 -0
- package/dist/functions/dot/index.js +160 -0
- package/dist/functions/grace/index.d.ts +84 -0
- package/dist/functions/grace/index.js +464 -0
- package/dist/functions/key/index.d.ts +2 -0
- package/dist/functions/key/index.js +40 -0
- package/dist/functions/note/accidentals.d.ts +10 -0
- package/dist/functions/note/accidentals.js +60 -0
- package/dist/functions/note/index.d.ts +2 -0
- package/dist/functions/note/index.js +284 -0
- package/dist/functions/note/noteNameFSM.d.ts +8 -0
- package/dist/functions/note/noteNameFSM.js +192 -0
- package/dist/functions/over/index.d.ts +2 -0
- package/dist/functions/over/index.js +307 -0
- package/dist/functions/page/index.d.ts +20 -0
- package/dist/functions/page/index.js +63 -0
- package/dist/functions/set/index.d.ts +2 -0
- package/dist/functions/set/index.js +55 -0
- package/dist/functions/stack/index.d.ts +2 -0
- package/dist/functions/stack/index.js +164 -0
- package/dist/functions/tempo/index.d.ts +2 -0
- package/dist/functions/tempo/index.js +37 -0
- package/dist/functions/text/index.d.ts +2 -0
- package/dist/functions/text/index.js +65 -0
- package/dist/functions/tie/index.d.ts +2 -0
- package/dist/functions/tie/index.js +301 -0
- package/dist/functions/voice/index.d.ts +3 -0
- package/dist/functions/voice/index.js +714 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/layout/default.d.ts +12 -0
- package/dist/layout/default.js +27 -0
- package/dist/layout/engine.d.ts +47 -0
- package/dist/layout/engine.js +410 -0
- package/dist/layout/model.d.ts +73 -0
- package/dist/layout/model.js +422 -0
- package/dist/layout/page.d.ts +27 -0
- package/dist/layout/page.js +121 -0
- package/dist/layout/types.d.ts +197 -0
- package/dist/layout/types.js +1 -0
- package/dist/lowering/loweringContext.d.ts +126 -0
- package/dist/lowering/loweringContext.js +625 -0
- package/dist/lowering/track.d.ts +61 -0
- package/dist/lowering/track.js +35 -0
- package/dist/lowering/types.d.ts +176 -0
- package/dist/lowering/types.js +82 -0
- package/dist/parser/grammarType.d.ts +81 -0
- package/dist/parser/grammarType.js +1 -0
- package/dist/parser/parse-utils/brace-utils.d.ts +1 -0
- package/dist/parser/parse-utils/brace-utils.js +29 -0
- package/dist/parser/parse-utils/call-utils.d.ts +24 -0
- package/dist/parser/parse-utils/call-utils.js +178 -0
- package/dist/parser/parse-utils/label-utils.d.ts +4 -0
- package/dist/parser/parse-utils/label-utils.js +17 -0
- package/dist/parser/parse-utils/length-utils.d.ts +3 -0
- package/dist/parser/parse-utils/length-utils.js +20 -0
- package/dist/parser/parse-utils/note-utils.d.ts +10 -0
- package/dist/parser/parse-utils/note-utils.js +111 -0
- package/dist/parser/parserContext.d.ts +93 -0
- package/dist/parser/parserContext.js +542 -0
- package/dist/parser/preprocess.d.ts +6 -0
- package/dist/parser/preprocess.js +193 -0
- package/dist/parser/types.d.ts +31 -0
- package/dist/parser/types.js +2 -0
- package/dist/pipeline.d.ts +36 -0
- package/dist/pipeline.js +51 -0
- package/dist/render/canvas.d.ts +19 -0
- package/dist/render/canvas.js +121 -0
- package/dist/render/index.d.ts +5 -0
- package/dist/render/index.js +5 -0
- package/dist/render/recording.d.ts +46 -0
- package/dist/render/recording.js +76 -0
- package/dist/render/svg.d.ts +18 -0
- package/dist/render/svg.js +126 -0
- package/dist/render/text.d.ts +6 -0
- package/dist/render/text.js +17 -0
- package/dist/render/types.d.ts +86 -0
- package/dist/render/types.js +1 -0
- package/grammar.md +123 -0
- package/package.json +58 -0
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { paintLayout } from "../layout/engine.js";
|
|
2
|
+
function number(value) {
|
|
3
|
+
if (!Number.isFinite(value))
|
|
4
|
+
return "0";
|
|
5
|
+
return String(Math.round(value * 1000) / 1000);
|
|
6
|
+
}
|
|
7
|
+
function escapeXml(value) {
|
|
8
|
+
return value
|
|
9
|
+
.replace(/&/g, "&")
|
|
10
|
+
.replace(/</g, "<")
|
|
11
|
+
.replace(/>/g, ">")
|
|
12
|
+
.replace(/"/g, """)
|
|
13
|
+
.replace(/'/g, "'");
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 把结构化路径命令转换为 SVG path data
|
|
17
|
+
* 可选缩放和平移用于直接生成最终坐标
|
|
18
|
+
*/
|
|
19
|
+
function pathCommandsToSvg(commands, transform) {
|
|
20
|
+
const parts = [];
|
|
21
|
+
const x = transform
|
|
22
|
+
? (value) => number(transform.x + value * transform.scaleX)
|
|
23
|
+
: number;
|
|
24
|
+
const y = transform
|
|
25
|
+
? (value) => number(transform.y + value * transform.scaleY)
|
|
26
|
+
: number;
|
|
27
|
+
for (const command of commands) {
|
|
28
|
+
if (command.op === "Z") {
|
|
29
|
+
parts.push("Z");
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (command.op === "M" || command.op === "L") {
|
|
33
|
+
parts.push(`${command.op}${x(command.x)} ${y(command.y)}`);
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (command.op === "Q") {
|
|
37
|
+
parts.push(`Q${x(command.cx)} ${y(command.cy)} `
|
|
38
|
+
+ `${x(command.x)} ${y(command.y)}`);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (!("cx1" in command))
|
|
42
|
+
continue;
|
|
43
|
+
parts.push(`C${x(command.cx1)} ${y(command.cy1)} `
|
|
44
|
+
+ `${x(command.cx2)} ${y(command.cy2)} `
|
|
45
|
+
+ `${x(command.x)} ${y(command.y)}`);
|
|
46
|
+
}
|
|
47
|
+
return parts.join(" ");
|
|
48
|
+
}
|
|
49
|
+
function opacityAttribute(style) {
|
|
50
|
+
if (style?.opacity === void 0)
|
|
51
|
+
return "";
|
|
52
|
+
return ` opacity="${number(style.opacity)}"`;
|
|
53
|
+
}
|
|
54
|
+
/** 生成独立 SVG 字符串的 Painter;路径直接输出最终坐标 */
|
|
55
|
+
export class SvgPainter {
|
|
56
|
+
body = [];
|
|
57
|
+
drawText(text, x, y, style) {
|
|
58
|
+
const family = style.fontFamily ?? "sans-serif";
|
|
59
|
+
const weight = style.fontWeight ?? "normal";
|
|
60
|
+
const fill = style.fill ?? "#000";
|
|
61
|
+
const textAnchor = style.textAlign === "center"
|
|
62
|
+
? "middle"
|
|
63
|
+
: style.textAlign === "right" ? "end" : "start";
|
|
64
|
+
this.body.push(`<text x="${number(x)}" y="${number(y)}" `
|
|
65
|
+
+ `font-family="${escapeXml(family)}" font-size="${number(style.fontSize)}" `
|
|
66
|
+
+ `font-weight="${escapeXml(String(weight))}" text-anchor="${textAnchor}" `
|
|
67
|
+
+ `fill="${escapeXml(fill)}"`
|
|
68
|
+
+ `${opacityAttribute(style)}>${escapeXml(text)}</text>`);
|
|
69
|
+
}
|
|
70
|
+
drawLine(x1, y1, x2, y2, style) {
|
|
71
|
+
const stroke = style?.stroke ?? style?.fill ?? "#000";
|
|
72
|
+
const strokeWidth = style?.strokeWidth ?? 1;
|
|
73
|
+
this.body.push(`<line x1="${number(x1)}" y1="${number(y1)}" `
|
|
74
|
+
+ `x2="${number(x2)}" y2="${number(y2)}" `
|
|
75
|
+
+ `stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}" stroke-linecap="round"`
|
|
76
|
+
+ `${opacityAttribute(style)} />`);
|
|
77
|
+
}
|
|
78
|
+
drawRect(x, y, w, h, style) {
|
|
79
|
+
const fill = style?.fill ?? "none";
|
|
80
|
+
const stroke = style?.stroke ?? "none";
|
|
81
|
+
const strokeWidth = style?.strokeWidth ?? 1;
|
|
82
|
+
this.body.push(`<rect x="${number(x)}" y="${number(y)}" `
|
|
83
|
+
+ `width="${number(Math.max(0, w))}" height="${number(Math.max(0, h))}" `
|
|
84
|
+
+ `fill="${escapeXml(fill)}" stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}"`
|
|
85
|
+
+ `${opacityAttribute(style)} />`);
|
|
86
|
+
}
|
|
87
|
+
drawCircle(cx, cy, r, style) {
|
|
88
|
+
const fill = style?.fill ?? "#000";
|
|
89
|
+
const stroke = style?.stroke ?? "none";
|
|
90
|
+
const strokeWidth = style?.strokeWidth ?? 1;
|
|
91
|
+
this.body.push(`<circle cx="${number(cx)}" cy="${number(cy)}" `
|
|
92
|
+
+ `r="${number(Math.max(0, r))}" fill="${escapeXml(fill)}" `
|
|
93
|
+
+ `stroke="${escapeXml(stroke)}" stroke-width="${number(strokeWidth)}"`
|
|
94
|
+
+ `${opacityAttribute(style)} />`);
|
|
95
|
+
}
|
|
96
|
+
drawPath(commands, style, transform) {
|
|
97
|
+
if (commands.length === 0)
|
|
98
|
+
return;
|
|
99
|
+
const d = pathCommandsToSvg(commands, transform);
|
|
100
|
+
const fill = style?.fill ?? "none";
|
|
101
|
+
const stroke = style?.stroke ?? "none";
|
|
102
|
+
const strokeWidth = style?.strokeWidth ?? 1;
|
|
103
|
+
this.body.push(`<path d="${d}" fill="${escapeXml(fill)}" stroke="${escapeXml(stroke)}" `
|
|
104
|
+
+ `stroke-width="${number(strokeWidth)}" stroke-linecap="round" stroke-linejoin="round"`
|
|
105
|
+
+ `${opacityAttribute(style)} />`);
|
|
106
|
+
}
|
|
107
|
+
toSvg(bounds, options = {}) {
|
|
108
|
+
const padding = Math.max(0, options.padding ?? 0);
|
|
109
|
+
const left = bounds.x - padding;
|
|
110
|
+
const top = bounds.y - padding;
|
|
111
|
+
const width = Math.max(1, bounds.w + padding * 2);
|
|
112
|
+
const height = Math.max(1, bounds.h + padding * 2);
|
|
113
|
+
const background = options.background
|
|
114
|
+
? `<rect x="${number(left)}" y="${number(top)}" width="${number(width)}" height="${number(height)}" fill="${escapeXml(options.background)}" />`
|
|
115
|
+
: "";
|
|
116
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${number(width)}" height="${number(height)}" `
|
|
117
|
+
+ `viewBox="${number(left)} ${number(top)} ${number(width)} ${number(height)}">`
|
|
118
|
+
+ `${background}${this.body.join("")}</svg>`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/** 使用 SVG 后端绘制一个已经完成布局的文档 */
|
|
122
|
+
export function renderLayoutToSvg(result, options = {}) {
|
|
123
|
+
const painter = new SvgPainter();
|
|
124
|
+
paintLayout(result, painter);
|
|
125
|
+
return painter.toSvg(result.bounds, options);
|
|
126
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { GlyphMetrics, TextMeasurer, TextStyle } from "./types.js";
|
|
2
|
+
/** 默认文本测量保持确定性,不依赖浏览器和操作系统字体。 */
|
|
3
|
+
export declare class DefaultTextMeasurer implements TextMeasurer {
|
|
4
|
+
measureText(text: string, style: TextStyle): GlyphMetrics;
|
|
5
|
+
}
|
|
6
|
+
export declare const defaultTextMeasurer: DefaultTextMeasurer;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** 默认文本测量保持确定性,不依赖浏览器和操作系统字体。 */
|
|
2
|
+
export class DefaultTextMeasurer {
|
|
3
|
+
measureText(text, style) {
|
|
4
|
+
const size = style.fontSize;
|
|
5
|
+
const monospaced = style.fontFamily?.toLowerCase().includes("mono") ?? false;
|
|
6
|
+
let units = 0;
|
|
7
|
+
for (const char of text) {
|
|
8
|
+
units += char.charCodeAt(0) > 0x7f ? 1 : monospaced ? 0.62 : 0.58;
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
w: Math.max(units * size, size * 0.25),
|
|
12
|
+
h: size,
|
|
13
|
+
baseline: size * 0.8,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export const defaultTextMeasurer = new DefaultTextMeasurer();
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 字形的排版度量
|
|
3
|
+
*
|
|
4
|
+
* 这里的坐标都位于字形自己的局部坐标系中
|
|
5
|
+
* 不依赖 SVG text 或 Canvas 字体测量结果
|
|
6
|
+
*/
|
|
7
|
+
export interface GlyphMetrics {
|
|
8
|
+
w: number;
|
|
9
|
+
h: number;
|
|
10
|
+
baseline: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* 通用绘制样式
|
|
14
|
+
*
|
|
15
|
+
* fill 和 stroke 都为空时由具体后端选择默认值
|
|
16
|
+
*/
|
|
17
|
+
export interface PaintStyle {
|
|
18
|
+
fill?: string;
|
|
19
|
+
stroke?: string;
|
|
20
|
+
strokeWidth?: number;
|
|
21
|
+
opacity?: number;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 任意文本的排版样式
|
|
25
|
+
*
|
|
26
|
+
* 固定乐谱符号应优先使用 glyph
|
|
27
|
+
* text 主要用于歌词、标题和用户输入的任意文本
|
|
28
|
+
*/
|
|
29
|
+
export interface TextStyle {
|
|
30
|
+
fontSize: number;
|
|
31
|
+
fontFamily?: string;
|
|
32
|
+
fontWeight?: string | number;
|
|
33
|
+
textAlign?: "left" | "center" | "right";
|
|
34
|
+
fill?: string;
|
|
35
|
+
opacity?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 通用路径命令
|
|
39
|
+
*
|
|
40
|
+
* 使用结构化命令而不是 SVG path 字符串
|
|
41
|
+
* 这样 Canvas 和 SVG 后端都可以直接消费
|
|
42
|
+
*/
|
|
43
|
+
export type PathCommand = {
|
|
44
|
+
op: "M" | "L";
|
|
45
|
+
x: number;
|
|
46
|
+
y: number;
|
|
47
|
+
} | {
|
|
48
|
+
op: "Q";
|
|
49
|
+
cx: number;
|
|
50
|
+
cy: number;
|
|
51
|
+
x: number;
|
|
52
|
+
y: number;
|
|
53
|
+
} | {
|
|
54
|
+
op: "C";
|
|
55
|
+
cx1: number;
|
|
56
|
+
cy1: number;
|
|
57
|
+
cx2: number;
|
|
58
|
+
cy2: number;
|
|
59
|
+
x: number;
|
|
60
|
+
y: number;
|
|
61
|
+
} | {
|
|
62
|
+
op: "Z";
|
|
63
|
+
};
|
|
64
|
+
export interface TextMeasurer {
|
|
65
|
+
measureText(text: string, style: TextStyle): GlyphMetrics;
|
|
66
|
+
}
|
|
67
|
+
/** 把局部路径放置到最终布局坐标;线宽仍使用最终布局像素 */
|
|
68
|
+
export interface PathTransform {
|
|
69
|
+
x: number;
|
|
70
|
+
y: number;
|
|
71
|
+
scaleX: number;
|
|
72
|
+
scaleY: number;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 所有渲染后端必须实现的最小绘制接口
|
|
76
|
+
*
|
|
77
|
+
* 函数节点只描述要画的 glyph、路径和基础图形
|
|
78
|
+
* 不直接访问 SVGElement 或 CanvasRenderingContext2D
|
|
79
|
+
*/
|
|
80
|
+
export interface Painter {
|
|
81
|
+
drawText(text: string, x: number, y: number, style: TextStyle): void;
|
|
82
|
+
drawLine(x1: number, y1: number, x2: number, y2: number, style?: PaintStyle): void;
|
|
83
|
+
drawRect(x: number, y: number, w: number, h: number, style?: PaintStyle): void;
|
|
84
|
+
drawCircle(cx: number, cy: number, r: number, style?: PaintStyle): void;
|
|
85
|
+
drawPath(commands: readonly PathCommand[], style?: PaintStyle, transform?: PathTransform): void;
|
|
86
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/grammar.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# jpFun 语法规范
|
|
2
|
+
|
|
3
|
+
## 1. 核心思想
|
|
4
|
+
完全函数式,但提供语法糖供快捷调用。
|
|
5
|
+
|
|
6
|
+
## 2. 基本类型
|
|
7
|
+
- 字符串,用引号包裹,只能用英文双引号`"`。如果不会引起歧义,可以不加引号。(单引号用于其他语法糖)
|
|
8
|
+
- bool值,只能为 `true` / `false`,
|
|
9
|
+
- 数字
|
|
10
|
+
- 内容,用 `{}` 包裹。如果不会引起歧义,可以不加括号
|
|
11
|
+
- 标签
|
|
12
|
+
- 长度,表现为 `${数字}${单位}` 的字符串。单位仅支持 `px`(默认) `em`,会统一固化到 `px`
|
|
13
|
+
|
|
14
|
+
“内容”的基本元素:
|
|
15
|
+
- 函数调用:`@func(...)`,函数名后必须立即接`(`,参数使用逗号分隔,参数可以是(2)中提到的四个类型
|
|
16
|
+
- 标签:`XXX@label`,不按要求的函数调用都会被理解为label,用于关系型函数的参数
|
|
17
|
+
|
|
18
|
+
> 歧义:比如字符串为 `"123,456"`,在函数调用时,逗号是参数的分隔符,如果没有引号,则会认为传递了两个数字,引发歧义。因此必须用引号分割
|
|
19
|
+
|
|
20
|
+
注释:`%` 直到行末
|
|
21
|
+
|
|
22
|
+
## 3. 主要函数
|
|
23
|
+
- `@1(tonality)` 修改调性 如 `1=C` 作用域为当前时间之后
|
|
24
|
+
- `@tempo(number)` 修改bpm 作用域为当前时间之后
|
|
25
|
+
|
|
26
|
+
- `@set(key=value, ...)` 修改函数的缺省值,作用域为当前 `{}`
|
|
27
|
+
几个特殊变量:
|
|
28
|
+
- `fontsize`: length类型,是所有`em`的基准
|
|
29
|
+
- `strict`: bool类型,开启后如果函数未定义不再忽略,一些宽容的语法糖也将被禁止
|
|
30
|
+
- `autobeam`: bool类型,默认 `true`;设为 `false` 后,不同 `div` 函数之间不会自动连接
|
|
31
|
+
|
|
32
|
+
- `@page(width, height, top, bottom, left, right, gap)` 设置文档页面
|
|
33
|
+
- 只能在文档顶层声明一次;嵌套声明或后续重复声明会产生诊断并被忽略
|
|
34
|
+
- 所有参数都是长度,parse 时按当时的 `em` 固化为 px
|
|
35
|
+
- 默认:`width=794px`、`height=0px`(无限高)、上下边距 `48px`、左右边距 `40px`、`gap=1em`
|
|
36
|
+
- `height <= 0` 表示无限高,不会换页
|
|
37
|
+
- 有限高度下,如果一行连同上下边距都放不下,layout 直接报错
|
|
38
|
+
- 一页放不下下一行时从下一页开始;已封口页面的行间距会平分剩余纵向空间,末页保持最小 `gap`
|
|
39
|
+
|
|
40
|
+
- `@stack(...)` 临时伴奏/临时多声部:第一个成员留在宿主轨,其余成员按书写顺序依次向上叠放
|
|
41
|
+
- `@voices(...)` 多声部块:所有成员都有独立轨道,整块在宿主基线上局部居中(首末两条基线的中点对齐宿主轴),两个以上成员时在声部名与音符之间画多声部括线;里面只能是 `@voice`,一般用语法糖创建
|
|
42
|
+
- `@voice({}, 音轨名, 名称1={}, 名称2={}, ...)`
|
|
43
|
+
语法糖:
|
|
44
|
+
```
|
|
45
|
+
N(音轨名): ... (note等元素)
|
|
46
|
+
L(名称1): ... 歌词1
|
|
47
|
+
L(名称2): ... 歌词2
|
|
48
|
+
```
|
|
49
|
+
起始条件1: `N(xxx):`。终止条件:该层级的换行符或结束。可以有大括号,大括号内随意换行
|
|
50
|
+
起始条件2: `L(xxx):`。终止条件:该层级的换行符或结束。如果上一个不是voice就报错
|
|
51
|
+
|
|
52
|
+
- `@over(...)` 创建层叠的音符,比如同时演奏多个长度相同的音符。时间、横向占用和对齐基准以第一个参数为准,其他参数只向上堆叠
|
|
53
|
+
|
|
54
|
+
- `@grace(宿主, 倚音, side=pre)` 倚音
|
|
55
|
+
- 语法糖:`>` 与 `<`,**箭头永远指向宿主**
|
|
56
|
+
- `2>1` 前倚音:`2` 画在 `1` 的左上角;`1<2` 后倚音:`2` 画在 `1` 的右上角
|
|
57
|
+
- 多个倚音用大括号:`{3 2}>1`;相邻倚音的减时线会自动连成整条
|
|
58
|
+
- 允许嵌套:`1>2>3` 表示 1 是 2 的倚音、2 是 3 的倚音,字号逐层缩小(`>` 左结合,`<` 右结合)
|
|
59
|
+
- 倚音比宿主小一号,嵌套时逐层相乘;宿主与倚音各自只能产生可见事件,宿主必须恰好一个
|
|
60
|
+
- 倚音**默认就是八分音符**(自带一条减时线),再写一个 `/` 变成十六分音符
|
|
61
|
+
- 倚音不进入时间流:整体时值等于宿主时值,小节线、拍位、自动连梁都不受影响
|
|
62
|
+
- 播放时倚音从宿主借时值:`比例 = 倚音字面总时值 / 四分音符`,基准取宿主经 div 与 dot 之后的实际时值,上限 0.75
|
|
63
|
+
|
|
64
|
+
- `@note(...)` / `@n(...)`
|
|
65
|
+
- 音名:`A B C D E F G`
|
|
66
|
+
- 可见休止:`0`(`Z` 等价 `0`)
|
|
67
|
+
- 隐形占位:`8`
|
|
68
|
+
- 只打拍不发音:`9`,显示为 `X`(不显示升降号和八度点)
|
|
69
|
+
- 升降符:`#` `b` `n`(可重复,如 `bb`、`###`)
|
|
70
|
+
- `@div(...)` / `@/(...)`
|
|
71
|
+
- 语法糖:直接在后面加`/`,可多个
|
|
72
|
+
- 同一个 `div` 内容中的减时事件始终连接,与 `autobeam` 无关
|
|
73
|
+
- 默认自动连接同轨、连续、同一逻辑行且位于同一四分音符拍内的独立 `div`,例如 `1/ 2// 3/`
|
|
74
|
+
- 自动连接在拍边界、小节线、显式换行、时间间隙、同轨可见非候选元素、普通长事件、开关变化或手工 `@beam` 处结束
|
|
75
|
+
- `@set(autobeam=false)` 关闭不同 `div` 之间的自动连接;局部减时线仍各自绘制
|
|
76
|
+
- `@dot(...)` / `@.(...)`
|
|
77
|
+
- 语法糖:直接在后面加`.`,可多个
|
|
78
|
+
- `@dash()` / `@-()`
|
|
79
|
+
- 语法糖:直接在后面加`-`,可多个
|
|
80
|
+
- `@bar(...)` 创建一个小节线 时间线上强制对齐
|
|
81
|
+
- `@br(offset=1)` 在后续事件前偏移指定行数
|
|
82
|
+
- `offset` 是第一个位置参数,因此通常写作 `@br(2)`
|
|
83
|
+
- 默认值为 1;按非负整数固化,小数向下取整,负数视为 0
|
|
84
|
+
- `@br(0)` 不换行,`@br(2)` 会保留一个空行
|
|
85
|
+
- 每次调用独立生效,多个 br 暂不合并
|
|
86
|
+
|
|
87
|
+
注意:简谱中永远先减时线作用,再加上附点,无论函数作用先后
|
|
88
|
+
|
|
89
|
+
### 关系型
|
|
90
|
+
参数类型为标签,如果不传,则默认取最后两个注册的节点(比如note)
|
|
91
|
+
- `@beam(...)`:手工连接传参的减时线;不受 `autobeam` 开关影响。参数必须按时间顺序对应同轨、同谱面行中的相邻可见元素;over 只有第一参数内的元素可参与外层 beam
|
|
92
|
+
- `@tie(...)`:按参数顺序连接同一轨道中的元素;跨轨端点报错。近距离使用普通弧线,远距离或跨行时使用带水平段的连音线
|
|
93
|
+
|
|
94
|
+
语法糖实例:
|
|
95
|
+
- `@bar(C1 C3)` -> `@bar({ C1 C3 })`
|
|
96
|
+
- `{...}//` -> `@div({ ... }, 2)`
|
|
97
|
+
- `{...}.` -> `@dot({ ... }, 1)`
|
|
98
|
+
- `-` -> `@dash()`
|
|
99
|
+
- `--` -> `@dash() @dash()`
|
|
100
|
+
- `C1 C3# |` -> `@note(C,1) @note(C,3,#) @bar()`
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
## 4. 参数规则
|
|
104
|
+
|
|
105
|
+
命名参数取值顺序固定:
|
|
106
|
+
|
|
107
|
+
1. 显式传参
|
|
108
|
+
2. 上下文变量(`@set`)
|
|
109
|
+
3. 默认值
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
## 5. 预处理规则(换行与注释)
|
|
113
|
+
以下规则由 `src/parser/preprocess.ts` 在词法预处理阶段执行。所有替换都保持等长(用空格替换),以保证 offset 稳定。
|
|
114
|
+
|
|
115
|
+
- 注释规则:`%` 在非字符串上下文下表示行注释起点,作用到行尾(不含换行)。
|
|
116
|
+
- `\%` 不会转义注释符:反斜杠不会阻止 `%` 开启注释。
|
|
117
|
+
- 换行识别:支持 `\n`、`\r\n`,兼容 `\r`;`lineStarts` 以逻辑换行为单位记录下一行起点。
|
|
118
|
+
- 行尾连续反斜杠规则:
|
|
119
|
+
1. 对每一行,从行尾向前找最后一个非换行空白字符;若它属于连续 `\` 块,块长度记为 `n`。
|
|
120
|
+
2. 连续块中的第 1/3/5/... 个 `\` 替换为空格,第 2/4/6/... 个保留。
|
|
121
|
+
3. 若 `n` 为奇数,则该行换行符也替换为空格(续行)。
|
|
122
|
+
4. 若 `n` 为偶数,则不续行(换行保留)。
|
|
123
|
+
- SourceSpan 统一按左闭右开 `[start, end)` 理解。
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "jpfun",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A functional numbered musical notation typesetting engine",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE",
|
|
20
|
+
"grammar.md"
|
|
21
|
+
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/madderscientist/jpFun.git",
|
|
28
|
+
"directory": "packages/jpfun"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/madderscientist/jpFun#readme",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/madderscientist/jpFun/issues"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"numbered-musical-notation",
|
|
39
|
+
"jianpu",
|
|
40
|
+
"music-notation",
|
|
41
|
+
"sheet-music",
|
|
42
|
+
"typesetting"
|
|
43
|
+
],
|
|
44
|
+
"author": "madderscientist",
|
|
45
|
+
"license": "Apache-2.0",
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"tsx": "^4.23.12",
|
|
48
|
+
"typescript": "^7.0.2"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"clean": "node -e \"for (const path of ['dist', 'tsconfig.tsbuildinfo']) require('node:fs').rmSync(path, { recursive: true, force: true })\"",
|
|
52
|
+
"build": "pnpm run clean && tsc -p tsconfig.json",
|
|
53
|
+
"test": "node --import tsx test/test.ts",
|
|
54
|
+
"test_preprocess": "node --import tsx test/test_preprocess.ts",
|
|
55
|
+
"test_layout": "node --import tsx test/test_layout.ts",
|
|
56
|
+
"test_render": "node --import tsx test/test_render.ts"
|
|
57
|
+
}
|
|
58
|
+
}
|