jpfun 0.3.0 → 0.4.1

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 (129) hide show
  1. package/README.md +73 -1
  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/functions/ASTtypes.d.ts +30 -26
  23. package/dist/functions/ASTtypes.js +25 -23
  24. package/dist/functions/adjust/index.js +13 -6
  25. package/dist/functions/arpeggio/index.d.ts +2 -0
  26. package/dist/functions/arpeggio/index.js +184 -0
  27. package/dist/functions/arpeggio/shape.d.ts +7 -0
  28. package/dist/functions/arpeggio/shape.js +122 -0
  29. package/dist/functions/bar/index.js +30 -23
  30. package/dist/functions/beam/auto.d.ts +1 -1
  31. package/dist/functions/beam/auto.js +1 -1
  32. package/dist/functions/beam/index.js +6 -1
  33. package/dist/functions/beam/layout.d.ts +3 -3
  34. package/dist/functions/beam/layout.js +5 -3
  35. package/dist/functions/box/index.js +10 -3
  36. package/dist/functions/br/index.js +10 -6
  37. package/dist/functions/dash/index.js +12 -9
  38. package/dist/functions/default.js +6 -2
  39. package/dist/functions/div/index.d.ts +4 -1
  40. package/dist/functions/div/index.js +8 -7
  41. package/dist/functions/dot/index.js +18 -25
  42. package/dist/functions/dyn/index.d.ts +2 -0
  43. package/dist/functions/dyn/index.js +242 -0
  44. package/dist/functions/grace/index.d.ts +1 -1
  45. package/dist/functions/grace/index.js +17 -11
  46. package/dist/functions/head/index.js +168 -159
  47. package/dist/functions/key/index.js +12 -7
  48. package/dist/functions/meter/index.js +14 -6
  49. package/dist/functions/note/index.js +73 -31
  50. package/dist/functions/note/noteNameFSM.js +52 -63
  51. package/dist/functions/page/index.d.ts +5 -1
  52. package/dist/functions/page/index.js +38 -16
  53. package/dist/functions/program/index.d.ts +2 -0
  54. package/dist/functions/program/index.js +39 -0
  55. package/dist/functions/set/index.js +13 -8
  56. package/dist/functions/stack/index.js +7 -5
  57. package/dist/functions/symbol/index.d.ts +13 -3
  58. package/dist/functions/symbol/index.js +71 -22
  59. package/dist/functions/symbol/shape.d.ts +2 -0
  60. package/dist/functions/symbol/shape.js +14 -10
  61. package/dist/functions/symbol/symbols/dc.d.ts +4 -0
  62. package/dist/functions/symbol/symbols/dc.js +82 -0
  63. package/dist/functions/symbol/symbols/{f.d.ts → ds.d.ts} +1 -1
  64. package/dist/functions/symbol/symbols/ds.js +93 -0
  65. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  66. package/dist/functions/symbol/symbols/dynamics.js +168 -0
  67. package/dist/functions/symbol/symbols/{p.d.ts → fine.d.ts} +1 -1
  68. package/dist/functions/symbol/symbols/fine.js +12 -0
  69. package/dist/functions/symbol/symbols/mordent.js +44 -31
  70. package/dist/functions/symbol/symbols/prall.d.ts +2 -0
  71. package/dist/functions/symbol/symbols/prall.js +42 -0
  72. package/dist/functions/symbol/symbols/segno.d.ts +4 -0
  73. package/dist/functions/symbol/symbols/segno.js +82 -0
  74. package/dist/functions/symbol/symbols/tr.js +0 -4
  75. package/dist/functions/tempo/index.js +12 -4
  76. package/dist/functions/temporal.d.ts +138 -0
  77. package/dist/functions/temporal.js +101 -0
  78. package/dist/functions/text/index.js +25 -10
  79. package/dist/functions/tie/index.js +45 -30
  80. package/dist/functions/tuplet/index.js +21 -8
  81. package/dist/functions/up/index.js +38 -33
  82. package/dist/functions/voice/index.js +153 -68
  83. package/dist/functions/volta/index.js +62 -55
  84. package/dist/iife/core.d.ts +1 -0
  85. package/dist/iife/core.js +3 -0
  86. package/dist/iife/from-midi.d.ts +1 -0
  87. package/dist/iife/from-midi.js +15 -0
  88. package/dist/iife/from-musicxml.d.ts +1 -0
  89. package/dist/iife/from-musicxml.js +3 -0
  90. package/dist/iife/full.d.ts +1 -0
  91. package/dist/iife/full.js +3 -0
  92. package/dist/index.d.ts +3 -21
  93. package/dist/index.js +3 -21
  94. package/dist/jpfun.core.min.js +119 -0
  95. package/dist/jpfun.from-midi.min.js +9 -0
  96. package/dist/jpfun.from-musicxml.min.js +12 -0
  97. package/dist/jpfun.min.js +111 -122
  98. package/dist/layout/default.js +2 -3
  99. package/dist/layout/engine.d.ts +2 -1
  100. package/dist/layout/engine.js +71 -26
  101. package/dist/layout/model.d.ts +4 -4
  102. package/dist/layout/model.js +15 -12
  103. package/dist/layout/page.js +5 -4
  104. package/dist/layout/path.js +3 -3
  105. package/dist/layout/types.d.ts +20 -2
  106. package/dist/lowering/loweringContext.d.ts +3 -1
  107. package/dist/lowering/loweringContext.js +28 -14
  108. package/dist/lowering/track.d.ts +4 -8
  109. package/dist/lowering/track.js +6 -1
  110. package/dist/lowering/types.d.ts +5 -128
  111. package/dist/lowering/types.js +1 -98
  112. package/dist/parser/parse-utils/string-utils.d.ts +2 -0
  113. package/dist/parser/parse-utils/string-utils.js +4 -0
  114. package/dist/parser/parserContext.d.ts +7 -8
  115. package/dist/parser/parserContext.js +41 -36
  116. package/dist/pipeline.d.ts +6 -5
  117. package/dist/pipeline.js +4 -5
  118. package/dist/playback/compile.d.ts +1 -1
  119. package/dist/playback/compile.js +15 -2
  120. package/dist/playback/event.d.ts +17 -3
  121. package/dist/playback/event.js +30 -12
  122. package/dist/playback/time.js +1 -1
  123. package/dist/playback/types.d.ts +5 -4
  124. package/dist/render/text.d.ts +6 -1
  125. package/dist/render/text.js +8 -1
  126. package/grammar.md +111 -57
  127. package/package.json +11 -2
  128. package/dist/functions/symbol/symbols/f.js +0 -55
  129. package/dist/functions/symbol/symbols/p.js +0 -61
