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.
Files changed (112) hide show
  1. package/README.md +69 -11
  2. package/dist/diagnostic.js +5 -2
  3. package/dist/fraction.d.ts +24 -0
  4. package/dist/fraction.js +132 -0
  5. package/dist/functions/ASTtypes.d.ts +24 -7
  6. package/dist/functions/ASTtypes.js +35 -16
  7. package/dist/functions/adjust/index.d.ts +2 -0
  8. package/dist/functions/adjust/index.js +193 -0
  9. package/dist/functions/bar/index.d.ts +8 -0
  10. package/dist/functions/bar/index.js +39 -10
  11. package/dist/functions/beam/auto.js +32 -15
  12. package/dist/functions/beam/index.js +10 -6
  13. package/dist/functions/beam/layout.d.ts +3 -2
  14. package/dist/functions/beam/layout.js +42 -64
  15. package/dist/functions/box/index.js +35 -34
  16. package/dist/functions/br/index.js +9 -7
  17. package/dist/functions/dash/index.js +33 -1
  18. package/dist/functions/default.js +14 -7
  19. package/dist/functions/div/index.d.ts +16 -0
  20. package/dist/functions/div/index.js +45 -17
  21. package/dist/functions/dot/index.js +11 -3
  22. package/dist/functions/grace/index.d.ts +8 -11
  23. package/dist/functions/grace/index.js +82 -76
  24. package/dist/functions/head/index.d.ts +2 -0
  25. package/dist/functions/head/index.js +492 -0
  26. package/dist/functions/key/index.js +97 -3
  27. package/dist/functions/meter/index.d.ts +2 -0
  28. package/dist/functions/meter/index.js +154 -0
  29. package/dist/functions/note/accidentals.d.ts +24 -3
  30. package/dist/functions/note/accidentals.js +31 -1
  31. package/dist/functions/note/index.js +45 -65
  32. package/dist/functions/page/index.d.ts +9 -2
  33. package/dist/functions/page/index.js +50 -4
  34. package/dist/functions/set/index.js +37 -29
  35. package/dist/functions/stack/index.js +26 -22
  36. package/dist/functions/symbol/index.d.ts +21 -0
  37. package/dist/functions/symbol/index.js +94 -0
  38. package/dist/functions/symbol/ornament.d.ts +15 -0
  39. package/dist/functions/symbol/ornament.js +73 -0
  40. package/dist/functions/symbol/shape.d.ts +22 -0
  41. package/dist/functions/symbol/shape.js +48 -0
  42. package/dist/functions/symbol/symbols/accent.d.ts +2 -0
  43. package/dist/functions/symbol/symbols/accent.js +24 -0
  44. package/dist/functions/symbol/symbols/f.d.ts +2 -0
  45. package/dist/functions/symbol/symbols/f.js +55 -0
  46. package/dist/functions/symbol/symbols/fermata.d.ts +2 -0
  47. package/dist/functions/symbol/symbols/fermata.js +24 -0
  48. package/dist/functions/symbol/symbols/mordent.d.ts +2 -0
  49. package/dist/functions/symbol/symbols/mordent.js +43 -0
  50. package/dist/functions/symbol/symbols/p.d.ts +2 -0
  51. package/dist/functions/symbol/symbols/p.js +61 -0
  52. package/dist/functions/symbol/symbols/tr.d.ts +2 -0
  53. package/dist/functions/symbol/symbols/tr.js +75 -0
  54. package/dist/functions/tempo/glyph.d.ts +12 -0
  55. package/dist/functions/tempo/glyph.js +30 -0
  56. package/dist/functions/tempo/index.js +53 -2
  57. package/dist/functions/text/index.js +76 -24
  58. package/dist/functions/tie/index.js +102 -127
  59. package/dist/functions/tuplet/index.d.ts +2 -0
  60. package/dist/functions/tuplet/index.js +211 -0
  61. package/dist/functions/up/index.d.ts +1 -0
  62. package/dist/functions/up/index.js +245 -171
  63. package/dist/functions/voice/index.js +86 -68
  64. package/dist/functions/volta/index.d.ts +2 -0
  65. package/dist/functions/volta/index.js +227 -0
  66. package/dist/index.d.ts +4 -0
  67. package/dist/index.js +4 -0
  68. package/dist/jpfun.min.js +143 -0
  69. package/dist/layout/engine.d.ts +4 -11
  70. package/dist/layout/engine.js +65 -59
  71. package/dist/layout/model.d.ts +8 -1
  72. package/dist/layout/model.js +16 -6
  73. package/dist/layout/path.d.ts +19 -0
  74. package/dist/layout/path.js +101 -0
  75. package/dist/layout/types.d.ts +51 -18
  76. package/dist/layout/types.js +3 -1
  77. package/dist/lowering/loweringContext.d.ts +18 -20
  78. package/dist/lowering/loweringContext.js +132 -94
  79. package/dist/lowering/track.d.ts +1 -1
  80. package/dist/lowering/track.js +1 -1
  81. package/dist/lowering/types.d.ts +49 -12
  82. package/dist/lowering/types.js +9 -3
  83. package/dist/parser/parse-utils/call-utils.d.ts +0 -2
  84. package/dist/parser/parse-utils/call-utils.js +0 -6
  85. package/dist/parser/parse-utils/note-utils.d.ts +8 -0
  86. package/dist/parser/parse-utils/note-utils.js +35 -13
  87. package/dist/parser/parse-utils/string-utils.d.ts +8 -0
  88. package/dist/parser/parse-utils/string-utils.js +20 -0
  89. package/dist/parser/parserContext.d.ts +8 -5
  90. package/dist/parser/parserContext.js +38 -33
  91. package/dist/parser/preprocess.js +17 -11
  92. package/dist/playback/compile.d.ts +18 -0
  93. package/dist/playback/compile.js +401 -0
  94. package/dist/playback/event.d.ts +91 -0
  95. package/dist/playback/event.js +87 -0
  96. package/dist/playback/time.d.ts +14 -0
  97. package/dist/playback/time.js +81 -0
  98. package/dist/playback/types.d.ts +128 -0
  99. package/dist/playback/types.js +3 -0
  100. package/dist/render/canvas.d.ts +4 -4
  101. package/dist/render/canvas.js +20 -5
  102. package/dist/render/index.d.ts +1 -0
  103. package/dist/render/index.js +1 -0
  104. package/dist/render/paint.d.ts +8 -0
  105. package/dist/render/paint.js +101 -0
  106. package/dist/render/svg.d.ts +3 -3
  107. package/dist/render/svg.js +10 -8
  108. package/dist/render/text.d.ts +17 -0
  109. package/dist/render/text.js +35 -0
  110. package/dist/render/types.d.ts +1 -1
  111. package/grammar.md +93 -10
  112. package/package.json +10 -6
