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.
Files changed (104) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +134 -0
  3. package/dist/diagnostic.d.ts +27 -0
  4. package/dist/diagnostic.js +141 -0
  5. package/dist/functions/ASTtypes.d.ts +115 -0
  6. package/dist/functions/ASTtypes.js +187 -0
  7. package/dist/functions/bar/index.d.ts +2 -0
  8. package/dist/functions/bar/index.js +180 -0
  9. package/dist/functions/beam/auto.d.ts +4 -0
  10. package/dist/functions/beam/auto.js +202 -0
  11. package/dist/functions/beam/index.d.ts +2 -0
  12. package/dist/functions/beam/index.js +54 -0
  13. package/dist/functions/beam/layout.d.ts +9 -0
  14. package/dist/functions/beam/layout.js +233 -0
  15. package/dist/functions/box/index.d.ts +2 -0
  16. package/dist/functions/box/index.js +202 -0
  17. package/dist/functions/br/index.d.ts +2 -0
  18. package/dist/functions/br/index.js +83 -0
  19. package/dist/functions/dash/index.d.ts +2 -0
  20. package/dist/functions/dash/index.js +61 -0
  21. package/dist/functions/default.d.ts +2 -0
  22. package/dist/functions/default.js +32 -0
  23. package/dist/functions/div/index.d.ts +50 -0
  24. package/dist/functions/div/index.js +190 -0
  25. package/dist/functions/dot/index.d.ts +2 -0
  26. package/dist/functions/dot/index.js +160 -0
  27. package/dist/functions/grace/index.d.ts +84 -0
  28. package/dist/functions/grace/index.js +464 -0
  29. package/dist/functions/key/index.d.ts +2 -0
  30. package/dist/functions/key/index.js +40 -0
  31. package/dist/functions/note/accidentals.d.ts +10 -0
  32. package/dist/functions/note/accidentals.js +60 -0
  33. package/dist/functions/note/index.d.ts +2 -0
  34. package/dist/functions/note/index.js +284 -0
  35. package/dist/functions/note/noteNameFSM.d.ts +8 -0
  36. package/dist/functions/note/noteNameFSM.js +192 -0
  37. package/dist/functions/over/index.d.ts +2 -0
  38. package/dist/functions/over/index.js +307 -0
  39. package/dist/functions/page/index.d.ts +20 -0
  40. package/dist/functions/page/index.js +63 -0
  41. package/dist/functions/set/index.d.ts +2 -0
  42. package/dist/functions/set/index.js +55 -0
  43. package/dist/functions/stack/index.d.ts +2 -0
  44. package/dist/functions/stack/index.js +164 -0
  45. package/dist/functions/tempo/index.d.ts +2 -0
  46. package/dist/functions/tempo/index.js +37 -0
  47. package/dist/functions/text/index.d.ts +2 -0
  48. package/dist/functions/text/index.js +65 -0
  49. package/dist/functions/tie/index.d.ts +2 -0
  50. package/dist/functions/tie/index.js +301 -0
  51. package/dist/functions/voice/index.d.ts +3 -0
  52. package/dist/functions/voice/index.js +714 -0
  53. package/dist/index.d.ts +17 -0
  54. package/dist/index.js +17 -0
  55. package/dist/layout/default.d.ts +12 -0
  56. package/dist/layout/default.js +27 -0
  57. package/dist/layout/engine.d.ts +47 -0
  58. package/dist/layout/engine.js +410 -0
  59. package/dist/layout/model.d.ts +73 -0
  60. package/dist/layout/model.js +422 -0
  61. package/dist/layout/page.d.ts +27 -0
  62. package/dist/layout/page.js +121 -0
  63. package/dist/layout/types.d.ts +197 -0
  64. package/dist/layout/types.js +1 -0
  65. package/dist/lowering/loweringContext.d.ts +126 -0
  66. package/dist/lowering/loweringContext.js +625 -0
  67. package/dist/lowering/track.d.ts +61 -0
  68. package/dist/lowering/track.js +35 -0
  69. package/dist/lowering/types.d.ts +176 -0
  70. package/dist/lowering/types.js +82 -0
  71. package/dist/parser/grammarType.d.ts +81 -0
  72. package/dist/parser/grammarType.js +1 -0
  73. package/dist/parser/parse-utils/brace-utils.d.ts +1 -0
  74. package/dist/parser/parse-utils/brace-utils.js +29 -0
  75. package/dist/parser/parse-utils/call-utils.d.ts +24 -0
  76. package/dist/parser/parse-utils/call-utils.js +178 -0
  77. package/dist/parser/parse-utils/label-utils.d.ts +4 -0
  78. package/dist/parser/parse-utils/label-utils.js +17 -0
  79. package/dist/parser/parse-utils/length-utils.d.ts +3 -0
  80. package/dist/parser/parse-utils/length-utils.js +20 -0
  81. package/dist/parser/parse-utils/note-utils.d.ts +10 -0
  82. package/dist/parser/parse-utils/note-utils.js +111 -0
  83. package/dist/parser/parserContext.d.ts +93 -0
  84. package/dist/parser/parserContext.js +542 -0
  85. package/dist/parser/preprocess.d.ts +6 -0
  86. package/dist/parser/preprocess.js +193 -0
  87. package/dist/parser/types.d.ts +31 -0
  88. package/dist/parser/types.js +2 -0
  89. package/dist/pipeline.d.ts +36 -0
  90. package/dist/pipeline.js +51 -0
  91. package/dist/render/canvas.d.ts +19 -0
  92. package/dist/render/canvas.js +121 -0
  93. package/dist/render/index.d.ts +5 -0
  94. package/dist/render/index.js +5 -0
  95. package/dist/render/recording.d.ts +46 -0
  96. package/dist/render/recording.js +76 -0
  97. package/dist/render/svg.d.ts +18 -0
  98. package/dist/render/svg.js +126 -0
  99. package/dist/render/text.d.ts +6 -0
  100. package/dist/render/text.js +17 -0
  101. package/dist/render/types.d.ts +86 -0
  102. package/dist/render/types.js +1 -0
  103. package/grammar.md +123 -0
  104. package/package.json +58 -0