@@ -0,0 +1,122 @@
1
+ import { pathBounds } from "../../layout/path.js";
2
+ const TILE_WIDTH = 54.2813;
3
+ const TILE_HEIGHT = 87.37505;
4
+ const TILE_STEP = 72.8125;
5
+ const ARROW_HEIGHT = 93.6719;
6
+ const ARROW_X = -27.4688;
7
+ const ARROW_Y = -7.9375;
8
+ const ARROW_BOTTOM = ARROW_Y + ARROW_HEIGHT;
9
+ /** MuseScore 4.7.4 导出的琶音波形块,已旋转到自上而下的局部坐标。 */
10
+ const TILE = [
11
+ { op: "M", x: 28.1406, y: 86.06255 },
12
+ { op: "C", cx1: 28.1406, cy1: 86.06255, cx2: 30.4531, cy2: 84.07815, x: 32.7656, y: 81.42185 },
13
+ { op: "C", cx1: 33.7656, cy1: 80.43755, cx2: 34.0938, cy2: 79.10935, x: 34.0938, y: 78.10935 },
14
+ { op: "C", cx1: 34.0938, cy1: 77.12505, cx2: 33.7656, cy2: 76.45315, x: 33.0938, y: 75.79685 },
15
+ { op: "L", x: 29.4531, y: 72.15625 },
16
+ { op: "C", cx1: 28.7969, cy1: 71.50005, cx2: 28.4688, cy2: 70.82815, x: 28.4688, y: 70.17185 },
17
+ { op: "C", cx1: 28.4688, cy1: 68.84375, cx2: 29.4531, cy2: 68.18755, x: 29.4531, y: 68.18755 },
18
+ { op: "C", cx1: 29.4531, cy1: 68.18755, cx2: 47.6563, cy2: 47.00005, x: 50.6406, y: 43.68755 },
19
+ { op: "C", cx1: 53.2969, cy1: 40.04685, cx2: 54.2813, cy2: 36.40625, x: 54.2813, y: 33.09375 },
20
+ { op: "C", cx1: 54.2813, cy1: 26.14065, cx2: 50.3125, cy2: 19.85935, x: 47.3281, y: 17.21875 },
21
+ { op: "L", x: 31.4375, y: 1.32812 },
22
+ { op: "C", cx1: 30.4531, cy1: 0.32812, cx2: 29.7969, cy2: 0, x: 28.7969, y: 0 },
23
+ { op: "C", cx1: 27.1406, cy1: 0, cx2: 25.8125, cy2: 1.32812, x: 25.8125, y: 1.32812 },
24
+ { op: "C", cx1: 25.8125, cy1: 1.32812, cx2: 23.5, cy2: 3.3125, x: 21.1875, y: 5.95312 },
25
+ { op: "C", cx1: 20.1875, cy1: 6.95313, cx2: 19.8594, cy2: 7.9375, x: 19.8594, y: 8.9375 },
26
+ { op: "C", cx1: 19.8594, cy1: 9.92188, cx2: 20.1875, cy2: 10.59375, x: 20.8594, y: 11.25 },
27
+ { op: "L", x: 24.5, y: 14.89063 },
28
+ { op: "C", cx1: 25.1563, cy1: 15.5625, cx2: 25.4844, cy2: 16.21875, x: 25.4844, y: 16.875 },
29
+ { op: "C", cx1: 25.4844, cy1: 18.20315, cx2: 24.5, cy2: 19.20315, x: 24.5, y: 19.20315 },
30
+ { op: "C", cx1: 24.5, cy1: 19.20315, cx2: 6.625, cy2: 40.37505, x: 3.64063, y: 43.68755 },
31
+ { op: "C", cx1: 1, cy1: 47.32815, cx2: 0, cy2: 50.96875, x: 0, y: 54.28125 },
32
+ { op: "C", cx1: 0, cy1: 61.23435, cx2: 3.96875, cy2: 67.51565, x: 6.95313, y: 70.17185 },
33
+ { op: "L", x: 22.8438, y: 86.06255 },
34
+ { op: "C", cx1: 23.8281, cy1: 87.04685, cx2: 24.8281, cy2: 87.37505, x: 25.4844, y: 87.37505 },
35
+ { op: "C", cx1: 27.1406, cy1: 87.37505, cx2: 28.1406, cy2: 86.06255, x: 28.1406, y: 86.06255 },
36
+ ];
37
+ /** MuseScore 下行琶音的末端箭头轮廓,坐标方向同 TILE。 */
38
+ const DOWN_ARROW = [
39
+ { op: "M", x: 111.8751, y: 21.5156 },
40
+ { op: "C", cx1: 112.5313, cy1: 20.1875, cx2: 113.5313, cy2: 18.2031, x: 113.5313, y: 16.875 },
41
+ { op: "C", cx1: 113.5313, cy1: 12.23438, cx2: 109.8907, cy2: 8.9375, x: 105.2501, y: 8.9375 },
42
+ { op: "C", cx1: 103.9219, cy1: 8.9375, cx2: 101.6094, cy2: 9.26563, x: 100.2813, y: 10.25 },
43
+ { op: "L", x: 72.1563, y: 31.7656 },
44
+ { op: "L", x: 72.1563, y: 28.4531 },
45
+ { op: "C", cx1: 72.1563, cy1: 16.54688, cx2: 66.8594, cy2: 8.26563, x: 58.2501, y: 0.98437 },
46
+ { op: "C", cx1: 57.2657, cy1: 0.32812, cx2: 55.9376, cy2: 0, x: 55.2657, y: 0 },
47
+ { op: "C", cx1: 53.6251, cy1: 0, cx2: 52.9532, cy2: 0.98437, x: 52.9532, y: 0.98437 },
48
+ { op: "C", cx1: 51.6251, cy1: 2.3125, cx2: 49.3126, cy2: 4.29687, x: 48.3282, y: 5.625 },
49
+ { op: "C", cx1: 47.3282, cy1: 6.9375, cx2: 47.0001, cy2: 7.9375, x: 47.0001, y: 8.9375 },
50
+ { op: "C", cx1: 47.0001, cy1: 9.92188, cx2: 47.3282, cy2: 10.57813, x: 47.6563, y: 11.25 },
51
+ { op: "C", cx1: 49.9688, cy1: 13.5625, cx2: 50.9688, cy2: 16.21875, x: 50.9688, y: 18.8594 },
52
+ { op: "C", cx1: 50.9688, cy1: 24.8125, cx2: 46.3282, cy2: 30.1094, x: 41.0469, y: 30.1094 },
53
+ { op: "C", cx1: 39.3907, cy1: 30.1094, cx2: 37.7344, cy2: 29.4531, x: 36.4063, y: 28.4531 },
54
+ { op: "L", x: 12.9063, y: 10.57813 },
55
+ { op: "C", cx1: 11.5782, cy1: 9.59375, cx2: 9.5938, cy2: 8.9375, x: 7.9375, y: 8.9375 },
56
+ { op: "C", cx1: 3.6407, cy1: 8.9375, cx2: 0, cy2: 12.57813, x: 0, y: 16.875 },
57
+ { op: "C", cx1: 0, cy1: 18.2031, cx2: 0.6563, cy2: 20.1875, x: 1.3125, y: 21.5156 },
58
+ { op: "L", x: 49.9688, y: 90.3594 },
59
+ { op: "C", cx1: 51.2969, cy1: 92.3438, cx2: 54.2813, cy2: 93.6719, x: 56.5938, y: 93.6719 },
60
+ { op: "C", cx1: 58.9063, cy1: 93.6719, cx2: 61.8907, cy2: 92.3438, x: 63.2188, y: 90.3594 },
61
+ { op: "L", x: 111.8751, y: 21.5156 },
62
+ ];
63
+ function appendShape(output, shape, width, height, offsetX, offsetY, scale, reverse) {
64
+ const x = (value) => offsetX + (reverse ? width - value : value) * scale;
65
+ const y = (value) => offsetY + (reverse ? height - value : value) * scale;
66
+ for (const command of shape) {
67
+ if (command.op === "Z")
68
+ output.push(command);
69
+ else if (command.op === "C")
70
+ output.push({
71
+ op: "C",
72
+ cx1: x(command.cx1), cy1: y(command.cy1),
73
+ cx2: x(command.cx2), cy2: y(command.cy2),
74
+ x: x(command.x), y: y(command.y),
75
+ });
76
+ else if (command.op === "Q")
77
+ output.push({
78
+ op: "Q",
79
+ cx: x(command.cx), cy: y(command.cy),
80
+ x: x(command.x), y: y(command.y),
81
+ });
82
+ else
83
+ output.push({ op: command.op, x: x(command.x), y: y(command.y) });
84
+ }
85
+ }
86
+ /** 按 MuseScore 的原始重叠比例铺满指定高度,并一次性返回最终边界。 */
87
+ export function prepareArpeggioShape(height, em, direction) {
88
+ const arrow = direction === "up" || direction === "down";
89
+ const segments = Math.max(1, Math.round(height / (em * 0.3)));
90
+ const regularCount = arrow ? Math.max(0, segments - 1) : segments;
91
+ const naturalHeight = !arrow
92
+ ? TILE_HEIGHT + TILE_STEP * (regularCount - 1)
93
+ : ARROW_BOTTOM + TILE_STEP * regularCount;
94
+ const scale = height / naturalHeight;
95
+ const commands = [];
96
+ for (let index = 0; index < regularCount; index++) {
97
+ appendShape(commands, TILE, TILE_WIDTH, TILE_HEIGHT, 0, index * TILE_STEP * scale, scale, direction === "");
98
+ }
99
+ if (arrow) {
100
+ appendShape(commands, DOWN_ARROW, 113.5313, ARROW_HEIGHT, ARROW_X * scale, (regularCount * TILE_STEP + ARROW_Y) * scale, scale, false);
101
+ }
102
+ const bounds = pathBounds(commands);
103
+ if (direction !== "up")
104
+ return { commands, bounds };
105
+ const rotated = commands.map(command => command.op === "Z" ? command : command.op === "C" ? {
106
+ op: "C",
107
+ cx1: bounds.x + bounds.w - (command.cx1 - bounds.x), cy1: height - command.cy1,
108
+ cx2: bounds.x + bounds.w - (command.cx2 - bounds.x), cy2: height - command.cy2,
109
+ x: bounds.x + bounds.w - (command.x - bounds.x), y: height - command.y,
110
+ } : command.op === "Q" ? {
111
+ op: "Q",
112
+ cx: bounds.x + bounds.w - (command.cx - bounds.x), cy: height - command.cy,
113
+ x: bounds.x + bounds.w - (command.x - bounds.x), y: height - command.y,
114
+ } : {
115
+ op: command.op,
116
+ x: bounds.x + bounds.w - (command.x - bounds.x), y: height - command.y,
117
+ });
118
+ return {
119
+ commands: rotated,
120
+ bounds: { x: bounds.x, y: height - bounds.y - bounds.h, w: bounds.w, h: bounds.h },
121
+ };
122
+ }
@@ -1,26 +1,25 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { ANCHOR_KEY, TemporalNodeBase } from "../../lowering/types.js";
2
+ import { ANCHOR_KEY, isVisualTemporalNode, TemporalNodeBase } from "../temporal.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: ':|:' 左右反复
14
- `,
7
+ details: `\
8
+ ~~~jpfun
9
+ @bar(0, length=1.25em)
10
+ ~~~
11
+ 有语法糖简写,见 type 参数的说明。`,
15
12
  allowExtraArgs: false,
16
13
  args: [
17
14
  {
18
15
  name: "type",
16
+ description: "0:普通线 `|`;1:终止线 `||`(左细右粗);2:反复起点 `|:`;3:反复终点 `:|`;4:左右反复 `:|:`",
19
17
  type: "number",
20
18
  default: 0,
21
19
  },
22
20
  {
23
21
  name: "length",
22
+ description: "小节线高度",
24
23
  type: "length",
25
24
  default: {
26
25
  value: 1.25,
@@ -94,12 +93,17 @@ class BarTemporalNode extends TemporalNodeBase {
94
93
  * 给 bar 两侧的相邻元素增加 mu,避免被压缩到一起
95
94
  */
96
95
  prepareHorizontal(line) {
97
- const run = line.trackRuns.get(this.track);
98
- const index = run?.indexOf(this) ?? -1;
96
+ let host = this;
97
+ while (host.foldedInto)
98
+ host = host.foldedInto;
99
+ if (!isVisualTemporalNode(host))
100
+ return;
101
+ const run = line.trackRuns.get(host.track);
102
+ const index = run?.indexOf(host) ?? -1;
99
103
  if (!run || index < 0)
100
104
  return;
101
- line.registerHorizontalLayoutHook(this, this, ({ columns, start }) => {
102
- const current = columns[start].find(element => element.time === this);
105
+ line.registerHorizontalLayoutHook(host, host, ({ columns, start }) => {
106
+ const current = columns[start].find(element => element.time === host);
103
107
  if (!current)
104
108
  return;
105
109
  const elementOf = (node) => {
@@ -114,7 +118,7 @@ class BarTemporalNode extends TemporalNodeBase {
114
118
  const rightNode = run[index + 1];
115
119
  const right = rightNode ? elementOf(rightNode) : undefined;
116
120
  // 相邻两个 bar 的中间 gap 由右侧 bar 处理,避免重复增强
117
- if (right && !(rightNode instanceof BarTemporalNode)) {
121
+ if (right && rightNode.mergeKey !== ANCHOR_KEY) {
118
122
  current.config.mu_R *= 4;
119
123
  right.config.mu_L *= 4;
120
124
  }
@@ -123,7 +127,7 @@ class BarTemporalNode extends TemporalNodeBase {
123
127
  prepareLayout() {
124
128
  const { type, size } = this.ast;
125
129
  this.lines.length = 0;
126
- this.repeatDots.length = 0;
130
+ const repeatDots = [];
127
131
  const h = size;
128
132
  const thin = Math.max(1, size * 0.065);
129
133
  const thick = thin * 2.6;
@@ -151,27 +155,30 @@ class BarTemporalNode extends TemporalNodeBase {
151
155
  }
152
156
  if (hasLeftDots) {
153
157
  const dotX = dotRadius;
154
- this.repeatDots.push({ x: dotX, y: h * 0.38 });
155
- this.repeatDots.push({ x: dotX, y: h * 0.62 });
158
+ repeatDots.push({ x: dotX, y: h * 0.38 });
159
+ repeatDots.push({ x: dotX, y: h * 0.62 });
156
160
  }
157
161
  if (hasRightDots) {
158
162
  const dotX = x + dotGap;
159
- this.repeatDots.push({ x: dotX, y: h * 0.38 });
160
- this.repeatDots.push({ x: dotX, y: h * 0.62 });
163
+ repeatDots.push({ x: dotX, y: h * 0.38 });
164
+ repeatDots.push({ x: dotX, y: h * 0.62 });
161
165
  x = dotX + dotRadius;
162
166
  }
163
167
  this.box.w = x;
164
168
  this.box.h = h;
165
- this.box.anchor = x / 2;
169
+ const anchorLine = type >= 2 ? this.lines[0] : undefined;
170
+ this.box.anchor = anchorLine ? anchorLine.x + anchorLine.w / 2 : x / 2;
166
171
  this.box.visualAxis = h / 2;
172
+ for (const line of this.lines)
173
+ line.x -= this.box.anchor;
174
+ this.repeatDots = repeatDots.map(dot => ({ x: dot.x - this.box.anchor, y: dot.y, r: dotRadius }));
167
175
  }
168
176
  paint(painter) {
169
177
  for (const line of this.lines) {
170
- painter.drawRect(this.box.x + line.x, this.box.y, line.w, this.box.h, { fill: "#000" });
178
+ painter.drawRect(this.box.x + this.box.anchor + line.x, this.box.y, line.w, this.box.h, { fill: "#000" });
171
179
  }
172
- const radius = Math.max(1, this.box.w * 0.06);
173
180
  for (const dot of this.repeatDots) {
174
- painter.drawCircle(this.box.x + dot.x, this.box.y + dot.y, radius, { fill: "#000" });
181
+ painter.drawCircle(this.box.x + this.box.anchor + dot.x, this.box.y + dot.y, dot.r, { fill: "#000" });
175
182
  }
176
183
  }
177
184
  }
@@ -1,4 +1,4 @@
1
- import { type LoweringResult } from "../../lowering/types.js";
1
+ import type { LoweringResult } from "../../lowering/types.js";
2
2
  import type { LayoutAttachment } from "../../layout/types.js";
3
3
  /** beam 域在 lowering 固化后统一生成作用域连接和相邻自动连接 */
4
4
  export declare function createAutomaticBeamAttachments(result: LoweringResult): LayoutAttachment[];
@@ -1,4 +1,4 @@
1
- import { ANCHOR_KEY, isVisualTemporalNode, } from "../../lowering/types.js";
1
+ import { ANCHOR_KEY, isVisualTemporalNode, } from "../temporal.js";
2
2
  import { Fraction } from "../../fraction.js";
3
3
  import { DIV_ADDON_KEY, DivNode, } from "../div/index.js";
4
4
  import { collectExplicitBeamEndpoints, createBeamLayoutAttachment, } from "./layout.js";
@@ -8,7 +8,12 @@ class BeamFunction extends ASTFunctionNode {
8
8
  static def = {
9
9
  name: ["beam"],
10
10
  description: "减时线连接",
11
- example: "@beam(label1, label2, ...): 将多个已标记音符按顺序连接为减时线组",
11
+ details: `\
12
+ 事后将多个减时线**强行**相连
13
+ ~~~jpfun
14
+ 1/@a 2/@b | 3/@c @beam(a, b, c)
15
+ ~~~
16
+ **位置参数**:已标记音符的标签,按传入顺序连接减时线,显式指定一组。先用 \`/\` 或 \`@div\` 为目标添加减时线,再用 \`@beam\` 控制连接。端点须同轨、同谱面行且相邻,可跨小节线;连线直接穿过小节线,保留原有间距。`,
12
17
  allowExtraArgs: true,