@@ -1,8 +1,10 @@
1
1
  import { ErrorDiagnostic } from "../../diagnostic.js";
2
2
  import { prepareLayoutHost } from "../../layout/engine.js";
3
+ import { Fraction } from "../../fraction.js";
3
4
  import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
4
5
  import { ASTBraceNode, ASTFunctionNode, ASTLabelNode, ASTTextNode, } from "../ASTtypes.js";
5
- import { claimDivLine, DIV_ADDON_KEY, divLinePortName } from "../div/index.js";
6
+ import { createBeamLayoutAttachment } from "../beam/layout.js";
7
+ import { DIV_ADDON_KEY } from "../div/index.js";
6
8
  /** 倚音相对宿主的字号比例 */
7
9
  const GRACE_SCALE = 0.7;
8
10
  /**
@@ -55,7 +57,7 @@ class GraceFunction extends ASTFunctionNode {
55
57
  }
56
58
  ],
57
59
  };
58
- labelable() { return this; }
60
+ labelable() { return ASTFunctionNode.findLabelable(this.host); }
59
61
  static deSugarAtom(source, start, _end) {
60
62
  let char = source[start];
61
63
  switch (char) {
@@ -97,7 +99,9 @@ class GraceFunction extends ASTFunctionNode {
97
99
  let replaceFrom = left;
98
100
  // 对 label 的特判: 目标变为label到被标记的节点范围内的所有节点
99
101
  if (leftNode instanceof ASTLabelNode) {
100
- const tgt = leftNode.parent;
102
+ let tgt = leftNode.target;
103
+ while (tgt && !ctx.nodes.includes(tgt))
104
+ tgt = tgt.parent;
101
105
  for (let j = left - 1; j >= 0; j--) {
102
106
  if (ctx.nodes[j] === tgt) {
103
107
  leftNode = new ASTBraceNode({
@@ -181,6 +185,7 @@ class GraceFunction extends ASTFunctionNode {
181
185
  if (this.side !== "pre" && this.side !== "post") {
182
186
  throw new ErrorDiagnostic("E_GRACE_INVALID_SIDE", "@grace 的 side 参数必须是 'pre' 或 'post'", span);
183
187
  }
188
+ this.adopt(this.host);
184
189
  this.setGrace(this.grace, scale);
185
190
  this.size = ctx.fontSize;
186
191
  }
@@ -207,7 +212,7 @@ class GraceFunction extends ASTFunctionNode {
207
212
  ? [this.grace, this.host]
208
213
  : [this.host, this.grace];
209
214
  for (const content of order) {
210
- const events = ctx.trackedEvents(content, 0, track).columns.flat();
215
+ const events = ctx.trackedEvents(content, new Fraction(), track).flat();
211
216
  const visible = events.filter(isVisualTemporalNode);
212
217
  // 并行分支的纵向关系由引擎解 Track 树,而折叠成员进不了引擎
213
218
  if (events.some(event => event.track !== track))
@@ -226,7 +231,15 @@ class GraceFunction extends ASTFunctionNode {
226
231
  }
227
232
  }
228
233
  });
229
- return [new GraceTemporal(this, host, graces, this.side)];
234
+ const composite = new GraceTemporal(this, host, graces, this.side);
235
+ // 倚音同段无条件全部相连,只被零时长标记切断,所以 grace 一定认识 beam,允许耦合
236
+ // 折叠成员不进全局时间列,autobeam 看不到它们,要手动调用且强制连接
237
+ for (const run of composite.graceRuns) {
238
+ if (run.length < 2)
239
+ continue;
240
+ ctx.addAttachment(createBeamLayoutAttachment([...run], false, this.grace.sourceSpan));
241
+ }
242
+ return [composite];
230
243
  }
231
244
  toString(source) {
232
245
  const host = this.host?.toString(source) ?? "";
@@ -245,29 +258,27 @@ const HOOK_COMMANDS = [
245
258
  const HOOK_WIDTH = 0.07;
246
259
  /** 倚音块底边到宿主顶边的视觉间隙;倚音线会探进这段空间指向宿主 */
