jpfun 0.2.0 → 0.4.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.
Files changed (167) hide show
  1. package/README.md +141 -11
  2. package/dist/converter/midi/convert.d.ts +50 -0
  3. package/dist/converter/midi/convert.js +700 -0
  4. package/dist/converter/midi/index.d.ts +5 -0
  5. package/dist/converter/midi/index.js +11 -0
  6. package/dist/converter/midi/json.d.ts +38 -0
  7. package/dist/converter/midi/json.js +1 -0
  8. package/dist/converter/midi/triplet.d.ts +30 -0
  9. package/dist/converter/midi/triplet.js +75 -0
  10. package/dist/converter/musicxml/dom.d.ts +25 -0
  11. package/dist/converter/musicxml/dom.js +65 -0
  12. package/dist/converter/musicxml/features.d.ts +51 -0
  13. package/dist/converter/musicxml/features.js +232 -0
  14. package/dist/converter/musicxml/index.d.ts +9 -0
  15. package/dist/converter/musicxml/index.js +1140 -0
  16. package/dist/converter/musicxml/model.d.ts +123 -0
  17. package/dist/converter/musicxml/model.js +1 -0
  18. package/dist/converter/source.d.ts +22 -0
  19. package/dist/converter/source.js +51 -0
  20. package/dist/core.d.ts +22 -0
  21. package/dist/core.js +22 -0
  22. package/dist/diagnostic.js +5 -2
  23. package/dist/fraction.d.ts +24 -0
  24. package/dist/fraction.js +132 -0
  25. package/dist/functions/ASTtypes.d.ts +51 -31
  26. package/dist/functions/ASTtypes.js +59 -38
  27. package/dist/functions/adjust/index.d.ts +2 -0
  28. package/dist/functions/adjust/index.js +193 -0
  29. package/dist/functions/arpeggio/index.d.ts +2 -0
  30. package/dist/functions/arpeggio/index.js +180 -0
  31. package/dist/functions/arpeggio/shape.d.ts +7 -0
  32. package/dist/functions/arpeggio/shape.js +122 -0
  33. package/dist/functions/bar/index.d.ts +8 -0
  34. package/dist/functions/bar/index.js +62 -25
  35. package/dist/functions/beam/auto.d.ts +1 -1
  36. package/dist/functions/beam/auto.js +33 -16
  37. package/dist/functions/beam/index.js +10 -6
  38. package/dist/functions/beam/layout.d.ts +4 -3
  39. package/dist/functions/beam/layout.js +43 -65
  40. package/dist/functions/box/index.js +37 -36
  41. package/dist/functions/br/index.js +10 -8
  42. package/dist/functions/dash/index.js +40 -7
  43. package/dist/functions/default.js +19 -8
  44. package/dist/functions/div/index.d.ts +16 -0
  45. package/dist/functions/div/index.js +45 -17
  46. package/dist/functions/dot/index.js +23 -25
  47. package/dist/functions/dyn/index.d.ts +2 -0
  48. package/dist/functions/dyn/index.js +242 -0
  49. package/dist/functions/grace/index.d.ts +8 -11
  50. package/dist/functions/grace/index.js +84 -79
  51. package/dist/functions/head/index.d.ts +2 -0
  52. package/dist/functions/head/index.js +496 -0
  53. package/dist/functions/key/index.js +98 -3
  54. package/dist/functions/meter/index.d.ts +2 -0
  55. package/dist/functions/meter/index.js +155 -0
  56. package/dist/functions/note/accidentals.d.ts +24 -3
  57. package/dist/functions/note/accidentals.js +31 -1
  58. package/dist/functions/note/index.js +106 -79
  59. package/dist/functions/note/noteNameFSM.js +52 -63
  60. package/dist/functions/page/index.d.ts +11 -2
  61. package/dist/functions/page/index.js +65 -4
  62. package/dist/functions/program/index.d.ts +2 -0
  63. package/dist/functions/program/index.js +30 -0
  64. package/dist/functions/set/index.js +43 -32
  65. package/dist/functions/stack/index.js +26 -22
  66. package/dist/functions/symbol/index.d.ts +31 -0
  67. package/dist/functions/symbol/index.js +139 -0
  68. package/dist/functions/symbol/ornament.d.ts +15 -0
  69. package/dist/functions/symbol/ornament.js +73 -0
  70. package/dist/functions/symbol/shape.d.ts +24 -0
  71. package/dist/functions/symbol/shape.js +52 -0
  72. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  73. package/dist/functions/symbol/symbols/accent.js +24 -0
  74. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  75. package/dist/functions/symbol/symbols/dc.js +82 -0
  76. package/dist/functions/symbol/symbols/ds.d.ts +2 -0
  77. package/dist/functions/symbol/symbols/ds.js +93 -0
  78. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  79. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  80. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  81. package/dist/functions/symbol/symbols/fermata.js +24 -0
  82. package/dist/functions/symbol/symbols/fine.d.ts +2 -0
  83. package/dist/functions/symbol/symbols/fine.js +12 -0
  84. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  85. package/dist/functions/symbol/symbols/mordent.js +56 -0
  86. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  87. package/dist/functions/symbol/symbols/prall.js +42 -0
  88. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  89. package/dist/functions/symbol/symbols/segno.js +82 -0
  90. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  91. package/dist/functions/symbol/symbols/tr.js +71 -0
  92. package/dist/functions/tempo/glyph.d.ts +12 -0
  93. package/dist/functions/tempo/glyph.js +30 -0
  94. package/dist/functions/tempo/index.js +57 -4
  95. package/dist/functions/temporal.d.ts +138 -0
  96. package/dist/functions/temporal.js +101 -0
  97. package/dist/functions/text/index.js +86 -27
  98. package/dist/functions/tie/index.js +136 -152
  99. package/dist/functions/tuplet/index.d.ts +2 -0
  100. package/dist/functions/tuplet/index.js +218 -0
  101. package/dist/functions/up/index.d.ts +1 -0
  102. package/dist/functions/up/index.js +258 -182
  103. package/dist/functions/voice/index.js +203 -109
  104. package/dist/functions/volta/index.d.ts +2 -0
  105. package/dist/functions/volta/index.js +230 -0
  106. package/dist/iife/core.d.ts +1 -0
  107. package/dist/iife/core.js +3 -0
  108. package/dist/iife/from-midi.d.ts +1 -0
  109. package/dist/iife/from-midi.js +15 -0
  110. package/dist/iife/from-musicxml.d.ts +1 -0
  111. package/dist/iife/from-musicxml.js +3 -0
  112. package/dist/iife/full.d.ts +1 -0
  113. package/dist/iife/full.js +3 -0
  114. package/dist/index.d.ts +3 -17
  115. package/dist/index.js +3 -17
  116. package/dist/jpfun.core.min.js +153 -0
  117. package/dist/jpfun.from-midi.min.js +9 -0
  118. package/dist/jpfun.from-musicxml.min.js +12 -0
  119. package/dist/jpfun.min.js +166 -0
  120. package/dist/layout/default.js +2 -3
  121. package/dist/layout/engine.d.ts +6 -12
  122. package/dist/layout/engine.js +115 -64
  123. package/dist/layout/model.d.ts +12 -5
  124. package/dist/layout/model.js +31 -18
  125. package/dist/layout/page.js +5 -4
  126. package/dist/layout/path.d.ts +19 -0
  127. package/dist/layout/path.js +101 -0
  128. package/dist/layout/types.d.ts +71 -20
  129. package/dist/layout/types.js +3 -1
  130. package/dist/lowering/loweringContext.d.ts +20 -20
  131. package/dist/lowering/loweringContext.js +153 -101
  132. package/dist/lowering/track.d.ts +5 -9
  133. package/dist/lowering/track.js +7 -2
  134. package/dist/lowering/types.d.ts +26 -112
  135. package/dist/lowering/types.js +1 -92
  136. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  137. package/dist/parser/parse-utils/call-utils.js +0 -6
  138. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  139. package/dist/parser/parse-utils/note-utils.js +35 -13
  140. package/dist/parser/parse-utils/string-utils.d.ts +10 -0
  141. package/dist/parser/parse-utils/string-utils.js +24 -0
  142. package/dist/parser/parserContext.d.ts +11 -9
  143. package/dist/parser/parserContext.js +65 -55
  144. package/dist/parser/preprocess.js +17 -11
  145. package/dist/pipeline.d.ts +6 -5
  146. package/dist/pipeline.js +4 -5
  147. package/dist/playback/compile.d.ts +18 -0
  148. package/dist/playback/compile.js +414 -0
  149. package/dist/playback/event.d.ts +105 -0
  150. package/dist/playback/event.js +105 -0
  151. package/dist/playback/time.d.ts +14 -0
  152. package/dist/playback/time.js +81 -0
  153. package/dist/playback/types.d.ts +129 -0
  154. package/dist/playback/types.js +3 -0
  155. package/dist/render/canvas.d.ts +4 -4
  156. package/dist/render/canvas.js +20 -5
  157. package/dist/render/index.d.ts +1 -0
  158. package/dist/render/index.js +1 -0
  159. package/dist/render/paint.d.ts +8 -0
  160. package/dist/render/paint.js +101 -0
  161. package/dist/render/svg.d.ts +3 -3
  162. package/dist/render/svg.js +10 -8
  163. package/dist/render/text.d.ts +22 -0
  164. package/dist/render/text.js +42 -0
  165. package/dist/render/types.d.ts +1 -1
  166. package/grammar.md +132 -13
  167. package/package.json +19 -6