13
18
  extraArgType: "label",
14
19
  args: [],
@@ -1,10 +1,10 @@
1
- import { type LoweringResult, type TemporalNodeBase } from "../../lowering/types.js";
1
+ import { type TemporalNodeBase } from "../temporal.js";
2
2
  import type { SourceSpan } from "../../parser/types.js";
3
3
  import type { LayoutAttachment } from "../../layout/types.js";
4
- import type { LoweringAttachment } from "../../lowering/types.js";
4
+ import type { LoweringAttachment, LoweringResult } from "../../lowering/types.js";
5
5
  /** 显式和自动分组共享同一个几何对象 */
6
6
  export declare function createBeamLayoutAttachment(endPoints: TemporalNodeBase[], explicit: boolean, sourceSpan: SourceSpan): LayoutAttachment;
7
- /** 显式 beam 必须严格连接最终同轨同谱面行中的相邻可见主体 */
7
+ /** 显式 beam 连接同轨同谱面行中的相邻可见主体,忽略零时长小节线 */
8
8
  export declare function validateExplicitBeamAttachments(result: LoweringResult): void;
9
9
  /** 自动分组在 loweringAugment 当下读取已经注册的显式 beam 端点 */
10
10
  export declare function collectExplicitBeamEndpoints(attachments: readonly LoweringAttachment[]): Set<TemporalNodeBase>;