247
260
  const GRACE_RISE = 0.18;
248
- /** 倚音向宿主借时值的上限,防止宿主被偷光 */
249
- const MAX_STEAL_RATIO = 0.75;
250
- /** 减时线粗细占字号的比例 */
251
- const DIV_LINE_WIDTH = 0.055;
261
+ /** 倪音向宿主借时值的上限,防止宿主被偷光;只读 */
262
+ const MAX_STEAL_RATIO = new Fraction(3, 4);
252
263
  /** 倚音复合体:宿主与倚音都折叠在这一个盒子里,对外只是一个可见事件 */
253
264
  export class GraceTemporal extends TemporalNodeBase {
254
265
  host;
255
266
  graces;
267
+ /** 承担节奏的倚音成员,按零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
268
+ graceRuns;
256
269
  side;
257
270
  /** 成员相对本盒左上角的局部偏移,onPlaced 时同步为绝对坐标 */
258
271
  hostOffset = { x: 0, y: 0 };
259
272
  graceOffsets = [];
260
273
  /** 倚音线起点,同样是局部偏移 */
261
274
  hookOrigin = null;
262
- divLines = [];
263
275
  constructor(ast, host, graces, side) {
264
276
  super();
265
277
  this.ast = ast;
266
278
  this.host = host;
267
279
  this.graces = graces;
268
280
  this.side = side;
269
- this.t = 0;
270
- this.T = host.T;
281
+ this.T.copyFrom(host.T);
271
282
  this.mergeKey = host.mergeKey;
272
283
  this.initLayoutBox();
273
284
  // 宿主携带整体节奏,它的修饰暂时成为复合体的修饰,自动连梁才看得到
@@ -276,20 +287,39 @@ export class GraceTemporal extends TemporalNodeBase {
276
287
  host.addon = void 0;
277
288
  // 成员不进入全局 columns,对外由复合体代表:写在成员上的标签仍可做关系端点
278
289
  host.foldedInto = this;
290
+ const runs = [];
291
+ let run = [];
279
292
  for (const grace of graces) {
280
293
  grace.foldedInto = this;
294
+ // 调号、速度这类零时长标记不承担节奏,还会在视觉上把倚音串切断
295
+ if (grace.T.isZero()) {
296
+ if (run.length > 0)
297
+ runs.push(run);
298
+ run = [];
299
+ continue;
300
+ }
301
+ run.push(grace);
281
302
  // 倚音默认就是八分音符:补一条减时线,书面时值随之减半
282
303
  const addon = grace.addon = { ...grace.addon };
283
304
  addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + 1;
284
- grace.T /= 2;
305
+ grace.T.divPow2();
285
306
  }
307
+ if (run.length > 0)
308
+ runs.push(run);
309
+ this.graceRuns = runs;
286
310
  }
311
+ /** 成员共享全局时间状态,按发声顺序固化 */
287
312
  onTimeState(state) {
288
- for (const member of [this.host, ...this.graces]) {
289
- member.t = this.t;
313
+ // 同步时间的修改。比如tuplet修改的是GraceTemporal.T,这里要把它传给成员
314
+ this.host.T.copyFrom(this.T);
315
+ const members = this.side === "pre"
316
+ ? [...this.graces, this.host]
317
+ : [this.host, ...this.graces];
318
+ for (const member of members) {
319
+ member.t.copyFrom(this.t);
290
320
  member.track = this.track;
291
321
  member.layoutLine = this.layoutLine;
292
- member.onTimeState?.({ ...state });
322
+ member.onTimeState?.(state);
293
323
  }
294
324
  }
295
325
  /**
@@ -298,9 +328,35 @@ export class GraceTemporal extends TemporalNodeBase {
298
328
  * 比例 = 倚音字面总时值 / 四分音符,而四分音符 T=1,所以直接累加成员的 T。
299
329
  * 基准取宿主经过 div 与 dot 后的实际时值;延时线是独立事件、tie 不改 T,天然不计入。
300
330
  */
301
- get stealTime() {
302
- const written = this.graces.reduce((sum, grace) => sum + grace.T, 0);
303
- return Math.min(written, MAX_STEAL_RATIO) * this.T;
331
+ emitPlayback(emitter) {
332
+ const written = new Fraction();
333
+ for (const grace of this.graces)
334
+ written.add(grace.T);
335
+ const ratio = written.compare(MAX_STEAL_RATIO) > 0 ? MAX_STEAL_RATIO : written;
336
+ const total = emitter.end.clone().sub(emitter.start);
337
+ const steal = total.clone().mul(ratio);
338
+ const hostDuration = total.sub(steal);
339
+ const start = emitter.start.clone();
340
+ const playGraces = (cursor) => {
341
+ for (const grace of this.graces) {
342
+ if (grace.T.isZero() || written.isZero()) {
343
+ emitter.play(grace, cursor, new Fraction());
344
+ continue;
345
+ }
346
+ const duration = steal.clone().mul(grace.T).div(written);
347
+ emitter.play(grace, cursor, duration);
348
+ cursor.add(duration);
349
+ }
350
+ };
351
+ if (this.side === "pre") {
352
+ const cursor = start.clone();
353
+ playGraces(cursor);
354
+ emitter.play(this.host, cursor, hostDuration);
355
+ }
356
+ else {
357
+ emitter.play(this.host, start, hostDuration);
358
+ playGraces(start.clone().add(hostDuration));
359
+ }
304
360
  }
305
361
  /**
306
362
  * 宿主留在轨道基线上,倚音整体抬到它的左上或右上角
@@ -366,60 +422,15 @@ export class GraceTemporal extends TemporalNodeBase {
366
422
  }
367
423
  // 宿主没有肩线时它自己的盒顶就是肩线;有则上面那轮转发已经带偏移抬好了
368
424
  this.ports[SHOULDER_PORT] ??= { x: this.box.anchor, y: this.hostOffset.y };
369
- this.connectGraceDivLines();
370
- // 钩形曲线挂在靠近宿主的那个倚音成员下方
371
- const near = this.side === "pre" ? this.graces.length - 1 : 0;
372
- const anchorGrace = this.graces[near];
373
- this.hookOrigin = anchorGrace ? {
374
- x: this.graceOffsets[near].x + anchorGrace.box.anchor,
425
+ // 钩形曲线挂在靠近宿主的那个倚音成员下方,零时长标记不承接它
426
+ const near = this.side === "pre"
427
+ ? this.graceRuns.at(-1)?.at(-1)
428
+ : this.graceRuns[0]?.[0];
429
+ this.hookOrigin = near ? {
430
+ x: this.graceOffsets[this.graces.indexOf(near)].x + near.box.anchor,
375
431
  y: graceTop + lift + graceHeight,
376
432
  } : null;
377
433
  }
378
- /**
379
- * 把相邻倚音成员的同级减时线连成整条
380
- *
381
- * 折叠成员不进入全局时间列,自动连梁看不到它们,只能由复合体自己连。
382
- */
383
- connectGraceDivLines() {
384
- this.divLines.length = 0;
385
- if (this.graces.length < 2)
386
- return;
387
- for (let level = 0;; level++) {
388
- let hasPorts = false;
389
- let run = [];
390
- const flush = () => {
391
- if (run.length >= 2) {
392
- const first = run[0];
393
- const last = run[run.length - 1];
394
- const leftPort = this.graces[first].ports[divLinePortName(level, "left")];
395
- const rightPort = this.graces[last].ports[divLinePortName(level, "right")];
396
- this.divLines.push({
397
- x1: this.graceOffsets[first].x + leftPort.x,
398
- x2: this.graceOffsets[last].x + rightPort.x,
399
- y: this.graceOffsets[first].y + leftPort.y,
400
- });
401
- for (const index of run)
402
- claimDivLine(this.graces[index], level);
403
- }
404
- run = [];
405
- };
406
- for (let i = 0; i < this.graces.length; i++) {
407
- const ports = this.graces[i].ports;
408
- const left = ports[divLinePortName(level, "left")];
409
- const right = ports[divLinePortName(level, "right")];
410
- if (left || right)
411
- hasPorts = true;
412
- if (!left || !right) {
413
- flush();
414
- continue;
415
- }
416
- run.push(i);
417
- }
418
- flush();
419
- if (!hasPorts)
420
- break;
421
- }
422
- }
423
434
  /** 引擎每次改变本节点坐标后,按准备阶段保存的局部偏移重算成员绝对坐标 */
424
435
  onPlaced() {
425
436
  this.host.box.x = this.box.x + this.hostOffset.x;
@@ -437,14 +448,9 @@ export class GraceTemporal extends TemporalNodeBase {
437
448
  for (const decoration of member.decorations)
438
449
  decoration.paint(painter);
439
450
  }
440
- const graceEm = this.ast.size * GRACE_SCALE;
441
- const divWidth = Math.max(0.8, graceEm * DIV_LINE_WIDTH);
442
- for (const line of this.divLines) {
443
- const y = this.box.y + line.y;
444
- painter.drawLine(this.box.x + line.x1, y, this.box.x + line.x2, y, { stroke: "#000", strokeWidth: divWidth });
445
- }
446
451
  if (!this.hookOrigin)
447
452
  return;
453
+ const graceEm = this.ast.size * GRACE_SCALE;
448
454
  const hookOrigin = this.hookOrigin;
449
455
  const dir = this.side === "pre" ? 1 : -1;
450
456
  painter.drawPath(HOOK_COMMANDS, { stroke: "#000", strokeWidth: Math.max(0.8, graceEm * HOOK_WIDTH) }, {
@@ -0,0 +1,2 @@
1
+ import { type ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const HeadNode: ASTFunctionClass;