jpfun 0.1.0 → 0.2.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 CHANGED
@@ -1,134 +1,134 @@
1
- # jpFun
2
-
3
- A functional numbered musical notation typesetting engine for TypeScript.
4
-
5
- jpFun compiles a compact numbered-notation DSL into a reusable document layout that can be rendered as SVG or Canvas.
6
-
7
- ## Features
8
-
9
- - Concise DSL with function-based extensibility
10
- - Numbered notes, rests, chords, lyrics, voices, grace notes, ties, and beams
11
- - Automatic spacing, line breaking, and pagination
12
- - Shared layout model for SVG and Canvas rendering
13
- - Structured diagnostics and source locations
14
- - Zero runtime dependencies
15
- - Works in both Node.js and modern browsers
16
-
17
- ## Installation
18
-
19
- ```sh
20
- npm install jpfun
21
- ```
22
-
23
- jpFun is ESM-only and requires Node.js 18 or later:
24
-
25
- ```ts
26
- import { compileScore } from "jpfun";
27
- ```
28
-
29
- CommonJS `require("jpfun")` is not supported.
30
-
31
- ## Quick Start
32
-
33
- ```ts
34
- import { compileScore, renderLayoutToSvg } from "jpfun";
35
-
36
- const source = `
37
- @1(C4)
38
- @tempo(96)
39
-
40
- 1/ 2/ 3 4 | 5 -
41
- `;
42
-
43
- const result = compileScore(source);
44
- const svg = renderLayoutToSvg(result.layout);
45
- ```
46
-
47
- `renderLayoutToSvg` returns an SVG string that can be written to a file, sent in an HTTP response, or inserted into a browser page.
48
-
49
- ## Canvas Rendering
50
-
51
- ```ts
52
- import { compileScore, renderLayoutToCanvas } from "jpfun";
53
-
54
- const result = compileScore("1 2 3 | 4");
55
- const canvas = document.querySelector("canvas");
56
- const context = canvas.getContext("2d");
57
-
58
- if (context) {
59
- renderLayoutToCanvas(result.layout, context);
60
- }
61
- ```
62
-
63
- ## Compilation Pipeline
64
-
65
- jpFun exposes the intermediate results of its compilation pipeline:
66
-
67
- ```text
68
- Source
69
- -> Parser -> AST
70
- -> Lowering -> timed events and relations
71
- -> Layout -> document geometry
72
- -> Renderer -> SVG or Canvas
73
- ```
74
-
75
- ```ts
76
- const result = compileScore("1 2 3 | 4");
77
-
78
- result.parser;
79
- result.ast;
80
- result.lowering;
81
- result.layout;
82
- ```
83
-
84
- This makes it possible to build editors, diagnostics, playback tools, and custom renderers on top of the same compilation result.
85
-
86
- ## Syntax Example
87
-
88
- ```jpfun
89
- @page(width=860px, left=38px, right=38px)
90
- @1(C4)
91
- @tempo(96)
92
-
93
- N(Melody): 1/ 1/ 5/ 5/ 6/ 6/ 5 -
94
- N: @div(C4 C4) G4/G4/ A4/ A/ G
95
- L(Lyrics): do do so so la la so
96
- ```
97
-
98
- See the [language reference](grammar.md) for the complete syntax.
99
-
100
- ## Runtime Support
101
-
102
- - ESM only
103
- - Node.js 18 or later
104
- - Modern browsers and bundlers
105
- - No DOM dependency in the compiler
106
- - No runtime dependencies
107
-
108
- ## Development
109
-
110
- jpFun is maintained in a pnpm workspace. From the repository root:
111
-
112
- ```sh
113
- pnpm install
114
- pnpm run build:core
115
- pnpm test
116
- ```
117
-
118
- The repository also includes a browser-based Playground with diagnostics and SVG/Canvas previews:
119
-
120
- ```sh
121
- pnpm run dev
122
- ```
123
-
124
- Then open <http://127.0.0.1:4173>.
125
-
126
- ## Documentation
127
-
128
- - [Language reference](grammar.md)
129
- - [Repository](https://github.com/madderscientist/jpFun)
130
- - [Issue tracker](https://github.com/madderscientist/jpFun/issues)
131
-
132
- ## License
133
-
1
+ # jpFun
2
+
3
+ A functional numbered musical notation typesetting engine for TypeScript.
4
+
5
+ jpFun compiles a compact numbered-notation DSL into a reusable document layout that can be rendered as SVG or Canvas.
6
+
7
+ ## Features
8
+
9
+ - Concise DSL with function-based extensibility
10
+ - Numbered notes, rests, chords, lyrics, voices, grace notes, ties, and beams
11
+ - Automatic spacing, line breaking, and pagination
12
+ - Shared layout model for SVG and Canvas rendering
13
+ - Structured diagnostics and source locations
14
+ - Zero runtime dependencies
15
+ - Works in both Node.js and modern browsers
16
+
17
+ ## Installation
18
+
19
+ ```sh
20
+ npm install jpfun
21
+ ```
22
+
23
+ jpFun is ESM-only and requires Node.js 18 or later:
24
+
25
+ ```ts
26
+ import { compileScore } from "jpfun";
27
+ ```
28
+
29
+ CommonJS `require("jpfun")` is not supported.
30
+
31
+ ## Quick Start
32
+
33
+ ```ts
34
+ import { compileScore, renderLayoutToSvg } from "jpfun";
35
+
36
+ const source = `
37
+ @1(C4)
38
+ @tempo(96)
39
+
40
+ 1/ 2/ 3 4 | 5 -
41
+ `;
42
+
43
+ const result = compileScore(source);
44
+ const svg = renderLayoutToSvg(result.layout);
45
+ ```
46
+
47
+ `renderLayoutToSvg` returns an SVG string that can be written to a file, sent in an HTTP response, or inserted into a browser page.
48
+
49
+ ## Canvas Rendering
50
+
51
+ ```ts
52
+ import { compileScore, renderLayoutToCanvas } from "jpfun";
53
+
54
+ const result = compileScore("1 2 3 | 4");
55
+ const canvas = document.querySelector("canvas");
56
+ const context = canvas.getContext("2d");
57
+
58
+ if (context) {
59
+ renderLayoutToCanvas(result.layout, context);
60
+ }
61
+ ```
62
+
63
+ ## Compilation Pipeline
64
+
65
+ jpFun exposes the intermediate results of its compilation pipeline:
66
+
67
+ ```text
68
+ Source
69
+ -> Parser -> AST
70
+ -> Lowering -> timed events and relations
71
+ -> Layout -> document geometry
72
+ -> Renderer -> SVG or Canvas
73
+ ```
74
+
75
+ ```ts
76
+ const result = compileScore("1 2 3 | 4");
77
+
78
+ result.parser;
79
+ result.ast;
80
+ result.lowering;
81
+ result.layout;
82
+ ```
83
+
84
+ This makes it possible to build editors, diagnostics, playback tools, and custom renderers on top of the same compilation result.
85
+
86
+ ## Syntax Example
87
+
88
+ ```jpfun
89
+ @page(width=860px, left=38px, right=38px)
90
+ @1(C4)
91
+ @tempo(96)
92
+
93
+ N(Melody): 1/ 1/ 5/ 5/ 6/ 6/ 5 -
94
+ N: @div(C4 C4) G4/G4/ A4/ A/ G
95
+ L(Lyrics): do do so so la la so
96
+ ```
97
+
98
+ See the [language reference](grammar.md) for the complete syntax.
99
+
100
+ ## Runtime Support
101
+
102
+ - ESM only
103
+ - Node.js 18 or later
104
+ - Modern browsers and bundlers
105
+ - No DOM dependency in the compiler
106
+ - No runtime dependencies
107
+
108
+ ## Development
109
+
110
+ jpFun is maintained in a pnpm workspace. From the repository root:
111
+
112
+ ```sh
113
+ pnpm install
114
+ pnpm run build:core
115
+ pnpm test
116
+ ```
117
+
118
+ The repository also includes a browser-based Playground with diagnostics and SVG/Canvas previews:
119
+
120
+ ```sh
121
+ pnpm run dev
122
+ ```
123
+
124
+ Then open <http://127.0.0.1:4173>.
125
+
126
+ ## Documentation
127
+
128
+ - [Language reference](grammar.md)
129
+ - [Repository](https://github.com/madderscientist/jpFun)
130
+ - [Issue tracker](https://github.com/madderscientist/jpFun/issues)
131
+
132
+ ## License
133
+
134
134
  [Apache License 2.0](LICENSE)
@@ -1,16 +1,16 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { TemporalNodeBase } from "../../lowering/types.js";
2
+ import { ANCHOR_KEY, TemporalNodeBase } from "../../lowering/types.js";
3
3
  class BarFunction extends ASTFunctionNode {
4
4
  static def = {
5
5
  name: ["bar", "|"],
6
6
  description: "小节线",
7
- example: `@bar(type, lengthEM) 创建一个小节线
8
- 语法糖:
9
- - type0: '|' 普通小节线
10
- - type1: '||' 终止小节线 左细右粗
11
- - type2: '|:' 重复小节线 左粗右细
12
- - type3: ':|'
13
- - type4: ':|:' 左右反复
7
+ example: `@bar(type, lengthEM) 创建一个小节线
8
+ 语法糖:
9
+ - type0: '|' 普通小节线
10
+ - type1: '||' 终止小节线 左细右粗
11
+ - type2: '|:' 重复小节线 左粗右细
12
+ - type3: ':|'
13
+ - type4: ':|:' 左右反复
14
14
  `,
15
15
  allowExtraArgs: false,
16
16
  args: [
@@ -85,7 +85,7 @@ class BarTemporalNode extends TemporalNodeBase {
85
85
  super();
86
86
  this.ast = ast;
87
87
  this.T = 0;
88
- this.type = 0 /* ColType.ANCHOR */;
88
+ this.mergeKey = ANCHOR_KEY;
89
89
  this.initLayoutBox();
90
90
  // 由于有最小时长,因此设置alpha依然有效
91
91
  this.springConfig = {
@@ -1,4 +1,4 @@
1
- import { isVisualTemporalNode, } from "../../lowering/types.js";
1
+ import { ANCHOR_KEY, isVisualTemporalNode, } from "../../lowering/types.js";
2
2
  import { DIV_ADDON_KEY, DivNode, } from "../div/index.js";
3
3
  import { collectExplicitBeamEndpoints, createBeamLayoutAttachment, } from "./layout.js";
4
4
  const QUARTER_NOTE_DURATION = 1;
@@ -138,13 +138,13 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
138
138
  flush(track);
139
139
  };
140
140
  for (const column of result.columns) {
141
- const anchor = column.find(node => node.type === 0 /* ColType.ANCHOR */);
141
+ const anchor = column.find(node => node.mergeKey === ANCHOR_KEY);
142
142
  if (anchor) {
143
143
  flushAll();
144
144
  measureStart = anchor.t;
145
145
  }
146
146
  for (const node of column) {
147
- if (node.type === 0 /* ColType.ANCHOR */)
147
+ if (node.mergeKey === ANCHOR_KEY)
148
148
  continue;
149
149
  const memberships = isVisualTemporalNode(node) ? nodeScopes.get(node) : undefined;
150
150
  const isIndependentDiv = memberships?.every(scope => scope.nodes.length === 1) ?? false;
@@ -6,10 +6,10 @@ class BrFunction extends ASTFunctionNode {
6
6
  static def = {
7
7
  name: ["br"],
8
8
  description: "换行",
9
- example: `@br(offset=1): 在后续事件前偏移指定行数
10
- @br() === @br(1)
11
- @br(2): 空出一行后继续
12
- 语法糖: [仅限顶层]至少连续两个换行
9
+ example: `@br(offset=1): 在后续事件前偏移指定行数
10
+ @br() === @br(1)
11
+ @br(2): 空出一行后继续
12
+ 语法糖: [仅限顶层]至少连续两个换行
13
13
  `,
14
14
  allowExtraArgs: false,
15
15
  args: [
@@ -45,7 +45,7 @@ class BrFunction extends ASTFunctionNode {
45
45
  const br = new TemporalNodeBase();
46
46
  br.ast = this;
47
47
  br.T = 0;
48
- br.type = 1 /* ColType.SINGLE */;
48
+ br.mergeKey = -1;
49
49
  br.breakBefore = this.offset;
50
50
  return [br];
51
51
  }
@@ -1,11 +1,11 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { TemporalNodeBase } from "../../lowering/types.js";
2
+ import { DEFAULT_KEY, TemporalNodeBase } from "../../lowering/types.js";
3
3
  class DashFunction extends ASTFunctionNode {
4
4
  static def = {
5
5
  name: ["dash", "-"],
6
6
  description: "增时线",
7
- example: `@dash() 创建一根增时线
8
- 语法糖: 一个 '-' 代表一个 @dash()
7
+ example: `@dash() 创建一根增时线
8
+ 语法糖: 一个 '-' 代表一个 @dash()
9
9
  `,
10
10
  allowExtraArgs: false,
11
11
  args: []
@@ -41,7 +41,7 @@ class DashTemporalNode extends TemporalNodeBase {
41
41
  super();
42
42
  this.ast = ast;
43
43
  this.T = 1;
44
- this.type = 2 /* ColType.DEFAULT */;
44
+ this.mergeKey = DEFAULT_KEY;
45
45
  this.initLayoutBox();
46
46
  }
47
47
  prepareLayout() {
@@ -8,7 +8,7 @@ import { GraceNode } from "./grace/index.js";
8
8
  import { KeyNode } from "./key/index.js";
9
9
  import { BrNode } from "./br/index.js";
10
10
  import { NoteNode } from "./note/index.js";
11
- import { UpNode } from "./over/index.js";
11
+ import { UpNode } from "./up/index.js";
12
12
  import { PageNode } from "./page/index.js";
13
13
  import { StackNode } from "./stack/index.js";
14
14
  import { SetNode } from "./set/index.js";
@@ -28,12 +28,12 @@ class DivFunction extends ASTFunctionNode {
28
28
  static def = {
29
29
  name: [DIV_FUNC_NAME, "/"],
30
30
  description: "减时线",
31
- example: `@div(C1, 2): C1下方创建2根减时线
32
- @/({C1 C2 @dash()}, 3): C1、C2和增时线下方创建3根减时线并连接
33
- 语法糖:在音符后加斜杠'/',可以多个
34
- @div(C1, 2) === C1//
35
- @div(C1 C2/ @dash(), 2) === {C1 C2/ @dash()}//
36
- 设置命名参数前缀: div
31
+ example: `@div(C1, 2): C1下方创建2根减时线
32
+ @/({C1 C2 @dash()}, 3): C1、C2和增时线下方创建3根减时线并连接
33
+ 语法糖:在音符后加斜杠'/',可以多个
34
+ @div(C1, 2) === C1//
35
+ @div(C1 C2/ @dash(), 2) === {C1 C2/ @dash()}//
36
+ 设置命名参数前缀: div
37
37
  `,
38
38
  allowExtraArgs: false,
39
39
  args: [
@@ -9,11 +9,11 @@ class DotFunction extends ASTFunctionNode {
9
9
  static def = {
10
10
  name: [DOT_FUNC_NAME, "."],
11
11
  description: "附点",
12
- example: `@dot(C1, 2): C1右侧创建2个点 仅接收一个可接收元素
13
- 语法糖:在音符后加斜杠'.',可以多个
14
- @dot(C1, 2) === C1..
15
- @dot(C1 C2/ @dash(), 2) 报错 因为点只能接收一个元素
16
- 设置命名参数前缀: dot
12
+ example: `@dot(C1, 2): C1右侧创建2个点 仅接收一个可接收元素
13
+ 语法糖:在音符后加斜杠'.',可以多个
14
+ @dot(C1, 2) === C1..
15
+ @dot(C1 C2/ @dash(), 2) 报错 因为点只能接收一个元素
16
+ 设置命名参数前缀: dot
17
17
  `,
18
18
  allowExtraArgs: false,
19
19
  args: [
@@ -1,7 +1,7 @@
1
1
  import type { LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
2
2
  import type { LoweringContext } from "../../lowering/loweringContext.js";
3
3
  import type { Track } from "../../lowering/track.js";
4
- import { TemporalNodeBase, type LoweringResult, type VisualTemporalNode } from "../../lowering/types.js";
4
+ import { TemporalNodeBase, type VisualTemporalNode } from "../../lowering/types.js";
5
5
  import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
6
6
  import { ParserContext } from "../../parser/parserContext.js";
7
7
  import type { Painter } from "../../render/types.js";
@@ -15,13 +15,6 @@ declare class GraceFunction extends ASTFunctionNode {
15
15
  node: GrammarSugarNode;
16
16
  } | null;
17
17
  static deSugarRelation(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number): number | null;
18
- /**
19
- * 语义确定后,把提升上来的修饰交还给宿主成员
20
- *
21
- * lowering 期间修饰必须挂在复合节点上,自动连梁之类的 augmenter 才看得到它的节奏;
22
- * 但渲染上「宿主代表整个复合体」,减时线要画在宿主音符下面而不是整个盒子下面。
23
- */
24
- static loweringFinalize: (result: LoweringResult) => void;
25
18
  host: ASTNodeBase;
26
19
  grace: ASTNodeBase;
27
20
  side: GraceSide;
@@ -29,12 +29,12 @@ class GraceFunction extends ASTFunctionNode {
29
29
  static def = {
30
30
  name: ["grace"],
31
31
  description: "倚音",
32
- example: `前倚音: @grace(宿主, 倚音, side=pre) 后倚音: @grace(宿主, 倚音, side=post)
33
- 语法糖:'>' 与 '<',箭头永远指向宿主
34
- 2>1 前倚音:2 是 1 的倚音,画在 1 的左上角
35
- 1<2 后倚音:2 是 1 的倚音,画在 1 的右上角
36
- {3 2}>1 多个倚音用大括号括起来
37
- 1>2>3 允许嵌套:1 是 2 的倚音,2 是 3 的倚音
32
+ example: `前倚音: @grace(宿主, 倚音, side=pre) 后倚音: @grace(宿主, 倚音, side=post)
33
+ 语法糖:'>' 与 '<',箭头永远指向宿主
34
+ 2>1 前倚音:2 是 1 的倚音,画在 1 的左上角
35
+ 1<2 后倚音:2 是 1 的倚音,画在 1 的右上角
36
+ {3 2}>1 多个倚音用大括号括起来
37
+ 1>2>3 允许嵌套:1 是 2 的倚音,2 是 3 的倚音
38
38
  倚音默认就是八分音符(自带一条减时线),再写 '/' 变成十六分音符`,
39
39
  allowExtraArgs: true,
40
40
  args: [
@@ -130,22 +130,6 @@ class GraceFunction extends ASTFunctionNode {
130
130
  }
131
131
  throw new ErrorDiagnostic("E_GRACE_NO_TARGET", "@grace语法糖错误: 右边没有找到目标", n.span);
132
132
  }
133
- /**
134
- * 语义确定后,把提升上来的修饰交还给宿主成员
135
- *
136
- * lowering 期间修饰必须挂在复合节点上,自动连梁之类的 augmenter 才看得到它的节奏;
137
- * 但渲染上「宿主代表整个复合体」,减时线要画在宿主音符下面而不是整个盒子下面。
138
- */
139
- static loweringFinalize = (result) => {
140
- for (const column of result.columns) {
141
- for (const node of column) {
142
- if (!(node instanceof GraceTemporal) || !node.addon)
143
- continue;
144
- node.host.addon = node.addon;
145
- node.addon = void 0;
146
- }
147
- }
148
- };
149
133
  host;
150
134
  grace;
151
135
  side = "pre";
@@ -225,6 +209,9 @@ class GraceFunction extends ASTFunctionNode {
225
209
  for (const content of order) {
226
210
  const events = ctx.trackedEvents(content, 0, track).columns.flat();
227
211
  const visible = events.filter(isVisualTemporalNode);
212
+ // 并行分支的纵向关系由引擎解 Track 树,而折叠成员进不了引擎
213
+ if (events.some(event => event.track !== track))
214
+ throw new ErrorDiagnostic("E_GRACE_PARALLEL_CONTENT", "@grace 的内容不能包含多声部结构(& / @voices);要在倚音里叠音请用 ^", content.sourceSpan);
228
215
  if (content === this.host) {
229
216
  if (visible.length !== 1 || visible.length !== events.length) {
230
217
  throw new ErrorDiagnostic("E_GRACE_INVALID_HOST", "@grace 的宿主必须恰好产生一个可见 Temporal", content.sourceSpan);
@@ -281,7 +268,7 @@ export class GraceTemporal extends TemporalNodeBase {
281
268
  this.side = side;
282
269
  this.t = 0;
283
270
  this.T = host.T;
284
- this.type = host.type;
271
+ this.mergeKey = host.mergeKey;
285
272
  this.initLayoutBox();
286
273
  // 宿主携带整体节奏,它的修饰暂时成为复合体的修饰,自动连梁才看得到
287
274
  if (host.addon)
@@ -322,6 +309,12 @@ export class GraceTemporal extends TemporalNodeBase {
322
309
  * 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
323
310
  */
324
311
  prepareLayout(context) {
312
+ // lowering 期间修饰挂在复合体上(augmenter 要看到整体节奏),渲染时交给宿主:
313
+ // 减时线要落在宿主数字与下八度点之间,而不是压在整个倚音盒下面
314
+ if (this.addon) {
315
+ this.host.addon = this.addon;
316
+ this.addon = void 0;
317
+ }
325
318
  for (const grace of this.graces)
326
319
  prepareLayoutHost(grace, context);
327
320
  prepareLayoutHost(this.host, context);
@@ -4,7 +4,7 @@ class KeyFunction extends ASTFunctionNode {
4
4
  static def = {
5
5
  name: ["key", "1"],
6
6
  description: "设置时间线上的 1= 调性基准",
7
- example: `@1(C4) 或 @key(F#3)
7
+ example: `@1(C4) 或 @key(F#3)
8
8
  它不会修改 parser 的局部变量,而是在时间固化阶段影响其后的数字音名解释`,
9
9
  allowExtraArgs: false,
10
10
  args: [
@@ -32,7 +32,6 @@ class KeyTemporalNode extends TemporalNodeBase {
32
32
  constructor(ast) {
33
33
  super();
34
34
  this.ast = ast;
35
- this.type = 1 /* ColType.SINGLE */;
36
35
  }
37
36
  onTimeState(state) {
38
37
  state.keySignature = this.ast.tonality;
@@ -6,20 +6,20 @@ class NoteFunction extends ASTFunctionNode {
6
6
  static def = {
7
7
  name: ["note", "n"],
8
8
  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'
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'
23
23
  `,
24
24
  allowExtraArgs: false,
25
25
  args: [
@@ -107,7 +107,7 @@ class NoteFunction extends ASTFunctionNode {
107
107
  }
108
108
  }
109
109
  export const NoteNode = NoteFunction;
110
- import { TemporalNodeBase } from "../../lowering/types.js";
110
+ import { DEFAULT_KEY, TemporalNodeBase } from "../../lowering/types.js";
111
111
  import { resolveLetterNameToJianpu, resolveNoteMidi } from "../../parser/parse-utils/note-utils.js";
112
112
  // 8 是隐形占位,9 是只打拍不发音的节拍记号,空串表示不绘制
113
113
  const NOTE_GLYPH = { "8": "", "9": "X" };
@@ -139,7 +139,7 @@ class NoteTemporalNode extends TemporalNodeBase {
139
139
  this.ast = ast;
140
140
  this.T = 1;
141
141
  this.t = 0;
142
- this.type = 2 /* ColType.DEFAULT */;
142
+ this.mergeKey = DEFAULT_KEY;
143
143
  this.name = ast.name;
144
144
  this.acc = ast.acc;
145
145
  this.octave = ast.octave;
@@ -4,11 +4,11 @@ class SetFunction extends ASTFunctionNode {
4
4
  static def = {
5
5
  name: ["set"],
6
6
  description: "设置局部默认值",
7
- example: `@set(fontSize=20) 则当前块内默认字体变为20px
8
- 可以设置其他函数的默认值: {函数名}.{属性}=值,例:
9
- @set(note.octave=5, note.color=#f00)
10
-
11
- 注意:@set 作用域仅在其所处 '{}' 内、其出现之后。离开当前作用域会恢复为之前的设置。此设置和时序无关,类似于局部变量。
7
+ example: `@set(fontSize=20) 则当前块内默认字体变为20px
8
+ 可以设置其他函数的默认值: {函数名}.{属性}=值,例:
9
+ @set(note.octave=5, note.color=#f00)
10
+
11
+ 注意:@set 作用域仅在其所处 '{}' 内、其出现之后。离开当前作用域会恢复为之前的设置。此设置和时序无关,类似于局部变量。
12
12
  `,
13
13
  allowExtraArgs: true,
14
14
  args: []
@@ -5,23 +5,23 @@ import { ErrorDiagnostic } from "../../diagnostic.js";
5
5
  * 间隙作用在盒边界之间,因此即使很小也不会互相穿模
6
6
  */
7
7
  const GAP_RATIO = 0.4;
8
+ const placeAbove = (host, group, gap) => host.top - gap * GAP_RATIO - group.bottom;
8
9
  /**
9
- * stack 的成员依次贴在宿主上方:书写顺序即自下而上
10
- * cursor 从宿主已占用的最上沿开始,因此同一宿主上的多个分组会自内向外依次堆叠
10
+ * stack 的成员在局部坐标中按书写顺序自下而上排列
11
11
  */
12
- const arrangeAbove = (host, members, gap) => {
12
+ const measureAbove = (members, gap) => {
13
13
  const step = gap * GAP_RATIO;
14
14
  const placements = [];
15
- let cursor = host.top;
15
+ let cursor = 0;
16
16
  for (const extent of members) {
17
17
  // 临时伴奏在本行没有内容就不占位,避免共用音轨白白抬高行高
18
18
  if (!extent) {
19
19
  placements.push(null);
20
20
  continue;
21
21
  }
22
- const offset = cursor - step - extent.bottom;
22
+ const offset = cursor - extent.bottom;
23
23
  placements.push({ offset, extent });
24
- cursor = offset + extent.top;
24
+ cursor = offset + extent.top - step;
25
25
  }
26
26
  return placements;
27
27
  };
@@ -32,16 +32,17 @@ const arrangeAbove = (host, members, gap) => {
32
32
  */
33
33
  const STACK_TRACKS = {
34
34
  laneKey: "stack",
35
- arrange: arrangeAbove,
35
+ measure: measureAbove,
36
+ place: placeAbove,
36
37
  };
37
38
  class StackFunction extends ASTFunctionNode {
38
39
  static def = {
39
40
  name: ["stack"],
40
- description: "时间对齐的上下层叠",
41
- example: `@stack(content1, content2, ...)
42
- 语法糖: &
43
- {content1} & {content2} & ...
44
- 表示content1和content2在时间上完全重叠,通常用于和声等需要对齐的场景。可以有任意多个参数,至少需要两个参数。
41
+ description: "临时多声部",
42
+ example: `@stack(content1, content2, ...)
43
+ 语法糖: &
44
+ {content1} & {content2} & ...
45
+ 表示content1和content2在时间上完全重叠,通常用于和声等需要对齐的场景。可以有任意多个参数,至少需要两个参数。
45
46
  `,
46
47
  allowExtraArgs: true,
47
48
  extraArgType: "content",