jpfun 0.6.0 → 0.8.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 (134) hide show
  1. package/Bravura-LICENSE.txt +93 -0
  2. package/README.md +49 -146
  3. package/dist/Bravura-LICENSE.txt +93 -0
  4. package/dist/converter/midi/convert.d.ts +3 -11
  5. package/dist/converter/midi/convert.js +89 -67
  6. package/dist/converter/musicxml/features.d.ts +34 -12
  7. package/dist/converter/musicxml/features.js +131 -24
  8. package/dist/converter/musicxml/index.js +221 -54
  9. package/dist/converter/musicxml/model.d.ts +24 -0
  10. package/dist/core.d.ts +1 -0
  11. package/dist/core.js +1 -0
  12. package/dist/diagnostic.d.ts +7 -1
  13. package/dist/diagnostic.js +40 -88
  14. package/dist/fraction.js +3 -2
  15. package/dist/functions/ASTtypes.d.ts +2 -18
  16. package/dist/functions/ASTtypes.js +0 -16
  17. package/dist/functions/arpeggio/index.js +17 -22
  18. package/dist/functions/arpeggio/shape.d.ts +1 -1
  19. package/dist/functions/arpeggio/shape.js +27 -21
  20. package/dist/functions/bar/index.d.ts +8 -0
  21. package/dist/functions/bar/index.js +139 -36
  22. package/dist/functions/beam/auto.d.ts +1 -2
  23. package/dist/functions/beam/auto.js +44 -27
  24. package/dist/functions/beam/index.js +2 -3
  25. package/dist/functions/beam/layout.d.ts +1 -1
  26. package/dist/functions/beam/layout.js +16 -24
  27. package/dist/functions/br/index.d.ts +34 -2
  28. package/dist/functions/br/index.js +8 -8
  29. package/dist/functions/dash/index.js +6 -6
  30. package/dist/functions/default.js +10 -5
  31. package/dist/functions/div/index.d.ts +1 -0
  32. package/dist/functions/div/index.js +8 -4
  33. package/dist/functions/dot/index.js +2 -2
  34. package/dist/functions/dyn/index.js +49 -31
  35. package/dist/functions/gliss/index.d.ts +2 -0
  36. package/dist/functions/gliss/index.js +202 -0
  37. package/dist/functions/grace/index.d.ts +1 -7
  38. package/dist/functions/grace/index.js +11 -19
  39. package/dist/functions/head/index.js +41 -60
  40. package/dist/functions/key/index.js +8 -10
  41. package/dist/functions/meter/index.d.ts +56 -1
  42. package/dist/functions/meter/index.js +30 -24
  43. package/dist/functions/note/accidentals.js +10 -16
  44. package/dist/functions/note/index.js +18 -8
  45. package/dist/functions/note/noteName.d.ts +16 -0
  46. package/dist/functions/note/noteName.js +88 -0
  47. package/dist/functions/note/source-transform.js +6 -5
  48. package/dist/functions/ottava/index.d.ts +2 -0
  49. package/dist/functions/ottava/index.js +233 -0
  50. package/dist/functions/page/index.d.ts +1 -0
  51. package/dist/functions/page/index.js +22 -13
  52. package/dist/functions/pagebreak/index.d.ts +2 -0
  53. package/dist/functions/pagebreak/index.js +36 -0
  54. package/dist/functions/rest/index.js +15 -15
  55. package/dist/functions/set/index.js +4 -4
  56. package/dist/functions/slide/index.d.ts +2 -0
  57. package/dist/functions/slide/index.js +132 -0
  58. package/dist/functions/stack/index.js +8 -15
  59. package/dist/functions/symbol/index.d.ts +30 -2
  60. package/dist/functions/symbol/index.js +93 -19
  61. package/dist/functions/symbol/ornament.d.ts +1 -2
  62. package/dist/functions/symbol/ornament.js +14 -19
  63. package/dist/functions/symbol/shape.d.ts +6 -0
  64. package/dist/functions/symbol/shape.js +20 -4
  65. package/dist/functions/symbol/symbols/coda.d.ts +4 -0
  66. package/dist/functions/symbol/symbols/coda.js +56 -0
  67. package/dist/functions/symbol/symbols/dynamics.d.ts +1 -1
  68. package/dist/functions/symbol/symbols/dynamics.js +18 -8
  69. package/dist/functions/symbol/symbols/fine.js +1 -1
  70. package/dist/functions/symbol/symbols/harmonic.d.ts +2 -0
  71. package/dist/functions/symbol/symbols/harmonic.js +11 -0
  72. package/dist/functions/symbol/symbols/marcato.d.ts +2 -0
  73. package/dist/functions/symbol/symbols/marcato.js +22 -0
  74. package/dist/functions/symbol/symbols/staccato.d.ts +2 -0
  75. package/dist/functions/symbol/symbols/staccato.js +24 -0
  76. package/dist/functions/symbol/symbols/tenuto.d.ts +2 -0
  77. package/dist/functions/symbol/symbols/tenuto.js +11 -0
  78. package/dist/functions/symbol/symbols/tocoda.d.ts +2 -0
  79. package/dist/functions/symbol/symbols/tocoda.js +16 -0
  80. package/dist/functions/symbol/symbols/tr.js +46 -1
  81. package/dist/functions/symbol/symbols/turn.d.ts +3 -0
  82. package/dist/functions/symbol/symbols/turn.js +50 -0
  83. package/dist/functions/symbol/symbols/wheel.d.ts +2 -0
  84. package/dist/functions/symbol/symbols/wheel.js +36 -0
  85. package/dist/functions/temporal.d.ts +17 -6
  86. package/dist/functions/temporal.js +17 -6
  87. package/dist/functions/text/index.js +15 -6
  88. package/dist/functions/tie/index.js +64 -30
  89. package/dist/functions/tremolo/index.d.ts +2 -0
  90. package/dist/functions/tremolo/index.js +319 -0
  91. package/dist/functions/tuplet/index.js +4 -8
  92. package/dist/functions/up/index.js +7 -9
  93. package/dist/functions/voice/connections.js +7 -5
  94. package/dist/functions/voice/index.js +19 -11
  95. package/dist/functions/volta/index.js +41 -21
  96. package/dist/jpfun.core.min.js +64 -80
  97. package/dist/jpfun.from-midi.min.js +5 -5
  98. package/dist/jpfun.from-musicxml.min.js +8 -8
  99. package/dist/jpfun.min.js +69 -85
  100. package/dist/layout/default.d.ts +0 -1
  101. package/dist/layout/default.js +0 -1
  102. package/dist/layout/engine.d.ts +1 -0
  103. package/dist/layout/engine.js +42 -11
  104. package/dist/layout/page.d.ts +3 -2
  105. package/dist/layout/page.js +6 -5
  106. package/dist/layout/types.d.ts +4 -0
  107. package/dist/lowering/loweringContext.d.ts +11 -13
  108. package/dist/lowering/loweringContext.js +30 -197
  109. package/dist/lowering/types.d.ts +1 -3
  110. package/dist/parser/parse-utils/note-utils.d.ts +1 -1
  111. package/dist/parser/parse-utils/note-utils.js +8 -9
  112. package/dist/parser/parse-utils/string-utils.d.ts +3 -2
  113. package/dist/parser/parse-utils/string-utils.js +16 -3
  114. package/dist/parser/parserContext.d.ts +2 -0
  115. package/dist/parser/parserContext.js +36 -8
  116. package/dist/pipeline.js +0 -1
  117. package/dist/playback/compile.js +131 -72
  118. package/dist/playback/event.d.ts +1 -1
  119. package/dist/playback/event.js +9 -9
  120. package/dist/playback/time.js +42 -31
  121. package/dist/playback/types.d.ts +7 -3
  122. package/dist/render/canvas.d.ts +2 -0
  123. package/dist/render/canvas.js +25 -22
  124. package/dist/render/recording.js +1 -4
  125. package/dist/render/svg.js +1 -0
  126. package/dist/render/text.d.ts +1 -1
  127. package/dist/render/text.js +3 -2
  128. package/dist/render/translate.d.ts +0 -1
  129. package/dist/render/translate.js +6 -23
  130. package/dist/render/types.d.ts +1 -0
  131. package/grammar.md +52 -25
  132. package/package.json +3 -3
  133. package/dist/functions/note/noteNameFSM.d.ts +0 -8
  134. package/dist/functions/note/noteNameFSM.js +0 -181