@@ -1,17 +1,19 @@
1
- import { isVisualTemporalNode, } from "../../lowering/types.js";
1
+ import { ANCHOR_KEY, isVisualTemporalNode, } from "../temporal.js";
2
2
  import { ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { claimDivLine, divLinePortName, divLineRuns, divLineWidth, } from "../div/index.js";
4
4
  /** 显式和自动分组共享同一个几何对象 */
5
5
  export function createBeamLayoutAttachment(endPoints, explicit, sourceSpan) {
6
6
  return new BeamLayoutAttachment(endPoints, explicit, sourceSpan);
7
7
  }
8
- /** 显式 beam 必须严格连接最终同轨同谱面行中的相邻可见主体 */
8
+ /** 显式 beam 连接同轨同谱面行中的相邻可见主体,忽略零时长小节线 */
9
9
  export function validateExplicitBeamAttachments(result) {
10
10
  const tracks = new Map();
11
11
  for (const column of result.columns) {
12
12
  for (const node of column) {
13
13
  if (!isVisualTemporalNode(node))
14
14
  continue;
15
+ if (node.mergeKey === ANCHOR_KEY && node.T.isZero())
16
+ continue;
15
17
  let lineTracks = tracks.get(node.layoutLine);
16
18
  if (!lineTracks) {
17
19
  lineTracks = new Map();
@@ -50,7 +52,7 @@ export function validateExplicitBeamAttachments(result) {
50
52
  && secondPosition.index === firstPosition.index + 1;
51
53
  if (adjacent)
52
54
  continue;
53
- throw new ErrorDiagnostic("E_NON_ADJACENT_BEAM", "@beam 的端点必须按时间顺序连接同一轨道、同一谱面行中的相邻可见元素", attachment.sourceSpan);
55
+ throw new ErrorDiagnostic("E_NON_ADJACENT_BEAM", "@beam 的端点必须按时间顺序连接同一轨道、同一谱面行中的相邻可见元素(可跨小节线)", attachment.sourceSpan);
54
56
  }
55
57
  }
56
58
  }
@@ -1,5 +1,5 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { isVisualTemporalNode } from "../../lowering/types.js";
2
+ import { isVisualTemporalNode } from "../temporal.js";
3
3
  import { ErrorDiagnostic } from "../../diagnostic.js";
4
4
  import { unionLayoutBoxes } from "../../layout/engine.js";
5
5
  import { layoutHorizontalRegion } from "../../layout/model.js";
@@ -8,15 +8,20 @@ class BoxFunction extends ASTFunctionNode {
8
8
  static def = {
9
9
  name: ["box"],
10
10
  description: "给目标结构或对象加外框",
11
- example: `@box(content, padding=0.2em, stroke=0.08em, width=-1px)`,
11
+ details: `\
12
+ ~~~jpfun
13
+ @box({1 2 3}, padding=0.2em, stroke=0.08em)
14
+ ~~~`,
12
15
  allowExtraArgs: false,
13
16
  args: [
14
17
  {
15
18
  type: "content",
19
+ description: "需要加框的内容或结构",
16
20
  default: null,
17
21
  },
18
22
  {
19
23
  name: "padding",
24
+ description: "内容到框线的内边距",
20
25
  type: "length",
21
26
  default: {
22
27
  value: 0,
@@ -25,6 +30,7 @@ class BoxFunction extends ASTFunctionNode {
25
30
  },
26
31
  {
27
32
  name: "stroke",
33
+ description: "框线宽度",
28
34
  type: "length",
29
35
  default: {
30
36
  value: 1,
@@ -33,6 +39,7 @@ class BoxFunction extends ASTFunctionNode {
33
39
  },
34
40
  {
35
41
  name: "width",
42
+ description: "约束内部排版的宽度,非正值表示随内容自动确定",
36
43
  type: "length",
37
44
  default: {
38
45
  value: -1,
@@ -154,7 +161,7 @@ class BoxLayoutAttachment {
154
161
  throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CROSSING", "定宽 @box 可以嵌套,但不能部分交叉", this.owner.sourceSpan);
155
162
  }
156
163
  const regionX = X.subarray(start, end + 1);
157
- layoutHorizontalRegion(columns.slice(start, end + 1), rows, regionX, fixed.subarray(start, end), this.owner.width, options, true);
164
+ layoutHorizontalRegion(columns.slice(start, end + 1), rows, regionX, fixed.subarray(start, end), this.owner.width, options, 0);
158
165
  const actualWidth = regionX[regionX.length - 1] - regionX[0] + leftInset + rightInset;
159
166
  if (Math.abs(actualWidth - this.owner.width) > 1e-6) {
160
167
  throw new ErrorDiagnostic("E_BOX_CONSTRAINT_CONFLICT", "多个 @box 为相同内容指定了不同宽度", this.owner.sourceSpan);
@@ -1,21 +1,25 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { skipSpaces, skipSpacesBack } from "../../parser/parserContext.js";
3
- import { TemporalNodeBase } from "../../lowering/types.js";
3
+ import { TemporalNodeBase } from "../temporal.js";
4
4
  import { Diagnostic, WarningDiagnostic } from "../../diagnostic.js";
5
5
  import { Fraction } from "../../fraction.js";
6
6
  class BrFunction extends ASTFunctionNode {
7
7
  static def = {
8
8
  name: ["br"],
9
9
  description: "换行",
10
- example: `@br(offset=1): 在后续事件前偏移指定行数
11
- @br() === @br(1)
12
- @br(2): 空出一行后继续
13
- 语法糖: [仅限顶层]至少连续两个换行
14
- `,
10
+ details: `\
11
+ ~~~jpfun
12
+ 1 2 @br() 3 4
13
+ ~~~
14
+
15
+ \`@br()\` 换到下一行,\`@br(2)\` 空出一行再继续
16
+
17
+ **简写**:仅在顶层,连续至少两个换行表示换行`,
15
18
  allowExtraArgs: false,
16
19
  args: [
17
20
  {
18
21
  name: "offset",
22
+ description: "后续内容偏移的谱面行数,2 表示空出一行再继续",
19
23
  type: "number",
20
24
  default: 1,
21
25
  },
@@ -1,13 +1,15 @@
1
1
  import { ASTFunctionNode } from "../ASTtypes.js";
2
- import { DEFAULT_KEY, TemporalNodeBase } from "../../lowering/types.js";
2
+ import { DEFAULT_KEY, TemporalNodeBase } from "../temporal.js";
3
3
  import { WarningDiagnostic } from "../../diagnostic.js";
4
4
  class DashFunction extends ASTFunctionNode {
5
5
  static def = {
6
6
  name: ["dash", "-"],
7
7
  description: "增时线",
8
- example: `@dash() 创建一根增时线
9
- 语法糖: 一个 '-' 代表一个 @dash()
10
- `,
8
+ details: `\
9
+ ~~~jpfun
10
+ 1 @dash()
11
+ ~~~
12
+ 无参数。添加一根增时线,延续前方音符的发声;也可简写为 \`1 -\`。每根线的基础时值为一个四分音符,可用减时线或附点调整。`,
11
13
  allowExtraArgs: false,
12
14
  args: []
13
15
  };
@@ -28,7 +30,7 @@ class DashFunction extends ASTFunctionNode {
28
30
  size;
29
31
  constructor(span, _args, ctx, parent = null) {
30
32
  super(span, parent);
31
- this.size = ctx.fontSize;
33
+ this.size = ctx.variables.fontsize;
32
34
  }
33
35
  loweringEnter() {
34
36
  return [new DashTemporalNode(this)];
@@ -37,6 +39,7 @@ class DashFunction extends ASTFunctionNode {
37
39
  }
38
40
  export const DashNode = DashFunction;
39
41
  class DashTemporalNode extends TemporalNodeBase {
42
+ lineWidth = 0;
40
43
  lineY = 0;
41
44
  constructor(ast) {
42
45
  super();
@@ -50,12 +53,12 @@ class DashTemporalNode extends TemporalNodeBase {
50
53
  }
51
54
  prepareLayout() {
52
55
  const size = this.ast.size;
53
- const width = size * 0.5;
56
+ this.lineWidth = size * 0.42;
54
57
  // dash 与数字音符共享视觉中心和完整字号高度
55
58
  // 线本身位于数字视觉中心,不使用极小的 glyph 高度作为轨道高度
56
- this.box.w = width;
59
+ this.box.w = this.lineWidth;
57
60
  this.box.h = size;
58
- this.box.anchor = width / 2;
61
+ this.box.anchor = this.lineWidth / 2;
59
62
  this.lineY = size * 0.5;
60
63
  this.box.visualAxis = this.lineY;
61
64
  }
@@ -88,6 +91,6 @@ class DashTemporalNode extends TemporalNodeBase {
88
91
  });
89
92
  }
90
93
  paint(painter) {
91
- painter.drawLine(this.box.x, this.box.y + this.lineY, this.box.x + this.box.w, this.box.y + this.lineY, { stroke: "#000", strokeWidth: Math.max(1, this.ast.size * 0.1) });
94
+ painter.drawLine(this.box.x, this.box.y + this.lineY, this.box.x + this.lineWidth, this.box.y + this.lineY, { stroke: "#000", strokeWidth: Math.max(1, this.ast.size * 0.1) });
92
95
  }
93
96
  }
@@ -1,10 +1,12 @@
1
1
  import { AdjustNode } from "./adjust/index.js";
2
+ import { ArpeggioNode } from "./arpeggio/index.js";
2
3
  import { BarNode } from "./bar/index.js";
3
4
  import { BeamNode } from "./beam/index.js";
4
5
  import { BoxNode } from "./box/index.js";
5
6
  import { DashNode } from "./dash/index.js";
6
7
  import { DivNode } from "./div/index.js";
7
8
  import { DotNode } from "./dot/index.js";
9
+ import { DynNode } from "./dyn/index.js";
8
10
  import { GraceNode } from "./grace/index.js";
9
11
  import { HeadNode } from "./head/index.js";
10
12
  import { KeyNode } from "./key/index.js";
@@ -13,6 +15,7 @@ import { BrNode } from "./br/index.js";
13
15
  import { NoteNode } from "./note/index.js";
14
16
  import { UpNode, DownNode } from "./up/index.js";
15
17
  import { PageNode } from "./page/index.js";
18
+ import { ProgramNode } from "./program/index.js";
16
19
  import { StackNode } from "./stack/index.js";
17
20
  import { SymbolNode } from "./symbol/index.js";
18
21
  import { SetNode } from "./set/index.js";
@@ -30,10 +33,11 @@ export const defaultFunctions = [
30
33
  PageNode, HeadNode, // 文档页面与谱头
31
34
  StackNode, // 时间同步
32
35
  UpNode, DownNode,
36
+ ArpeggioNode,
33
37
  GraceNode, // 倚音
34
- TieNode, BeamNode,
38
+ TieNode, BeamNode, DynNode,
35
39
  BoxNode, VoltaNode,
36
40
  AdjustNode, // 手工微调
37
- SetNode, KeyNode, MeterNode, TempoNode, // 设置
41
+ SetNode, KeyNode, MeterNode, TempoNode, ProgramNode, // 设置
38
42
  TextNode, SymbolNode,
39
43
  ];
@@ -22,18 +22,21 @@ declare class DivFunction extends ASTFunctionNode {
22
22
  static def: {
23
23
  name: string[];
24
24
  description: string;
25
- example: string;
25
+ details: string;
26
26
  allowExtraArgs: boolean;
27
27
  args: ({
28
28
  type: "content";
29
+ description: string;
29
30
  default: null;
30
31
  name?: undefined;
31
32
  } | {
32
33
  name: string;
34
+ description: string;
33
35
  type: "number";
34
36
  default: number;
35
37
  } | {
36
38
  name: string;
39
+ description: string;
37
40
  type: "boolean";
38
41
  default: boolean;
39
42
  })[];
@@ -48,26 +48,27 @@ class DivFunction extends ASTFunctionNode {
48
48
  static def = {
49
49
  name: [DIV_FUNC_NAME, "/"],
50
50
  description: "减时线",
51
- example: `@div(C1, 2): C1下方创建2根减时线
52
- @/({C1 C2 @dash()}, 3): C1、C2和增时线下方创建3根减时线并连接
53
- 语法糖:在音符后加斜杠'/',可以多个
54
- @div(C1, 2) === C1//
55
- @div(C1 C2/ @dash(), 2) === {C1 C2/ @dash()}//
56
- 设置命名参数前缀: div
57
- `,
51
+ details: `\
52
+ ~~~jpfun
53
+ @div({1 2 -}, n=2, autobeam=true)
54
+ ~~~
55
+ **简写**:在目标后加 \`/\`,可重复或作用于内容块。\`@div(1, 2)\` 等价于 \`1//\`,\`@div({1 2/ -}, 2)\` 等价于 \`{1 2/ -}//\`。默认参数使用 \`div.n\`、\`div.autobeam\` 设置。`,
58
56
  allowExtraArgs: false,
59
57
  args: [
60
58
  {
61
59
  type: "content",
60
+ description: "要减时的内容,可包含多个音符或增时线",
62
61
  default: null,
63
62
  },
64
63
  {
65
64
  name: "n",
65
+ description: "减时层数,每级将时值减半,取非负整数",
66
66
  type: "number",
67
67
  default: 1,
68
68
  },
69
69
  {
70
70
  name: "autobeam",
71
+ description: "是否参与不同 div 之间的自动连线,同一 div 内部的连线不受影响",
71
72
  type: "boolean",
72
73
  default: true,
73
74
  },
@@ -10,20 +10,23 @@ class DotFunction extends ASTFunctionNode {
10
10
  static def = {
11
11
  name: [DOT_FUNC_NAME, "."],
12
12
  description: "附点",
13
- example: `@dot(C1, 2): C1右侧创建2个点 仅接收一个可接收元素
14
- 语法糖:在音符后加斜杠'.',可以多个
15
- @dot(C1, 2) === C1..
16
- @dot(C1 C2/ @dash(), 2) 报错 因为点只能接收一个元素
17
- 设置命名参数前缀: dot
18
- `,
13
+ details: `\
14
+ ~~~jpfun
15
+ @dot(1, n=2)
16
+ ~~~
17
+ **简写**:目标后加 \`.\`,可重复。\`@dot(1, 2)\` 等价于 \`1..\`;\`{1^2}.\` 等价于 \`{1.^2}\`。
18
+
19
+ \`@dot({1 2/ -}, 2)\` 包含多个时间元素,会报错;需要分别为各元素添加附点。`,
19
20
  allowExtraArgs: false,
20
21
  args: [
21
22
  {
22
23
  type: "content",
24
+ description: "单个可见时间元素,如音符、增时线或折叠和弦",
23
25
  default: null,
24
26
  },
25
27
  {
26
28
  name: "n",
29
+ description: "附点数,第一点增加原时值的一半,后续各点增加量再减半",
27
30
  type: "number",
28
31
  default: 1,
29
32
  },
@@ -107,8 +110,10 @@ class DotFunction extends ASTFunctionNode {
107
110
  };
108
111
  loweringEnter(ctx) {
109
112
  const count = this.n;
110
- ctx.beginLoweringGroup(this, {
113
+ const group = {
114
+ temporalCount: 0,
111
115
  onTemporal(node) {
116
+ group.temporalCount++;
112
117
  if (count === 0)
113
118
  return;
114
119
  const addon = node.addon ??= {};
@@ -119,11 +124,15 @@ class DotFunction extends ASTFunctionNode {
119
124
  node.T.mul(totalNumerator * currentDenominator, totalDenominator * currentNumerator);
120
125
  addon[DOT_ADDON_KEY] = total;
121
126
  },
122
- });
127
+ };
128
+ ctx.beginLoweringGroup(this, group);
123
129
  return [];
124
130
  }
125
131
  loweringExit(ctx) {
126
- ctx.endLoweringGroup(this);
132
+ const group = ctx.endLoweringGroup(this);
133
+ if (group.temporalCount !== 1) {
134
+ throw new ErrorDiagnostic("E_DOT_INVALID_CONTENT", `函数 @dot 只能接收 1个元素,但找到了 ${group.temporalCount} 个`, this.sourceSpan);
135
+ }
127
136
  return [];
128
137
  }
129
138
  timeFlowModel() {
@@ -138,10 +147,6 @@ class DotFunction extends ASTFunctionNode {
138
147
  constructor(sourceSpan, args, ctx, parent = null) {
139
148
  super(sourceSpan, parent);
140
149
  [this.content, this.n] = this.getArgValue(args, ctx);
141
- const contentJudge = DotFunction.leafNum(this.content);
142
- if (contentJudge !== 1) {
143
- throw new ErrorDiagnostic("E_DOT_INVALID_CONTENT", `函数 @dot 只能接收 1个元素,但找到了 ${contentJudge} 个`, sourceSpan);
144
- }
145
150
  this.content.parent = this;
146
151
  const n = Math.max(0, Math.trunc(this.n));
147
152
  if (n !== this.n) {
@@ -152,17 +157,5 @@ class DotFunction extends ASTFunctionNode {
152
157
  toString(source) {
153
158
  return `@dot(${this.content.toString(source)}, ${this.n})`;
154
159
  }
155
- // 判断叶节点数量
156
- static leafNum(node) {
157
- let count = 0;
158
- const chs = node.children;
159
- if (chs) { // 不是叶节点
160
- for (const child of chs)
161
- count += DotFunction.leafNum(child);
162
- }
163
- else
164
- count++;
165
- return count;
166
- }
167
160
  }
168
161
  export const DotNode = DotFunction;
@@ -0,0 +1,2 @@
1
+ import { type ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const DynNode: ASTFunctionClass;