@@ -0,0 +1,464 @@
1
+ import { ErrorDiagnostic } from "../../diagnostic.js";
2
+ import { prepareLayoutHost } from "../../layout/engine.js";
3
+ import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
4
+ import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, } from "../ASTtypes.js";
5
+ import { claimDivLine, DIV_ADDON_KEY, divLinePortName } from "../div/index.js";
6
+ /** 倚音相对宿主的字号比例 */
7
+ const GRACE_SCALE = 0.7;
8
+ /**
9
+ * 肩线端口:要在这个盒子上方叠东西的人,应该从哪条线开始
10
+ *
11
+ * 普通音符不发布它,读取方退化成盒顶即可(倚音本来就该避开上八度点);
12
+ * 复合节点发布它,指向自己内部那个真正承载节奏的音符的顶边。
13
+ */
14
+ const SHOULDER_PORT = "shoulder";
15
+ /**
16
+ * 递归缩小一棵子树里所有已冻结的字号
17
+ *
18
+ * 字号在 parse 时就冻结进各具体函数的 size 字段,而「谁是倚音」要到关系去糖时才知道,
19
+ * 所以只能在此刻回头改。这发生在 parse 进行中,不违反「AST parse 后只读」。
20
+ */
21
+ function scaleFontSize(node, factor) {
22
+ const sized = node;
23
+ if (typeof sized.size === "number")
24
+ sized.size *= factor;
25
+ for (const child of node.children ?? [])
26
+ scaleFontSize(child, factor);
27
+ }
28
+ class GraceFunction extends ASTFunctionNode {
29
+ static def = {
30
+ name: ["grace"],
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 的倚音
38
+ 倚音默认就是八分音符(自带一条减时线),再写 '/' 变成十六分音符`,
39
+ allowExtraArgs: true,
40
+ args: [
41
+ {
42
+ name: "host",
43
+ type: "content",
44
+ default: null,
45
+ },
46
+ {
47
+ name: "grace",
48
+ type: "content",
49
+ default: null,
50
+ },
51
+ {
52
+ name: "side",
53
+ type: "string",
54
+ default: "pre",
55
+ }
56
+ ],
57
+ };
58
+ labelable() { return this; }
59
+ static deSugarAtom(source, start, _end) {
60
+ let char = source[start];
61
+ switch (char) {
62
+ case '>':
63
+ char = "pre";
64
+ break;
65
+ case '<':
66
+ char = "post";
67
+ break;
68
+ default: return null;
69
+ }
70
+ return {
71
+ next: start + 1,
72
+ node: {
73
+ kind: "sugar",
74
+ data: {
75
+ class: GraceFunction,
76
+ side: char
77
+ },
78
+ span: { start, end: start + 1 }
79
+ }
80
+ };
81
+ }
82
+ // 取左右操作数的流程同 stack / up
83
+ static deSugarRelation(ctx, nodes, at) {
84
+ const n = nodes[at++];
85
+ if (n.kind !== "sugar" || n.data?.class !== GraceFunction)
86
+ return null;
87
+ const side = n.data.side;
88
+ // 找上一个非文本节点 实现忽略中间内容的作用
89
+ let left = ctx.nodes.length - 1;
90
+ while (left >= 0 && ctx.nodes[left] instanceof ASTTextNode)
91
+ left--;
92
+ if (left < 0) {
93
+ throw new ErrorDiagnostic("E_GRACE_NO_TARGET", "@grace语法糖错误: 左边没有找到目标", n.span);
94
+ }
95
+ let leftNode = ctx.nodes[left];
96
+ /** 左操作数在 ctx.nodes 中的起点;只有这一段会被 grace 吞并 */
97
+ let replaceFrom = left;
98
+ // 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
99
+ if (leftNode instanceof ASTLabelNode) {
100
+ const tgt = leftNode.parent;
101
+ for (let j = left - 1; j >= 0; j--) {
102
+ if (ctx.nodes[j] === tgt) {
103
+ leftNode = new ASTBraceNode({
104
+ start: tgt.sourceSpan.start,
105
+ end: leftNode.sourceSpan.end,
106
+ }, ctx.nodes.slice(j, left + 1), null);
107
+ replaceFrom = j;
108
+ break;
109
+ }
110
+ }
111
+ }
112
+ // 找到下一个非文本节点 通过全量后续解析的方式进行
113
+ const storage = ctx.nodes;
114
+ ctx.nodes = [];
115
+ ctx.makeNodes(nodes, at);
116
+ for (let i = 0; i < ctx.nodes.length; i++) {
117
+ const right = ctx.nodes[i];
118
+ if (right instanceof ASTTextNode)
119
+ continue;
120
+ // '<' 的倚音在右侧,右结合的解析结果本来就是嵌套语义,不必像 '>' 那样重排
121
+ const node = side === "pre"
122
+ ? GraceFunction.attachPre(leftNode, right, n.span, ctx)
123
+ : GraceFunction.create(ctx, n.span, "post", leftNode, right);
124
+ storage.length = replaceFrom;
125
+ storage.push(node);
126
+ while (++i < ctx.nodes.length)
127
+ storage.push(ctx.nodes[i]);
128
+ ctx.nodes = storage;
129
+ return nodes.length;
130
+ }
131
+ throw new ErrorDiagnostic("E_GRACE_NO_TARGET", "@grace语法糖错误: 右边没有找到目标", n.span);
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
+ host;
150
+ grace;
151
+ side = "pre";
152
+ size;
153
+ /**
154
+ * `A > B`:前倚音必须左结合,而右操作数是把后续全部解析完才取的,
155
+ * 所以 `1>2>3` 到这里已经成了 `1>(2>3)`。把 A 插回链条最深处就能恢复为 `(1>2)>3`
156
+ */
157
+ static attachPre(left, right, span, ctx) {
158
+ if (right instanceof GraceFunction && right.side === "pre") {
159
+ // 找到最深层的 pre
160
+ let deepest = right;
161
+ let depth = 1;
162
+ while (deepest.grace instanceof GraceFunction && deepest.grace.side === "pre") {
163
+ deepest = deepest.grace;
164
+ depth++;
165
+ }
166
+ const argMap = new Map();
167
+ argMap.set(0, deepest.grace);
168
+ argMap.set(1, left);
169
+ const scale = GRACE_SCALE ** depth;
170
+ const inner = new GraceFunction(span, argMap, ctx, null, GRACE_SCALE * scale);
171
+ inner.size *= scale;
172
+ deepest.setGrace(inner, 1); // inner 内部已按深度缩放,不能再缩
173
+ return right;
174
+ }
175
+ return GraceFunction.create(ctx, span, "pre", right, left);
176
+ }
177
+ static create(ctx, span, side, host, grace) {
178
+ const argMap = new Map();
179
+ argMap.set(0, host);
180
+ argMap.set(1, grace);
181
+ argMap.set("side", side);
182
+ return new GraceFunction(span, argMap, ctx);
183
+ }
184
+ /** 宿主在前:findActiveDivs 下潜子树时要先命中宿主的 @div,而不是倚音的 */
185
+ get children() {
186
+ const out = [];
187
+ if (this.host)
188
+ out.push(this.host);
189
+ if (this.grace)
190
+ out.push(this.grace);
191
+ return out;
192
+ }
193
+ constructor(span, args, ctx, parent = null, scale = GRACE_SCALE) {
194
+ super(span, parent);
195
+ this.size = ctx.fontSize;
196
+ [this.host, this.grace, this.side] = this.getArgValue(args, ctx);
197
+ if (this.side !== "pre" && this.side !== "post") {
198
+ throw new ErrorDiagnostic("E_GRACE_INVALID_SIDE", "@grace 的 side 参数必须是 'pre' 或 'post'", span);
199
+ }
200
+ this.setGrace(this.grace, scale);
201
+ this.size = ctx.fontSize;
202
+ }
203
+ setGrace(node, factor = GRACE_SCALE) {
204
+ scaleFontSize(node, factor);
205
+ this.grace = node;
206
+ this.adopt(node);
207
+ }
208
+ adopt(node) {
209
+ node.parent = this;
210
+ this.sourceSpan.start = Math.min(this.sourceSpan.start, node.sourceSpan.start);
211
+ this.sourceSpan.end = Math.max(this.sourceSpan.end, node.sourceSpan.end);
212
+ }
213
+ /** 倚音与宿主各自收敛成可见 Temporal,然后整体折叠进一个复合节点 */
214
+ loweringEnter(ctx, track) {
215
+ if (!this.host || !this.grace) {
216
+ throw new ErrorDiagnostic("E_GRACE_MISSING_ARGS", "@grace 需要同时给出宿主和倚音", this.sourceSpan);
217
+ }
218
+ let graces = [];
219
+ let host = null;
220
+ // 成员不是外层分组的成员,复合节点才是;否则 voice 的歌词会按下标错位
221
+ ctx.isolateFromLoweringGroups(() => {
222
+ const order = this.side === "pre"
223
+ ? [this.grace, this.host]
224
+ : [this.host, this.grace];
225
+ for (const content of order) {
226
+ const events = ctx.trackedEvents(content, 0, track).columns.flat();
227
+ const visible = events.filter(isVisualTemporalNode);
228
+ if (content === this.host) {
229
+ if (visible.length !== 1 || visible.length !== events.length) {
230
+ throw new ErrorDiagnostic("E_GRACE_INVALID_HOST", "@grace 的宿主必须恰好产生一个可见 Temporal", content.sourceSpan);
231
+ }
232
+ host = visible[0];
233
+ }
234
+ else {
235
+ if (visible.length === 0 || visible.length !== events.length) {
236
+ throw new ErrorDiagnostic("E_GRACE_INVALID_CONTENT", "@grace 的倚音必须只产生可见 Temporal,且至少一个", content.sourceSpan);
237
+ }
238
+ graces = visible;
239
+ }
240
+ }
241
+ });
242
+ return [new GraceTemporal(this, host, graces, this.side)];
243
+ }
244
+ toString(source) {
245
+ const host = this.host?.toString(source) ?? "";
246
+ const grace = this.grace?.toString(source) ?? "";
247
+ return `@grace(${host}, ${grace}, side=${this.side})`;
248
+ }
249
+ }
250
+ export const GraceNode = GraceFunction;
251
+ //==== 倚音线 ====
252
+ // 固定形状的小钩,起点在最靠近宿主的那个倚音的 anchor、盒底边(后倚音镜像),
253
+ // 尺寸只跟倚音字号有关,不随倚音到宿主的距离变化。
254
+ const HOOK_COMMANDS = [
255
+ { op: "M", x: 0, y: 0 },
256
+ { op: "C", cx1: -0.01, cy1: 0.26, cx2: 0.14, cy2: 0.42, x: 0.29, y: 0.36 },
257
+ ];
258
+ const HOOK_WIDTH = 0.07;
259
+ /** 倚音块底边到宿主顶边的视觉间隙;倚音线会探进这段空间指向宿主 */
260
+ const GRACE_RISE = 0.18;
261
+ /** 倚音向宿主借时值的上限,防止宿主被偷光 */
262
+ const MAX_STEAL_RATIO = 0.75;
263
+ /** 减时线粗细占字号的比例 */
264
+ const DIV_LINE_WIDTH = 0.055;
265
+ /** 倚音复合体:宿主与倚音都折叠在这一个盒子里,对外只是一个可见事件 */
266
+ export class GraceTemporal extends TemporalNodeBase {
267
+ host;
268
+ graces;
269
+ side;
270
+ /** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
271
+ hostOffset = { x: 0, y: 0 };
272
+ graceOffsets = [];
273
+ /** 倚音线起点,同样是局部偏移 */
274
+ hookOrigin = null;
275
+ divLines = [];
276
+ constructor(ast, host, graces, side) {
277
+ super();
278
+ this.ast = ast;
279
+ this.host = host;
280
+ this.graces = graces;
281
+ this.side = side;
282
+ this.t = 0;
283
+ this.T = host.T;
284
+ this.type = host.type;
285
+ this.initLayoutBox();
286
+ // 宿主携带整体节奏,它的修饰暂时成为复合体的修饰,自动连梁才看得到
287
+ if (host.addon)
288
+ this.addon = { ...host.addon };
289
+ host.addon = void 0;
290
+ // 成员不进入全局 columns,对外由复合体代表:写在成员上的标签仍可做关系端点
291
+ host.foldedInto = this;
292
+ for (const grace of graces) {
293
+ grace.foldedInto = this;
294
+ // 倚音默认就是八分音符:补一条减时线,书面时值随之减半
295
+ const addon = grace.addon = { ...grace.addon };
296
+ addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + 1;
297
+ grace.T /= 2;
298
+ }
299
+ }
300
+ onTimeState(state) {
301
+ for (const member of [this.host, ...this.graces]) {
302
+ member.t = this.t;
303
+ member.track = this.track;
304
+ member.layoutLine = this.layoutLine;
305
+ member.onTimeState?.({ ...state });
306
+ }
307
+ }
308
+ /**
309
+ * 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
310
+ *
311
+ * 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
312
+ * 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
313
+ */
314
+ get stealTime() {
315
+ const written = this.graces.reduce((sum, grace) => sum + grace.T, 0);
316
+ return Math.min(written, MAX_STEAL_RATIO) * this.T;
317
+ }
318
+ /**
319
+ * 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
320
+ *
321
+ * 倚音成员之间自己紧排,不参与横向弹簧;整块的宽度计入复合盒,
322
+ * 因此左右邻居会被固有宽度推开,不会在空间紧张时被压穿。
323
+ */
324
+ prepareLayout(context) {
325
+ for (const grace of this.graces)
326
+ prepareLayoutHost(grace, context);
327
+ prepareLayoutHost(this.host, context);
328
+ const em = this.ast.size;
329
+ const graceEm = em * GRACE_SCALE;
330
+ const memberGap = graceEm * 0.16;
331
+ const sideGap = graceEm * 0.2;
332
+ const rise = graceEm * GRACE_RISE;
333
+ let graceWidth = 0;
334
+ let graceAxis = 0;
335
+ let graceHeight = 0;
336
+ // 倚音之间按视觉轴对齐,否则带下八度点的成员会把数字顶得比旁边高
337
+ for (const grace of this.graces)
338
+ graceAxis = Math.max(graceAxis, grace.box.visualAxis);
339
+ this.graceOffsets = this.graces.map(grace => {
340
+ const x = graceWidth;
341
+ graceWidth += grace.box.w + memberGap;
342
+ const y = graceAxis - grace.box.visualAxis;
343
+ graceHeight = Math.max(graceHeight, y + grace.box.h);
344
+ return { x, y };
345
+ });
346
+ if (this.graces.length > 0)
347
+ graceWidth -= memberGap;
348
+ // 倚音贴着宿主的肩线而不是宿主盒顶:宿主本身也是复合体时,
349
+ // 肩线由它转发上来,所以两侧倚音会落在同一高度而不是层层叠高
350
+ const shoulder = this.host.ports[SHOULDER_PORT]?.y ?? 0;
351
+ const graceTop = shoulder - rise - graceHeight;
352
+ const lift = Math.max(0, -graceTop);
353
+ const graceX = this.side === "pre" ? 0 : this.host.box.w + sideGap;
354
+ this.hostOffset = {
355
+ x: this.side === "pre" ? graceWidth + sideGap : 0,
356
+ y: lift,
357
+ };
358
+ for (const offset of this.graceOffsets) {
359
+ offset.x += graceX;
360
+ offset.y += graceTop + lift;
361
+ }
362
+ this.box.w = this.host.box.w + sideGap + graceWidth;
363
+ this.box.h = lift + this.host.box.h;
364
+ this.box.anchor = this.hostOffset.x + this.host.box.anchor;
365
+ this.box.visualAxis = lift + this.host.box.visualAxis;
366
+ // 端口原样上提:减时线、歌词、连音线都接到宿主上,关系函数不需要认识倚音
367
+ for (const name in this.host.ports) {
368
+ const port = this.host.ports[name];
369
+ this.ports[name] = {
370
+ x: this.hostOffset.x + port.x,
371
+ y: this.hostOffset.y + port.y,
372
+ };
373
+ }
374
+ // 宿主没有肩线时它自己的盒顶就是肩线;有则上面那轮转发已经带偏移抬好了
375
+ this.ports[SHOULDER_PORT] ??= { x: this.box.anchor, y: this.hostOffset.y };
376
+ this.connectGraceDivLines();
377
+ // 钩形曲线挂在靠近宿主的那个倚音成员下方
378
+ const near = this.side === "pre" ? this.graces.length - 1 : 0;
379
+ const anchorGrace = this.graces[near];
380
+ this.hookOrigin = anchorGrace ? {
381
+ x: this.graceOffsets[near].x + anchorGrace.box.anchor,
382
+ y: graceTop + lift + graceHeight,
383
+ } : null;
384
+ }
385
+ /**
386
+ * 把相邻倚音成员的同级减时线连成整条
387
+ *
388
+ * 折叠成员不进入全局时间列,自动连梁看不到它们,只能由复合体自己连。
389
+ */
390
+ connectGraceDivLines() {
391
+ this.divLines.length = 0;
392
+ if (this.graces.length < 2)
393
+ return;
394
+ for (let level = 0;; level++) {
395
+ let hasPorts = false;
396
+ let run = [];
397
+ const flush = () => {
398
+ if (run.length >= 2) {
399
+ const first = run[0];
400
+ const last = run[run.length - 1];
401
+ const leftPort = this.graces[first].ports[divLinePortName(level, "left")];
402
+ const rightPort = this.graces[last].ports[divLinePortName(level, "right")];
403
+ this.divLines.push({
404
+ x1: this.graceOffsets[first].x + leftPort.x,
405
+ x2: this.graceOffsets[last].x + rightPort.x,
406
+ y: this.graceOffsets[first].y + leftPort.y,
407
+ });
408
+ for (const index of run)
409
+ claimDivLine(this.graces[index], level);
410
+ }
411
+ run = [];
412
+ };
413
+ for (let i = 0; i < this.graces.length; i++) {
414
+ const ports = this.graces[i].ports;
415
+ const left = ports[divLinePortName(level, "left")];
416
+ const right = ports[divLinePortName(level, "right")];
417
+ if (left || right)
418
+ hasPorts = true;
419
+ if (!left || !right) {
420
+ flush();
421
+ continue;
422
+ }
423
+ run.push(i);
424
+ }
425
+ flush();
426
+ if (!hasPorts)
427
+ break;
428
+ }
429
+ }
430
+ /** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
431
+ onPlaced() {
432
+ this.host.box.x = this.box.x + this.hostOffset.x;
433
+ this.host.box.y = this.box.y + this.hostOffset.y;
434
+ this.host.onPlaced?.();
435
+ this.graces.forEach((grace, i) => {
436
+ grace.box.x = this.box.x + this.graceOffsets[i].x;
437
+ grace.box.y = this.box.y + this.graceOffsets[i].y;
438
+ grace.onPlaced?.();
439
+ });
440
+ }
441
+ paint(painter) {
442
+ for (const member of [this.host, ...this.graces]) {
443
+ member.paint(painter);
444
+ for (const decoration of member.decorations)
445
+ decoration.paint(painter);
446
+ }
447
+ const graceEm = this.ast.size * GRACE_SCALE;
448
+ const divWidth = Math.max(0.8, graceEm * DIV_LINE_WIDTH);
449
+ for (const line of this.divLines) {
450
+ const y = this.box.y + line.y;
451
+ painter.drawLine(this.box.x + line.x1, y, this.box.x + line.x2, y, { stroke: "#000", strokeWidth: divWidth });
452
+ }
453
+ if (!this.hookOrigin)
454
+ return;
455
+ const hookOrigin = this.hookOrigin;
456
+ const dir = this.side === "pre" ? 1 : -1;
457
+ painter.drawPath(HOOK_COMMANDS, { stroke: "#000", strokeWidth: Math.max(0.8, graceEm * HOOK_WIDTH) }, {
458
+ x: this.box.x + hookOrigin.x,
459
+ y: this.box.y + hookOrigin.y,
460
+ scaleX: graceEm * dir,
461
+ scaleY: graceEm,
462
+ });
463
+ }
464
+ }
@@ -0,0 +1,2 @@
1
+ import { ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const KeyNode: ASTFunctionClass;
@@ -0,0 +1,40 @@
1
+ import { TemporalNodeBase } from "../../lowering/types.js";
2
+ import { ASTFunctionNode } from "../ASTtypes.js";
3
+ class KeyFunction extends ASTFunctionNode {
4
+ static def = {
5
+ name: ["key", "1"],
6
+ description: "设置时间线上的 1= 调性基准",
7
+ example: `@1(C4) 或 @key(F#3)
8
+ 它不会修改 parser 的局部变量,而是在时间固化阶段影响其后的数字音名解释`,
9
+ allowExtraArgs: false,
10
+ args: [
11
+ {
12
+ name: "tonality",
13
+ type: "string",
14
+ default: null,
15
+ },
16
+ ],
17
+ };
18
+ tonality;
19
+ constructor(sourceSpan, args, ctx, parent = null) {
20
+ super(sourceSpan, parent);
21
+ [this.tonality] = this.getArgValue(args, ctx);
22
+ }
23
+ loweringEnter() {
24
+ return [new KeyTemporalNode(this)];
25
+ }
26
+ toString() {
27
+ return `@1(${this.tonality})`;
28
+ }
29
+ }
30
+ export const KeyNode = KeyFunction;
31
+ class KeyTemporalNode extends TemporalNodeBase {
32
+ constructor(ast) {
33
+ super();
34
+ this.ast = ast;
35
+ this.type = 1 /* ColType.SINGLE */;
36
+ }
37
+ onTimeState(state) {
38
+ state.keySignature = this.ast.tonality;
39
+ }
40
+ }
@@ -0,0 +1,10 @@
1
+ import type { PathCommand } from "../../render/types.js";
2
+ export interface PreparedAccidental {
3
+ w: number;
4
+ h: number;
5
+ baseline: number;
6
+ strokeWidth: number;
7
+ commands: readonly PathCommand[];
8
+ }
9
+ /** size 是数字字号,升降号自己决定缩小多少 */
10
+ export declare function prepareAccidental(symbol: string, size: number): PreparedAccidental | null;
@@ -0,0 +1,60 @@
1
+ /** 字形相对数字字号的缩放,线宽则取与数字笔画相当的固定比例 */
2
+ const GLYPH_SCALE = 0.72;
3
+ const STROKE_RATIO = 0.06;
4
+ const ACCIDENTALS = {
5
+ "#": {
6
+ w: 0.46,
7
+ h: 0.78,
8
+ baseline: 0.63,
9
+ commands: [
10
+ { op: "M", x: 0.34, y: 0.05 },
11
+ { op: "L", x: 0.27, y: 0.95 },
12
+ { op: "M", x: 0.72, y: 0.05 },
13
+ { op: "L", x: 0.65, y: 0.95 },
14
+ { op: "M", x: 0.08, y: 0.38 },
15
+ { op: "L", x: 0.9, y: 0.28 },
16
+ { op: "M", x: 0.05, y: 0.72 },
17
+ { op: "L", x: 0.87, y: 0.62 },
18
+ ],
19
+ },
20
+ b: {
21
+ w: 0.38,
22
+ h: 0.82,
23
+ baseline: 0.67,
24
+ commands: [
25
+ { op: "M", x: 0.3, y: 0.04 },
26
+ { op: "L", x: 0.3, y: 0.94 },
27
+ { op: "M", x: 0.3, y: 0.5 },
28
+ { op: "Q", cx: 0.92, cy: 0.36, x: 0.76, y: 0.68 },
29
+ { op: "Q", cx: 0.63, cy: 0.87, x: 0.3, y: 0.86 },
30
+ ],
31
+ },
32
+ n: {
33
+ w: 0.42,
34
+ h: 0.82,
35
+ baseline: 0.67,
36
+ commands: [
37
+ { op: "M", x: 0.28, y: 0.05 },
38
+ { op: "L", x: 0.28, y: 0.82 },
39
+ { op: "L", x: 0.72, y: 0.68 },
40
+ { op: "L", x: 0.72, y: 0.95 },
41
+ { op: "M", x: 0.72, y: 0.95 },
42
+ { op: "L", x: 0.72, y: 0.18 },
43
+ { op: "L", x: 0.28, y: 0.32 },
44
+ ],
45
+ },
46
+ };
47
+ /** size 是数字字号,升降号自己决定缩小多少 */
48
+ export function prepareAccidental(symbol, size) {
49
+ const definition = ACCIDENTALS[symbol];
50
+ if (!definition)
51
+ return null;
52
+ const glyphSize = size * GLYPH_SCALE;
53
+ return {
54
+ w: definition.w * glyphSize,
55
+ h: definition.h * glyphSize,
56
+ baseline: definition.baseline * glyphSize,
57
+ strokeWidth: size * STROKE_RATIO,
58
+ commands: definition.commands,
59
+ };
60
+ }
@@ -0,0 +1,2 @@
1
+ import { ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const NoteNode: ASTFunctionClass;