@@ -1,7 +1,11 @@
1
1
  import { DEFAULT_BPM } from "../functions/temporal.js";
2
- /** 演奏时间(QN)按分段速度积分为秒 */
3
- export function performanceTimeToSeconds(events, performanceTime) {
4
- const target = Math.max(0, performanceTime);
2
+ const tempoSegmentsCache = new WeakMap();
3
+ /** 提取并缓存分段速度积分表,使帧级时间换算无需重复扫描全部音符事件 */
4
+ function tempoSegmentsOf(events) {
5
+ let segments = tempoSegmentsCache.get(events);
6
+ if (segments)
7
+ return segments;
8
+ const built = [{ performance: 0, seconds: 0, bpm: DEFAULT_BPM }];
5
9
  let seconds = 0;
6
10
  let position = 0;
7
11
  let bpm = DEFAULT_BPM;
@@ -9,43 +13,50 @@ export function performanceTimeToSeconds(events, performanceTime) {
9
13
  if (event.kind !== "tempo")
10
14
  continue;
11
15
  const next = event.at.toNumber();
12
- if (next > target)
13
- break;
14
- if (next > position)
16
+ if (next > position) {
15
17
  seconds += (next - position) * 60 / bpm;
16
- position = next;
18
+ position = next;
19
+ }
17
20
  bpm = event.bpm;
21
+ const last = built[built.length - 1];
22
+ if (last.performance === position)
23
+ last.bpm = bpm;
24
+ else
25
+ built.push({ performance: position, seconds, bpm });
18
26
  }
19
- return seconds + (target - position) * 60 / bpm;
27
+ tempoSegmentsCache.set(events, built);
28
+ return built;
20
29
  }
21
- export function secondsToPerformanceTime(events, seconds) {
22
- let remaining = Math.max(0, seconds);
23
- let position = 0;
24
- let bpm = DEFAULT_BPM;
25
- for (const event of events) {
26
- if (event.kind !== "tempo")
27
- continue;
28
- const next = event.at.toNumber();
29
- const segmentSeconds = (next - position) * 60 / bpm;
30
- if (remaining < segmentSeconds)
31
- return position + remaining * bpm / 60;
32
- remaining -= segmentSeconds;
33
- position = next;
34
- bpm = event.bpm;
30
+ function stepAt(items, target, valueOf) {
31
+ let low = 0;
32
+ let high = items.length;
33
+ while (low < high) {
34
+ const middle = (low + high) >>> 1;
35
+ if (valueOf(items[middle]) <= target)
36
+ low = middle + 1;
37
+ else
38
+ high = middle;
35
39
  }
36
- return position + remaining * bpm / 60;
40
+ return items[Math.max(0, low - 1)];
41
+ }
42
+ /** 演奏时间(QN)按分段速度积分为秒 */
43
+ export function performanceTimeToSeconds(events, performanceTime) {
44
+ const target = Math.max(0, performanceTime);
45
+ const segment = stepAt(tempoSegmentsOf(events), target, item => item.performance);
46
+ return segment.seconds + (target - segment.performance) * 60 / segment.bpm;
47
+ }
48
+ export function secondsToPerformanceTime(events, seconds) {
49
+ const target = Math.max(0, seconds);
50
+ const segment = stepAt(tempoSegmentsOf(events), target, item => item.seconds);
51
+ return segment.performance + (target - segment.seconds) * segment.bpm / 60;
37
52
  }
38
53
  /** 演奏进度换算为谱面进度 */
39
54
  export function performanceTimeToScoreTime(scoreMap, performanceTime) {
40
55
  const target = Math.max(0, performanceTime);
41
- let result = target;
42
- for (const point of scoreMap) {
43
- const performance = point.performance.toNumber();
44
- if (performance > target)
45
- break;
46
- result = point.score.toNumber() + target - performance;
47
- }
48
- return result;
56
+ const point = stepAt(scoreMap, target, item => item.performance.toNumber());
57
+ return point && point.performance.toNumber() <= target
58
+ ? point.score.toNumber() + target - point.performance.toNumber()
59
+ : target;
49
60
  }
50
61
  /**
51
62
  * 谱面位置换算为演奏时间,供“点击谱面从这里播放”使用
@@ -30,8 +30,8 @@ export type PlaybackFlowAction = {
30
30
  } | {
31
31
  kind: "stop";
32
32
  };
33
- /** 把事件映射到它在播放时间流中的列;折叠成员会映射到宿主 */
34
- export type PlaybackColumnOf = (node: TemporalNodeBase) => number | undefined;
33
+ /** 默认查询起点列;end 查询宿主时值内的最后一列,不包含结束时刻的列。折叠成员映射到宿主 */
34
+ export type PlaybackColumnOf = (node: TemporalNodeBase, edge?: "start" | "end") => number | undefined;
35
35
  /** 控制流在编译开始时生成的执行声明;range 是运行的升序闭区间,缺省表示每列都运行 */
36
36
  export interface PlaybackFlowHook {
37
37
  readonly range?: readonly [number, number];
@@ -51,6 +51,8 @@ export interface PlaybackState {
51
51
  readonly bpm?: number;
52
52
  readonly velocity?: number;
53
53
  readonly program?: number;
54
+ /** 只改变发声的半音偏移,不改变记谱音高及其源码改写 */
55
+ readonly pitchShift?: number;
54
56
  }
55
57
  /** 按线性化时间扫描的可变系统状态 */
56
58
  export interface PlaybackSystemState {
@@ -90,7 +92,7 @@ export interface PlaybackNote extends PlaybackNoteInput, PlaybackSpan {
90
92
  }
91
93
  /** 当前节点发布完成后,修改此前音段的结构;此时尚未生成系统状态 */
92
94
  export type PlaybackHook = (context: PlaybackHookContext) => void;
93
- /** 在最终状态下展开一个音段;结果须保持原音段的起止边界 */
95
+ /** 在最终状态下展开一个音段;结果须位于原音段区间内并保持原起点 */
94
96
  export type PlaybackTransform = (context: PlaybackTransformContext, notes: PlaybackNote[]) => PlaybackNote[] | void;
95
97
  /** 结构阶段的已发布前缀;集合只读,其中区间的边界仍可调整 */
96
98
  export interface PlaybackHookContext {
@@ -99,6 +101,8 @@ export interface PlaybackHookContext {
99
101
  /** 按发布顺序排列,仅包含当前位置此前已发布的有声音段 */
100
102
  readonly notes: readonly PlaybackNote[];
101
103
  readonly diagnostics: Diagnostic[];
104
+ /** 为当前计划内的已发布音段追加声音展开变换 */
105
+ transform(note: PlaybackNote, transform: PlaybackTransform): void;
102
106
  }
103
107
  /** 声音阶段只查询最终状态,接口不提供全局区间的写入口 */
104
108
  export interface PlaybackTransformContext {
@@ -9,6 +9,8 @@ import type { PaintStyle, Painter, PathCommand, PathTransform, TextStyle } from
9
9
  export declare class CanvasPainter implements Painter {
10
10
  private readonly context;
11
11
  constructor(context: CanvasRenderingContext2D);
12
+ private beginOpacity;
13
+ private endOpacity;
12
14
  drawText(text: string, x: number, y: number, style: TextStyle): void;
13
15
  drawLine(x1: number, y1: number, x2: number, y2: number, style?: PaintStyle): void;
14
16
  drawRect(x: number, y: number, w: number, h: number, style?: PaintStyle): void;
@@ -24,11 +24,6 @@ function tracePath(context, commands, x = 0, y = 0, scaleX = 1, scaleY = 1) {
24
24
  context.bezierCurveTo(x + command.cx1 * scaleX, y + command.cy1 * scaleY, x + command.cx2 * scaleX, y + command.cy2 * scaleY, x + command.x * scaleX, y + command.y * scaleY);
25
25
  }
26
26
  }
27
- function applyOpacity(context, style) {
28
- if (style?.opacity === void 0)
29
- return;
30
- context.globalAlpha *= style.opacity;
31
- }
32
27
  /**
33
28
  * CanvasRenderingContext2D 的 Painter 适配器
34
29
  *
@@ -40,19 +35,28 @@ export class CanvasPainter {
40
35
  constructor(context) {
41
36
  this.context = context;
42
37
  }
38
+ beginOpacity(style) {
39
+ if (style?.opacity === void 0)
40
+ return void 0;
41
+ const previous = this.context.globalAlpha;
42
+ this.context.globalAlpha = previous * style.opacity;
43
+ return previous;
44
+ }
45
+ endOpacity(previous) {
46
+ if (previous !== void 0)
47
+ this.context.globalAlpha = previous;
48
+ }
43
49
  drawText(text, x, y, style) {
44
- this.context.save();
45
- applyOpacity(this.context, style);
50
+ const previousAlpha = this.beginOpacity(style);
46
51
  this.context.fillStyle = style.fill ?? "#000";
47
52
  this.context.font = canvasFont(style);
48
53
  this.context.textAlign = style.textAlign ?? "left";
49
54
  this.context.textBaseline = "alphabetic";
50
55
  this.context.fillText(text, x, y);
51
- this.context.restore();
56
+ this.endOpacity(previousAlpha);
52
57
  }
53
58
  drawLine(x1, y1, x2, y2, style) {
54
- this.context.save();
55
- applyOpacity(this.context, style);
59
+ const previousAlpha = this.beginOpacity(style);
56
60
  this.context.beginPath();
57
61
  this.context.moveTo(x1, y1);
58
62
  this.context.lineTo(x2, y2);
@@ -60,13 +64,12 @@ export class CanvasPainter {
60
64
  this.context.lineWidth = style?.strokeWidth ?? 1;
61
65
  this.context.lineCap = "round";
62
66
  this.context.stroke();
63
- this.context.restore();
67
+ this.endOpacity(previousAlpha);
64
68
  }
65
69
  drawRect(x, y, w, h, style) {
66
70
  if (w < 0 || h < 0)
67
71
  return;
68
- this.context.save();
69
- applyOpacity(this.context, style);
72
+ const previousAlpha = this.beginOpacity(style);
70
73
  if (style?.fill) {
71
74
  this.context.fillStyle = style.fill;
72
75
  this.context.fillRect(x, y, w, h);
@@ -74,19 +77,20 @@ export class CanvasPainter {
74
77
  if (style?.stroke) {
75
78
  this.context.strokeStyle = style.stroke;
76
79
  this.context.lineWidth = style.strokeWidth ?? 1;
80
+ this.context.lineJoin = "miter";
77
81
  this.context.strokeRect(x, y, w, h);
78
82
  }
79
- this.context.restore();
83
+ this.endOpacity(previousAlpha);
80
84
  }
81
85
  drawCircle(cx, cy, r, style) {
82
86
  if (r < 0)
83
87
  return;
84
- this.context.save();
85
- applyOpacity(this.context, style);
88
+ const previousAlpha = this.beginOpacity(style);
86
89
  this.context.beginPath();
87
90
  this.context.arc(cx, cy, r, 0, Math.PI * 2);
88
- if (style?.fill ?? true) {
89
- this.context.fillStyle = style?.fill ?? "#000";
91
+ const fill = style?.fill ?? "#000";
92
+ if (fill && fill !== "none") {
93
+ this.context.fillStyle = fill;
90
94
  this.context.fill();
91
95
  }
92
96
  if (style?.stroke) {
@@ -94,13 +98,12 @@ export class CanvasPainter {
94
98
  this.context.lineWidth = style.strokeWidth ?? 1;
95
99
  this.context.stroke();
96
100
  }
97
- this.context.restore();
101
+ this.endOpacity(previousAlpha);
98
102
  }
99
103
  drawPath(commands, style, transform) {
100
104
  if (commands.length === 0)
101
105
  return;
102
- this.context.save();
103
- applyOpacity(this.context, style);
106
+ const previousAlpha = this.beginOpacity(style);
104
107
  tracePath(this.context, commands, transform?.x, transform?.y, transform?.scaleX, transform?.scaleY);
105
108
  if (style?.fill) {
106
109
  this.context.fillStyle = style.fill;
@@ -113,7 +116,7 @@ export class CanvasPainter {
113
116
  this.context.lineJoin = "round";
114
117
  this.context.stroke();
115
118
  }
116
- this.context.restore();
119
+ this.endOpacity(previousAlpha);
117
120
  }
118
121
  }
119
122
  /** 使用 Canvas 后端把一个布局结果一次绘制到独立页面 context */
@@ -70,9 +70,6 @@ function transformPathCommand(command, transform) {
70
70
  return { ...command };
71
71
  const x = (value) => transform.x + value * transform.scaleX;
72
72
  const y = (value) => transform.y + value * transform.scaleY;
73
- if (command.op === "M" || command.op === "L") {
74
- return { op: command.op, x: x(command.x), y: y(command.y) };
75
- }
76
73
  if (command.op === "Q") {
77
74
  return {
78
75
  op: "Q",
@@ -93,5 +90,5 @@ function transformPathCommand(command, transform) {
93
90
  y: y(command.y),
94
91
  };
95
92
  }
96
- return { ...command };
93
+ return { op: command.op, x: x(command.x), y: y(command.y) };
97
94
  }
@@ -65,6 +65,7 @@ export class SvgPainter {
65
65
  this.body.push(`<text x="${number(x)}" y="${number(y)}" `
66
66
  + `font-family="${escapeXml(family)}" font-size="${number(style.fontSize)}" `
67
67
  + `font-weight="${escapeXml(String(weight))}" text-anchor="${textAnchor}" `
68
+ + (style.fontStyle ? `font-style="${escapeXml(style.fontStyle)}" ` : "")
68
69
  + `fill="${escapeXml(fill)}" xml:space="preserve"`
69
70
  + `${opacityAttribute(style)}>${escapeXml(text)}</text>`);
70
71
  }
@@ -21,7 +21,7 @@ export declare class CanvasTextMeasurer implements TextMeasurer {
21
21
  constructor(context: CanvasRenderingContext2D);
22
22
  /**
23
23
  * 字体名称不变但实际资源变化时调用,例如 Web 字体加载完成后,旧缓存仍是回退字体的宽度。
24
- * 清理后宿主需重新编译、布局和绘制;仅切换字体族、字号或字重无需清理,它们已包含在缓存键中。
24
+ * 清理后宿主需重新编译、布局和绘制;切换字体族、字号、字重或斜体无需清理,它们已包含在缓存键中。
25
25
  */
26
26
  clearCache(): void;
27
27
  measureText(text: string, style: TextStyle): GlyphMetrics;
@@ -4,7 +4,8 @@ export const DEFAULT_TEXT_FONT = "sans-serif";
4
4
  export const JIANPU_NUMBER_FONT = "'Cascadia Mono', Consolas, 'Liberation Mono', 'Noto Sans Mono', monospace";
5
5
  /** Canvas 的 font 简写;测量与绘制必须用同一份,否则量到的不是将要画出的字形 */
6
6
  export function canvasFont(style) {
7
- return `${style.fontWeight ?? "normal"} ${style.fontSize}px ${style.fontFamily ?? DEFAULT_TEXT_FONT}`;
7
+ const slant = style.fontStyle ? `${style.fontStyle} ` : "";
8
+ return `${slant}${style.fontWeight ?? "normal"} ${style.fontSize}px ${style.fontFamily ?? DEFAULT_TEXT_FONT}`;
8
9
  }
9
10
  /** 默认文本测量保持确定性,不依赖浏览器和操作系统字体。 */
10
11
  export class DefaultTextMeasurer {
@@ -36,7 +37,7 @@ export class CanvasTextMeasurer {
36
37
  }
37
38
  /**
38
39
  * 字体名称不变但实际资源变化时调用,例如 Web 字体加载完成后,旧缓存仍是回退字体的宽度。
39
- * 清理后宿主需重新编译、布局和绘制;仅切换字体族、字号或字重无需清理,它们已包含在缓存键中。
40
+ * 清理后宿主需重新编译、布局和绘制;切换字体族、字号、字重或斜体无需清理,它们已包含在缓存键中。
40
41
  */
41
42
  clearCache() {
42
43
  this.widths.clear();
@@ -10,5 +10,4 @@ export declare class TranslatingPainter implements Painter {
10
10
  drawRect(x: number, y: number, w: number, h: number, style?: PaintStyle): void;
11
11
  drawCircle(cx: number, cy: number, r: number, style?: PaintStyle): void;
12
12
  drawPath(commands: readonly PathCommand[], style?: PaintStyle, transform?: PathTransform): void;
13
- private translateCommand;
14
13
  }
@@ -21,28 +21,11 @@ export class TranslatingPainter {
21
21
  this.target.drawCircle(cx + this.dx, cy + this.dy, r, style);
22
22
  }
23
23
  drawPath(commands, style, transform) {
24
- if (transform) {
25
- const moved = { ...transform, x: transform.x + this.dx, y: transform.y + this.dy };
26
- this.target.drawPath(commands, style, moved);
27
- return;
28
- }
29
- this.target.drawPath(commands.map(command => this.translateCommand(command)), style);
30
- }
31
- translateCommand(command) {
32
- switch (command.op) {
33
- case "Z": return command;
34
- case "Q": return {
35
- op: "Q",
36
- cx: command.cx + this.dx, cy: command.cy + this.dy,
37
- x: command.x + this.dx, y: command.y + this.dy,
38
- };
39
- case "C": return {
40
- op: "C",
41
- cx1: command.cx1 + this.dx, cy1: command.cy1 + this.dy,
42
- cx2: command.cx2 + this.dx, cy2: command.cy2 + this.dy,
43
- x: command.x + this.dx, y: command.y + this.dy,
44
- };
45
- default: return { op: command.op, x: command.x + this.dx, y: command.y + this.dy };
46
- }
24
+ this.target.drawPath(commands, style, {
25
+ x: (transform?.x ?? 0) + this.dx,
26
+ y: (transform?.y ?? 0) + this.dy,
27
+ scaleX: transform?.scaleX ?? 1,
28
+ scaleY: transform?.scaleY ?? 1,
29
+ });
47
30
  }
48
31
  }
@@ -30,6 +30,7 @@ export interface TextStyle {
30
30
  fontSize: number;
31
31
  fontFamily?: string;
32
32
  fontWeight?: string | number;
33
+ fontStyle?: "normal" | "italic" | "oblique";
33
34
  textAlign?: "left" | "center" | "right";
34
35
  fill?: string;
35
36
  opacity?: number;
package/grammar.md CHANGED
@@ -17,17 +17,18 @@ sidebar:
17
17
  | [音符](#音符) | `@note` |
18
18
  | [时值与小节线](#时值与小节线) | `@div` `@dot` `@dash` `@bar` |
19
19
  | [文本与符号](#文本与符号) | `@text` `@symbol` |
20
- | [换行](#换行) | `@br` |
20
+ | [换行](#换行) | `@br` `@pagebreak` / `@pb` |
21
21
  | [声部与歌词](#声部与歌词) | `@voice` `@voices` |
22
22
  | [堆叠与和弦](#堆叠与和弦) | `@stack` `@up` `@down` |
23
- | [琶音倚音与连音](#琶音倚音与连音) | `@arp` `@grace` `@tuplet` |
24
- | [外框与微调](#外框与微调) | `@box` `@adjust` |
25
- | [关系型:连线、反复房子、渐强渐弱](#关系型) | `@beam` `@tie` `@volta` `@dyn` |
23
+ | [琶音倚音与连音](#琶音倚音与连音) | `@arp` `@grace` `@tremolo` `@tuplet` |
24
+ | [外框与微调](#外框与微调) | `@box` `@adjust` `@slide` |
25
+ | [关系型:连线、反复房子、八度号、渐强渐弱](#关系型) | `@beam` `@tie` `@gliss` `@volta` `@ottava` `@dyn` |
26
26
 
27
27
  ## 2. 基本类型
28
28
  - **字符串**:双引号 `"..."`,无歧义时可省;含分隔符须加引号,如 `@note("b6,")`。
29
29
  - 可跨行;引号须配对,孤立 `"` 按普通字符处理。
30
- - `\` 转义后一个字符,如 `\"`;不支持 `\n` 等语义转义。
30
+ - 引号内仅支持 JSON 风格的 Unicode 转义:`\uXXXX` 为恰好四位十六进制码元,例如 `"\uE050"`;补充平面字符用 `"\uD834\uDD1E"` 这样的代理对表示。
31
+ - 其他 `\X` 仍转为 `X`,如 `\"`、`\\`;不新增 `\n`、`\t` 等语义转义,换行和制表符直接输入。转义只解码一次,`"\\uE050"` 显示字面文本 `\uE050`。
31
32
  - **布尔值**:`true` / `false`。
32
33
  - **数字**:有限整数或小数。
33
34
  - **长度**:数字加 `px`(默认)或 `em`(相对当前字号)。
@@ -57,12 +58,13 @@ sidebar:
57
58
 
58
59
  ### 页面与谱头
59
60
 
60
- - `@page(width, height, top, bottom, left, right, gap, numbering, font, fillRatio)`:页面布局。
61
+ - `@page(width, height, top, bottom, left, right, gap, numbering, barNumbering, font, fillRatio)`:页面布局。
61
62
  - 仅顶层首次声明有效,其余警告并忽略;`em` 按声明时字号计算。
62
63
  - 默认:宽 `794px`、高 `0px`、上 `45px`、下 `48px`、左右 `40px`、行距 `1em`、`fillRatio=0.5`。
63
- - `height <= 0` 不分页;正值按整行分页,单行超高报错。末页保持最小行距,其余页均分剩余高度。
64
+ - `height <= 0` 不自动分页,仍可手动换页;正值按整行分页,单行超高报错。自动封页均分剩余高度,末页保持最小行距。
64
65
  - `fillRatio`:自然宽度达到版心宽度的该比例时撑满;负值始终撑满,`>= 1` 不自动拉开。
65
- - `numbering`:页码格式,如 `"1/1"`,默认 `""` 不显示;`font`:页码字体,默认 `""` 沿用当前字体。
66
+ - `numbering`:页码格式,如 `"1/1"`,默认 `""` 不显示;`font`:页码/小节号字体,默认 `""` 沿用当前字体。
67
+ - `barNumbering`:从第二行起每行行首的小节号格式,如 `"1"`、`"(1)"`,默认 `""` 不显示。
66
68
 
67
69
  - `@head(left={}, center={}, right={}, gap=5px)`:谱头,`gap` 为栏内行距。
68
70
  - 只接受零时长内容;可嵌套,不自动换行。
@@ -85,8 +87,8 @@ sidebar:
85
87
  - 基本时值:一个四分音符(`1 QN`);先减时线、后附点,与嵌套顺序无关。
86
88
 
87
89
  - `@div(内容, n=1, autobeam=true)` / `@/(...)`:减时线,后缀 `/`;每条时值减半。
88
- - 内部始终连梁;独立 `div` 在同轨、同谱面行、同四分音符拍内连续时自动连梁。
89
- - 拍边界、小节线、换行、时间间隙、非候选可见元素、开关变化或手工 `@beam` 截断自动连接。
90
+ - 内部始终连梁;独立 `div` 在同轨、同谱面行、同一拍内连续且短于一拍时自动连梁(未设拍号、散板或分母 $\le 4$ 按 `1 QN`;分母 $\ge 8$ 且分子为 3 的倍数按 3 个分母单位;其余分母 $> 4$ 按 1 个分母单位)。
91
+ - 拍边界、小节线、拍号变更、换行、时间间隙、非候选可见元素、开关变化或手工 `@beam` 截断自动连接。
90
92
  - `autobeam=false` 只关闭与周围 `div` 的自动连接。
91
93
 
92
94
  - `@dot(内容, n=1)` / `@.(...)`:附点,后缀 `.`;单附点 ×1.5,双附点 ×1.75,如 `1/.`。
@@ -94,30 +96,36 @@ sidebar:
94
96
 
95
97
  - `@dash()` / `@-()`:增时线,简写 `-`;独占 `1 QN` 并延续前音,可加减时线或附点。
96
98
 
97
- - `@rest(n=1, width=3em, size=1em)`:整小节休止,必须先声明拍号。
99
+ - `@rest(n=1, width=3em, size=1em)`:整小节休止,必须先声明拍号。所有声部必须同时使用。
98
100
 
99
101
  - `@bar(type=0, length=1.25em)` / `@|(...)`:小节线,多声部间强制时间对齐。
100
- - `type`:`0` 普通 `|`,`1` 终止 `||`,`2` 反复起点 `|:`,`3` 反复终点 `:|`,`4` 双向反复 `:|:`。
102
+ - `type`:`0` 普通 `|`,`1` 双竖线 `||`,`2` 终止 `|||`,`3` 反复起点 `|:`,`4` 反复终点 `:|`,`5` 双向反复 `:|:`。
101
103
  - `:|` 回到此前最近的 `|:`,无起点则回曲首;每条只回跳一次,多条可增加遍数。
102
104
 
103
105
  ### 文本与符号
104
106
 
105
- - `@text(字符串, size=1em, lineheight=1.25, align=left)`:文本标记。
107
+ - `@text(字符串, size=1em, lineheight=1.25, align=left, font="")`:文本标记。
106
108
  - 简写 `"渐强"`;可直接换行,`lineheight` 为自身字号的倍数。
107
109
  - `align=left/center/right`:左对齐 / 居中 / 右对齐,锚点为首行首字中心 / 整块中心 / 首行末字中心。
110
+ - `font` 指定字体,空值沿用当前字体。宿主加载音乐字体后可写 `@text("\uE050", font="Bravura")`;Unicode 转义只决定字符,不会自动选择或加载字体。
108
111
 
109
112
  - `@symbol(name, size=0.5em)` / `@$(...)`:乐谱符号,简写 `$name`。
110
- - 名称匹配 `[A-Za-z][A-Za-z0-9_-]*`,按小写查找;未知名称警告,严格模式报错。
111
- - 装饰:`$tr` 颤音、`$prall` 上波音、`$mordent` 下波音、`$accent` 重音;附在 `^` / `_` 上才影响播放,独立写出仅绘制。
112
- - 延长:`$fermata`,目标区间速度减半,记谱时值不变。
113
- - 力度:`$ppp` `$pp` `$p` `$mp` `$mf` `$f` `$ff` `$fff`,默认 `$mf`;同轨持续至下一记号,反复时恢复目标位置的力度。
114
- - 新声部继承分叉处力度,开始发声后不再跟随父轨变化。
115
- - 跳转:`$dc` 回曲首;`$ds` 回此前最近的 `$segno`,无记号则回曲首。均只跳一次,回跳后忽略 `|: :|`。
116
- - 终止:`$fine` 仅在其后最近的 `$dc` / `$ds` 已回跳时生效。
117
- - `$dc`、`$ds`、`$fine` 应附在小节线上,如 `| _$dc`;执行跳转或终止时,该时间列不发声。
113
+ - 名称按小写精确查表,未命中时尝试各符号的可选正则,要求完整匹配;未知名称警告,严格模式报错。
114
+ - `$` 后连续的非空白文本作为完整名称,直到空白或当前内容区间末尾。
115
+ - 装饰:`$tr` 颤音、`$prall` 上波音、`$mordent` 下波音、`$turn` 回音、`$iturn` 逆回音。
116
+ - 奏法:`$accent` 重音、`$marcato` 强调音、`$staccato` 断奏、`$tenuto` 保持音。
117
+ - 技法:`$wheel` 轮指、`$harmonic` 泛音。
118
+ - 颤音尾巴:`$tr+`、`$tr++`、`$tr+++` 等。
119
+ - 延长:`$fermata` 延长号。
120
+ - 力度:`$ppp`、`$pp`、`$p`、`$mp`、`$mf`、`$f`、`$ff`、`$fff` 等。
121
+ - 跳转:`$dc` 回曲首、`$ds` 回标记、`$segno` 标记、`$fine` 终止、`$tocoda` 跳尾声、`$coda` 尾声标记。
118
122
 
119
123
  ### 换行
120
124
 
125
+ - `@pagebreak(justify=true)` / `@pb(...)`:结束当前页
126
+ - 有限页高下默认分配当前页的剩余空间;`justify=false` 只保留最小行距。无限页高按自然高度结束,不拉伸。
127
+ - 同时刻的并行调用只换一次页,任一 `justify=false` 则不拉伸;同轨连续调用分别执行。
128
+
121
129
  - `@br(offset=1)`:后续内容偏移指定谱面行数。
122
130
  - 顶层空行(连续至少两个换行)等价于一次 `@br()`;普通源码换行及块内空行不换谱面行。
123
131
  - `offset` 向下取整,负值按 0;`@br(0)` 不换行,`@br(2)` 留一空行,多次调用累加。
@@ -134,7 +142,7 @@ sidebar:
134
142
  - 位置参数须为 `@voice`,各占独立音轨;连续 `N:` 及所属 `L:` 自动成组,空行、作用域结束或普通非成员内容结束组。
135
143
  - **connect(仅命名传参)**:只改外观。`"[-]"` 全组带钩括线,`"{-}"` 全组大括号,`""` 或纯空白字符串仅公共细连谱线;单声部组不画连接。
136
144
  - 范围可组合,如 `"[1-4]{5-7}"`;从 1 编号,只数直接声部,含两端,省略端点取首 / 末声部。
137
- - 端点为正整数,不可反向或越界;单成员写 `[3-3]`;可重叠,不自动嵌套或避让。
145
+ - 端点为正整数,不可反向或越界;单成员可写 `[3]` 或 `{3}`,分别等价于 `[3-3]` 或 `{3-3}`;可重叠,不自动嵌套或避让。
138
146
  - **连接声明**:`V{}:` 大括号、`V[]:` 带钩括线、`V|:` 仅细线;不限行首,后须有 `N:`,可同行或换行。
139
147
  - 到下一 `V` 或组末结束,不拆分并行组;相邻同形声明仍独立,`L:` 不跨声明寻找声部。
140
148
  - **默认值**:省略 `connect` 时取 `@set` 预设或内置 `"[-]"`;显式参数或本组 `V` 声明优先。任一 `V` 完整替换本组预设,未声明部分仅细线,不影响后续组。
@@ -165,9 +173,13 @@ sidebar:
165
173
  - `2>1` 前倚音(`pre`),`1<2` 后倚音(`post`),**箭头指向宿主**;多倚音用 `{3 2}>1`。
166
174
  - 宿主须为一个可见事件;倚音至少一个可见对象,可含和弦、连音、多声部及状态设置,不可换行。
167
175
  - 倚音默认八分音符,后缀 `/` 变十六分;相邻减时线自动连接。
168
- - 不增正文时值;借宿主实际时值的比例为 `min(倚音字面总时长 / 1 QN, 0.75)`,多声部取最长分支,在宿主轨同步起奏。
169
176
  - 可嵌套,`>` 左结合、`<` 右结合,字号逐层缩小;成员标签保留,调用后的标签绑定宿主。
170
177
 
178
+ - `@tremolo(content, n=3)`:震音。
179
+ - `n`:斜杠数量,取 `1..3` 的整数,默认 `3`。
180
+ - **单音震音**:`content` 为单个发声音符或和弦(后可接增时线 `-`),斜杠记在最末元素右下角,按 `1 / 2^n QN` 同音重复。
181
+ - **双音震音**:`content` 为两个同轨、同谱面行、等时值的相邻发声音符或和弦,斜杠记在两音之间,在两音总时值内按 `1 / 2^n QN` 交替发声。
182
+
171
183
  - `@tuplet(content, normal)`:多连音。
172
184
  - 显示数字 `actual = 原总时值 / 最短正时值`,须为不小于 2 的整数;所有时值乘以 `normal / actual`。
173
185
  - 如 `@tuplet({1/ 2 3}, 4)` 显示 5,总长 `2 QN`。
@@ -185,16 +197,31 @@ sidebar:
185
197
  - `dx/dy` 平移,邻居不避让,关系线跟随;`dw/dh` 改占位,邻居随之移动,宽高下限为 0。
186
198
  - `dw` 按左右占位比例分配,`dh` 只改下方;多个对象逐个生效,可调整和弦、倚音成员或单根关系线。
187
199
 
200
+ - `@slide(内容, side=pre, direction=up)`:纯视觉的滑入/滑出箭头。
201
+ - `pre` 修饰首个可绘制对象,`post` 修饰末个可绘制对象;多音内容只绘制一个记号。
202
+ - `up/down` 表示随时间上行/下行;四种组合分别为低处滑入、高处滑入、向高处滑出、向低处滑出。
203
+
188
204
  ### 关系型
205
+ - `@gliss(标签1, 标签2)`:滑音斜直箭头,不改变记谱时值;两个位置标签必填,不接受命名参数。
206
+ - 标签顺序不限,按谱面时间自动确定起点和终点;`@gliss(a,b)` 与 `@gliss(b,a)` 等价,同时刻端点不合法。
207
+ - 端点不限对象类型,但须具有排版盒子。优先读取端点自身的 `resolvedMidi`;没有该属性时沿同轨向前找第一个带该属性的对象,可跨时间缺口和谱面行,找到后即停止,空值不继续追溯。
208
+ - 两端音高均为有限数值时,同轨按音高绘制,并在起点(或所标增时线及其后续延音)区间内按半音阶离散步进至终点音高;无法比较音高时发出 `W_GLISS_NO_PITCH` 警告并画水平箭头,每条关系只警告一次。
209
+ - 从被标记对象右侧连接到终点左侧,避让附点与升降号,不自动移至最后一根增时线。
210
+ - 如 `1 - -@a 5@b @gliss(a,b)`;增时线缺少自身音高时,由 gliss 查找前方音高,并从该增时线起点开始半音步进。
211
+ - 同一谱面行内可跨轨;跨轨始终连接实际端点,斜率不代表音高方向。不支持跨行、跨页、同一倚音组内部连接或复杂绕障。
189
212
  - `@beam(...端点标签)`:手工连梁,不受 `div.autobeam` 影响。
190
213
  - 端点须按时间顺序指向同轨、同谱面行的相邻可见元素,可跨零时长小节线;和弦仅宿主参与。
191
- - `@tie(...端点标签, height=0.5em)`:连音线 / 延音线。
214
+ - `@tie(...端点标签, height=0)`:连音线 / 延音线。
192
215
  - 按传入顺序连接相邻端点;不足两个时,向前寻找最近的可标注对象补足。
193
- - `height` 仅命名,负值按 0;跨行分段,跨轨可画线。
216
+ - `height` 仅命名;非正数为自动模式,按水平跨度平滑递增,在 `2.5em` 跨度处达到 `0.5em` 上限(em 相对调用处字号);正数指定固定高度。
217
+ - 跨行分段,自动模式下各段统一使用 `0.45em` 高度,不随局部跨度变化;跨轨可画线。
194
218
  - 仅**同轨、同音高、时间连续**时合并发声,否则只画线。
195
219
  - `@volta(from, to, pass, ...)`:反复房子,限定区间演奏遍数。
196
220
  - 首末标签可相同、顺序不限;遍数为必填正整数,可多个,如 `@volta(a,b,1,2)`,自动排序去重。
197
221
  - 区间位置决定所属反复段,各段独立计遍;重叠房子限定互斥遍数时警告,重叠列不演奏。
222
+ - `@ottava(a, b, shift=1)`:八度号,前两个标签仅支持位置传参;`shift` 为非零整数八度数,正数为高、负数为低,可命名传参。
223
+ - 标签按 `7 * abs(shift) + 1` 显示:`±1` 为 8、`±2` 为 15、`±3` 为 22,以此类推;使用 `numberfont` 的斜体。
224
+ - 只改变区间内同轨的播放音高(`12 * shift` 半音,须为安全整数),不改变记谱音符、八度点或时值;其他音轨和打击节拍不受影响。
198
225
  - `@dyn(from, to, dv)`:渐强渐弱。
199
226
  - 标签须同轨、不同时间,顺序不限,可指向增时线;按时间从早到晚生效。
200
227
  - `dv`:非零力度增量,正强负弱;超出 `-127..127` 的有限值限制并警告。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jpfun",
3
- "version": "0.6.0",
3
+ "version": "0.8.0",
4
4
  "description": "A functional numbered musical notation typesetting engine",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -27,6 +27,7 @@
27
27
  "dist",
28
28
  "README.md",
29
29
  "LICENSE",
30
+ "Bravura-LICENSE.txt",
30
31
  "grammar.md"
31
32
  ],
32
33
  "engines": {
@@ -50,8 +51,7 @@
50
51
  "build:bundle": "node ../../scripts/build-jpfun-bundles.mjs && node --test ../../scripts/test-jpfun-bundles.mjs",
51
52
  "prepack": "pnpm run build",
52
53
  "typecheck": "tsc -p test/tsconfig.json",
53
- "test": "node --import tsx --test \"test/*.test.ts\"",
54
- "test:update": "node --import tsx --test \"test/*.test.ts\""
54
+ "test": "node --import tsx --test \"test/*.test.ts\""
55
55
  },
56
56
  "keywords": [
57
57
  "numbered-musical-notation",
@@ -1,8 +0,0 @@
1
- import { Diagnostic } from "../../diagnostic.js";
2
- export declare function parseNoteName(str: string, start?: number, end?: number): {
3
- name: string;
4
- next: number;
5
- acc: string | null;
6
- octave: number | null;
7
- absOctave: boolean;
8
- } | Diagnostic;