@@ -0,0 +1,230 @@
1
+ import { ANCHOR_KEY, isVisualTemporalNode, } from "../temporal.js";
2
+ import { repeatPass } from "../bar/index.js";
3
+ import { ASTFunctionNode, } from "../ASTtypes.js";
4
+ import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
5
+ class VoltaFunction extends ASTFunctionNode {
6
+ static def = {
7
+ name: ["volta"],
8
+ description: "反复房子",
9
+ example: `@volta(from, to, pass, ...) 只在指定遍数演奏的一段区间
10
+ from 和 to 是区间首末音符的标签,因此房子可以横跨谱面行
11
+ pass 是遍数:整段谱面演奏到第几遍时才播这里,必填正整数
12
+ 再写几个遍数就是几遍共用一个房子,顺序随意,标签按升序写成 1.2.
13
+
14
+ |: 1 2 | 3@a 4@b :| 5@c 6@d | 7
15
+ @volta(a, b, 1) @volta(c, d, 2)`,
16
+ allowExtraArgs: true,
17
+ extraArgType: "number",
18
+ args: [
19
+ {
20
+ name: "from",
21
+ type: "label",
22
+ default: null,
23
+ },
24
+ {
25
+ name: "to",
26
+ type: "label",
27
+ default: null,
28
+ },
29
+ {
30
+ // 不给默认值:房子上的数字本来就是谱面的一部分,漏写会让整段被静默跳过
31
+ name: "pass",
32
+ type: "number",
33
+ default: null,
34
+ },
35
+ ],
36
+ };
37
+ from;
38
+ to;
39
+ /** 进入本房子的遍数,升序去重 */
40
+ passes;
41
+ /** 区间没有可见宿主时括线的尺寸基准 */
42
+ size;
43
+ font;
44
+ /** volta 不推进时间,只把 AST 端点解析成稳定的 temporal 引用 */
45
+ loweringEnter(ctx) {
46
+ let from = ctx.getTemporalNodes(this.from).at(0);
47
+ let to = ctx.getTemporalNodes(this.to).at(-1);
48
+ while (from?.foldedInto)
49
+ from = from.foldedInto;
50
+ while (to?.foldedInto)
51
+ to = to.foldedInto;
52
+ if (!from || !to) {
53
+ ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("volta", this.sourceSpan));
54
+ return [];
55
+ }
56
+ ctx.addAttachment(new VoltaAttachment(this, from, to));
57
+ return [];
58
+ }
59
+ constructor(span, args, ctx, parent = null) {
60
+ super(span, parent);
61
+ const [from, to, pass] = this.getArgValue(args, ctx);
62
+ this.font = ctx.variables.numberfont;
63
+ // 第一个遍数走 def.args,其余的落在 extraArgType 里,按位置索引取
64
+ const passes = new Set([pass]);
65
+ for (let i = 3; args.has(i); i++)
66
+ passes.add(args.get(i));
67
+ this.from = from;
68
+ this.to = to;
69
+ this.passes = [...passes].sort((a, b) => a - b);
70
+ this.size = ctx.variables.fontsize;
71
+ if (this.passes.some(value => !Number.isSafeInteger(value) || value <= 0)) {
72
+ throw new ErrorDiagnostic("E_VOLTA_INVALID_PASS", "@volta 的遍数必须是正整数", span);
73
+ }
74
+ }
75
+ toString(source) {
76
+ const name = (node) => node.label ?? node.toString(source);
77
+ return `@volta(${name(this.from)}, ${name(this.to)}, ${this.passes.join(", ")})`;
78
+ }
79
+ }
80
+ export const VoltaNode = VoltaFunction;
81
+ /** 取 ANCHOR 列的几何宿主 */
82
+ function barIn(column) {
83
+ if (column?.[0]?.mergeKey !== ANCHOR_KEY)
84
+ return undefined;
85
+ return column[0];
86
+ }
87
+ class VoltaAttachment {
88
+ ast;
89
+ from;
90
+ to;
91
+ layer = "foreground";
92
+ constructor(ast, from, to) {
93
+ this.ast = ast;
94
+ this.from = from;
95
+ this.to = to;
96
+ }
97
+ get sourceSpan() { return this.ast.sourceSpan; }
98
+ /** 房子只决定跳不跳过本列;遍数怎么数、什么时候回跳都归小节线 */
99
+ playbackFlow(columnOf) {
100
+ const from = columnOf(this.from);
101
+ const to = columnOf(this.to);
102
+ if (from === undefined || to === undefined)
103
+ return;
104
+ const start = Math.min(from, to);
105
+ const end = Math.max(from, to);
106
+ return {
107
+ range: [start, end],
108
+ // 这里还是会和 bar 耦合。但只有bar有反复功能,不值得抽取为系统机制
109
+ run: cursor => this.ast.passes.includes(repeatPass(cursor, start))
110
+ ? undefined
111
+ : { kind: "jump", column: end + 1 },
112
+ };
113
+ }
114
+ createGeometry(context) {
115
+ if (!isVisualTemporalNode(this.from) || !isVisualTemporalNode(this.to)) {
116
+ return { regions: [], paint() { } };
117
+ }
118
+ const locate = (node) => {
119
+ const view = context.lines[node.layoutLine];
120
+ return { line: node.layoutLine, view, column: view.columnOf(node) };
121
+ };
122
+ // 两个标签谁写在前面不一定,按谱面顺序摆正
123
+ let [from, to] = [locate(this.from), locate(this.to)];
124
+ if (from.line > to.line || (from.line === to.line && from.column > to.column)) {
125
+ [from, to] = [to, from];
126
+ }
127
+ if (from.column < 0 || to.column < 0)
128
+ return { regions: [], paint() { } };
129
+ const head = from.view.columns[from.column][0];
130
+ const tail = to.view.columns[to.column][0];
131
+ const leftBar = barIn(from.view.columns[from.column - 1]);
132
+ const rightBar = barIn(to.view.columns[to.column + 1]);
133
+ let span = 0;
134
+ const coverage = [];
135
+ // 括线盖住的是:首行从起点列向右、末行向左到终点列、中间行整行
136
+ for (let line = from.line; line <= to.line; line++) {
137
+ const view = context.lines[line];
138
+ const start = line === from.line ? from.column : 0;
139
+ const end = line === to.line ? to.column : view.columns.length - 1;
140
+ for (let column = start; column <= end; column++) {
141
+ span = Math.max(span, view.columns[column][0].ast.size);
142
+ }
143
+ coverage.push({ from: start, to: end, wholeLine: line !== from.line && line !== to.line });
144
+ }
145
+ const firstLine = head.layoutLine;
146
+ const lastLine = tail.layoutLine;
147
+ const size = span || this.ast.size;
148
+ const style = {
149
+ fontSize: size * 0.6,
150
+ fontFamily: this.ast.font,
151
+ textAlign: "left",
152
+ fill: "#000",
153
+ };
154
+ const text = this.ast.passes.map(pass => `${pass}.`).join("");
155
+ const metrics = context.textMeasurer.measureText(text, style);
156
+ const strokeWidth = Math.max(1, size * 0.055);
157
+ const halfStroke = strokeWidth / 2;
158
+ const hook = metrics.h * 1.2;
159
+ // 标签在下折钩的高度里垂直居中
160
+ const textY = (hook - metrics.h) / 2 + metrics.baseline;
161
+ // 中间行可能没有本房子的可见对象,仍要补满,所以按行号连续遍历
162
+ const draws = Array.from({ length: lastLine - firstLine + 1 }, (_, offset) => {
163
+ const line = firstLine + offset;
164
+ let topTrack = head.track;
165
+ let hostTop = Infinity;
166
+ const range = coverage[offset];
167
+ const extents = range.wholeLine
168
+ ? context.getRangeExtents(line)
169
+ : context.getRangeExtents(line, [range.from, range.to]);
170
+ for (const [track, extent] of extents) {
171
+ const y = context.getVisualAxis(line, track) + extent.top;
172
+ if (y < hostTop) {
173
+ topTrack = track;
174
+ hostTop = y;
175
+ }
176
+ }
177
+ // 整行空白时没有主体可以参照,退到轨道轴上
178
+ if (hostTop === Infinity)
179
+ hostTop = context.getVisualAxis(line, topTrack);
180
+ // 换行断点跟随系统的视觉边界;空行没有主体时仍退到内容区页边
181
+ const rawLeft = line !== firstLine ? this.lineEntryX(context, line, topTrack)
182
+ : leftBar?.layoutLine === line ? leftBar.box.x + leftBar.box.anchor + 5 // 5是防止前后房子竖线重合
183
+ : head.box.x + (head.ports["body.left"]?.x ?? 0);
184
+ const rawRight = line !== lastLine ? this.lineRightX(context, line)
185
+ : rightBar?.layoutLine === line ? rightBar.box.x + rightBar.box.anchor
186
+ : tail.box.x + (tail.ports["body.right"]?.x ?? tail.box.w);
187
+ const left = Math.min(rawLeft, rawRight);
188
+ const right = Math.max(rawLeft, rawRight);
189
+ return { left, right, lineY: hostTop - size * 0.2 - hook, line, track: topTrack };
190
+ });
191
+ return {
192
+ regions: draws.map(({ left, right, lineY, line, track }) => ({
193
+ x: left - halfStroke,
194
+ y: lineY - halfStroke,
195
+ w: right - left + strokeWidth,
196
+ h: hook + strokeWidth,
197
+ line,
198
+ track,
199
+ })),
200
+ paint(painter) {
201
+ const lineStyle = { stroke: "#000", strokeWidth };
202
+ for (const { left, right, lineY, line } of draws) {
203
+ painter.drawLine(left, lineY, right, lineY, lineStyle);
204
+ // 只有房子真正开始和结束的那一端才下折
205
+ if (line === firstLine) {
206
+ painter.drawLine(left, lineY, left, lineY + hook, lineStyle);
207
+ painter.drawText(text, left + size * 0.15, lineY + textY + 2, style);
208
+ }
209
+ if (line === lastLine)
210
+ painter.drawLine(right, lineY, right, lineY + hook, lineStyle);
211
+ }
212
+ },
213
+ };
214
+ }
215
+ lineEntryX(context, line, track) {
216
+ const host = context.lines[line].trackRuns.get(track)?.find(item => !item.T.isZero());
217
+ return host?.box.x ?? context.originX;
218
+ }
219
+ lineRightX(context, line) {
220
+ const runs = context.lines[line].trackRuns;
221
+ if (runs.size === 0)
222
+ return context.originX + context.width;
223
+ let right = context.originX;
224
+ for (const run of runs.values()) {
225
+ for (const host of run)
226
+ right = Math.max(right, host.box.x + host.box.w);
227
+ }
228
+ return right;
229
+ }
230
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import * as core from "../core.js";
2
+ const target = globalThis;
3
+ Object.assign(target.jpfun ??= {}, core);
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ import { convertMidiJsonToJpFun, } from "../converter/midi/convert.js";
2
+ const target = globalThis;
3
+ const api = target.jpfun ??= {};
4
+ Object.assign(api, {
5
+ midiJsonToJpFun(input, options = {}) {
6
+ const layoutCapability = (options.barsPerLine ?? 0) <= 0
7
+ ? {
8
+ compileScore: api.compileScore,
9
+ anchorKey: api.ANCHOR_KEY,
10
+ page: api.DEFAULT_PAGE_CONFIG,
11
+ }
12
+ : undefined;
13
+ return convertMidiJsonToJpFun(input, options, layoutCapability);
14
+ },
15
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { musicXmlToJpFun } from "../converter/musicxml/index.js";
2
+ const target = globalThis;
3
+ Object.assign(target.jpfun ??= {}, { musicXmlToJpFun });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import * as full from "../index.js";
2
+ const target = globalThis;
3
+ Object.assign(target.jpfun ??= {}, full);
package/dist/index.d.ts CHANGED
@@ -1,17 +1,3 @@
1
- export * from "./pipeline.js";
2
- export * from "./functions/ASTtypes.js";
3
- export * from "./functions/default.js";
4
- export * from "./diagnostic.js";
5
- export * from "./parser/types.js";
6
- export * from "./parser/grammarType.js";
7
- export * from "./parser/parserContext.js";
8
- export * from "./parser/preprocess.js";
9
- export * from "./lowering/types.js";
10
- export * from "./lowering/loweringContext.js";
11
- export * from "./lowering/track.js";
12
- export * from "./layout/types.js";
13
- export * from "./layout/model.js";
14
- export * from "./layout/default.js";
15
- export * from "./layout/page.js";
16
- export * from "./layout/engine.js";
17
- export * from "./render/index.js";
1
+ export * from "./core.js";
2
+ export * from "./converter/midi/index.js";
3
+ export * from "./converter/musicxml/index.js";
package/dist/index.js CHANGED
@@ -1,17 +1,3 @@
1
- export * from "./pipeline.js";
2
- export * from "./functions/ASTtypes.js";
3
- export * from "./functions/default.js";
4
- export * from "./diagnostic.js";
5
- export * from "./parser/types.js";
6
- export * from "./parser/grammarType.js";
7
- export * from "./parser/parserContext.js";
8
- export * from "./parser/preprocess.js";
9
- export * from "./lowering/types.js";
10
- export * from "./lowering/loweringContext.js";
11
- export * from "./lowering/track.js";
12
- export * from "./layout/types.js";
13
- export * from "./layout/model.js";
14
- export * from "./layout/default.js";
15
- export * from "./layout/page.js";
16
- export * from "./layout/engine.js";
17
- export * from "./render/index.js";
1
+ export * from "./core.js";
2
+ export * from "./converter/midi/index.js";
3
+ export * from "./converter/musicxml/index.js";