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
@@ -1,58 +1,79 @@
1
- import { ASTFunctionNode } from "../ASTtypes.js";
1
+ import { ASTTextNode, ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { parseNoteName } from "./noteNameFSM.js";
4
- import { JIANPU_NUMBER_FONT } from "../../render/text.js";
5
4
  import { paintAccidental, placeAccidentals } from "./accidentals.js";
5
+ const JE_OCTAVE_OFFSET = "note.jeOctaveOffset";
6
6
  class NoteFunction extends ASTFunctionNode {
7
7
  static def = {
8
8
  name: ["note", "n"],
9
9
  description: "创建音符",
10
- example: `@note(name, acc, octave, color)
11
- 参数说明:
12
- - name: [必填]音符名,可以是大写字母或者数字,不允许小写字母(会和降号冲突)。此参数写法有语法糖,见下。
13
- 其中 0 是休止符,8 是隐形占位,9 是只打拍不发音的节拍记号(显示为 X),这三者都不显示升降号和八度点。
14
- - acc: [可选]的额外升降号字符串,例如 "##" 表示再升两个半音,"b" 表示再降一个半音。
15
- - octave: [可选]八度,类型为数字。如果 name 是字母,则此项代表绝对八度;如果是数字,则此项代表相对八度。
16
-
17
- 语法糖:[音名][升降号][八度] 音名和升降号可以交换
18
- 例:A3# === @note(A3#) === @note(A, #, 3)
19
- 也支持 A99##bn 的写法。可以设置 note.octave 改变默认的绝对八度
20
-
21
- 支持 数字音名, 此时 octave 为相对八度
22
- 支持使用数字音名时使用相对八度,如 "1,," 代表在当前基准音(由上下文属性"1=?"决定)的基础上降低两倍八度,"1'" 代表在当前基准音的基础上提高一个八度
23
- 例:1#' === @note(1#') === @note(1, #, 1)。此时支持升降号写音名前面,如 #1'
24
- `,
10
+ details: `\
11
+ ~~~jpfun
12
+ @note(A, acc=#, octave=3, color=#c33)
13
+ ~~~
14
+ **简写**:数字音名按“升降号、音名、相对八度”书写;字母音名按“音名、升降号、绝对八度”书写。\`1,,\` 比当前调性基准低两个八度,\`1'\` 高一个八度。
15
+ - \`A#3\` 等价于 \`@note(A#3)\` 或 \`@note(A, #, 3)\`
16
+ - \`#1'\` 等价于 \`@note("#1'")\` 或 \`@note(1, #, 1)\`
17
+ - 支持组合升降号,如 \`A##bn99\`;无歧义时也接受后置写法,如 \`A3#\`、\`6#,,\`
18
+ - 升降号后紧跟数字时归入下一音符:\`6#3\` 解析为 \`6\` 和 \`#3\``,
25
19
  allowExtraArgs: false,
26
20
  args: [
27
21
  {
28
22
  name: "name",
23
+ description: "数字音名或大写字母音名,可自带升降号与八度;`0` / `Z` 为休止符,`8` 为隐形占位,`9` / `X` 为节拍记号,后三类不显示升降号和八度点",
29
24
  type: "string",
30
25
  default: null,
31
26
  },
32
27
  {
33
28
  name: "acc",
29
+ description: "额外升降号,`##` 再升两个半音,`b` 再降一个半音",
34
30
  type: "string",
35
31
  default: "",
36
32
  },
37
33
  {
38
34
  name: "octave",
35
+ description: "字母音名使用绝对八度;数字音名使用相对八度,省略时为 0",
39
36
  type: "number",
40
37
  default: 4, // 如果是数字则默认值为0 需要代码中区分
41
38
  },
42
39
  {
43
40
  name: "color",
41
+ description: "音符颜色,使用 CSS 颜色值",
44
42
  type: "string",
45
43
  default: "#000",
46
44
  }
47
45
  ]
48
46
  };
49
47
  static deSugarAtom(source, start, end) {
48
+ // JE 谱兼容
49
+ let jeOffset = 0;
50
+ switch (source[start]) {
51
+ case "(":
52
+ case "]":
53
+ jeOffset = -1;
54
+ break;
55
+ case "[":
56
+ case ")":
57
+ jeOffset = 1;
58
+ break;
59
+ }
60
+ if (jeOffset)
61
+ return {
62
+ next: start + 1,
63
+ node: {
64
+ kind: "sugar",
65
+ data: { jeOffset },
66
+ span: { start, end: start + 1 },
67
+ syntaxKind: "punctuation",
68
+ },
69
+ };
70
+ // 解析音符名
50
71
  const parseResult = parseNoteName(source, start, end);
51
72
  if (parseResult instanceof Diagnostic)
52
73
  return null;
53
74
  const argMap = new Map();
54
75
  argMap.set("name", parseResult.name);
55
- if (parseResult.octave)
76
+ if (parseResult.octave !== null)
56
77
  argMap.set("octave", parseResult.octave);
57
78
  if (parseResult.acc)
58
79
  argMap.set("acc", parseResult.acc);
@@ -66,6 +87,16 @@ class NoteFunction extends ASTFunctionNode {
66
87
  };
67
88
  return { next: parseResult.next, node };
68
89
  }
90
+ static deSugarRelation(ctx, nodes, at) {
91
+ const node = nodes[at];
92
+ const jeOffset = node.data?.jeOffset;
93
+ if (typeof jeOffset !== "number")
94
+ return null;
95
+ ctx.variables[JE_OCTAVE_OFFSET] = (ctx.variables[JE_OCTAVE_OFFSET] ?? 0) + jeOffset;
96
+ // JE 谱兼容:保留括号源码,并让关系语法糖像跳过普通文本一样跳过它。
97
+ ctx.nodes.push(new ASTTextNode(node.span));
98
+ return at + 1;
99
+ }
69
100
  labelable() { return this; }
70
101
  loweringEnter() {
71
102
  return [new NoteTemporalNode(this)];
@@ -76,10 +107,14 @@ class NoteFunction extends ASTFunctionNode {
76
107
  acc;
77
108
  color;
78
109
  size;
110
+ font;
111
+ // JE 八度偏移量
112
+ jeOctaveOffset;
79
113
  constructor(sourceSpan, args, ctx, parent = null) {
80
114
  super(sourceSpan, parent);
81
115
  [this.name, this.acc, this.octave, this.color] = this.getArgValue(args, ctx);
82
- this.size = ctx.fontSize;
116
+ this.font = ctx.variables.numberfont;
117
+ this.size = ctx.variables.fontsize;
83
118
  // 创建时就固化参数值
84
119
  // 校验 note name
85
120
  const parseResult = parseNoteName(this.name);
@@ -87,7 +122,9 @@ class NoteFunction extends ASTFunctionNode {
87
122
  parseResult.span = sourceSpan; // 定位到整个函数调用
88
123
  throw parseResult;
89
124
  }
90
- this.name = parseResult.name;
125
+ if (parseResult.next !== this.name.length)
126
+ throw new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 无法解析尾部 "${this.name.slice(parseResult.next)}"`, sourceSpan);
127
+ this.name = parseResult.name === "X" ? "9" : parseResult.name === "Z" ? "0" : parseResult.name;
91
128
  // 校验octave
92
129
  const inputOctave = args.get("octave") ?? args.get(2);
93
130
  if (inputOctave !== void 0) {
@@ -95,10 +132,13 @@ class NoteFunction extends ASTFunctionNode {
95
132
  throw new ErrorDiagnostic("E_NOTE_OCTAVE_CONFLICT", `函数 @note 的参数 "octave" 与音符名称中的八度信息冲突: ${inputOctave} != ${parseResult.octave}`, sourceSpan);
96
133
  }
97
134
  else {
98
- // 没有传入octave 此时this.octave是字母模式下的默认值 仅在数字模式下才需要被覆盖
99
- if (!parseResult.absOctave)
100
- this.octave = parseResult.octave ?? 0;
135
+ // 没有单独传入 octave 时,优先采用音名中的八度;数字音名缺省为相对 0。
136
+ if (parseResult.octave !== null)
137
+ this.octave = parseResult.octave;
138
+ else if (!parseResult.absOctave)
139
+ this.octave = 0;
101
140
  }
141
+ this.jeOctaveOffset = ctx.variables[JE_OCTAVE_OFFSET] ?? 0;
102
142
  // 补充acc
103
143
  if (parseResult.acc !== null)
104
144
  this.acc = parseResult.acc + this.acc;
@@ -108,9 +148,9 @@ class NoteFunction extends ASTFunctionNode {
108
148
  }
109
149
  }
110
150
  export const NoteNode = NoteFunction;
111
- import { DEFAULT_KEY, DEFAULT_VELOCITY, TemporalNodeBase } from "../../lowering/types.js";
151
+ import { DEFAULT_KEY, DEFAULT_VELOCITY, TemporalNodeBase } from "../temporal.js";
112
152
  import { createDiatonicTranspose, resolveLetterNameToJianpu, resolveNoteMidi, } from "../../parser/parse-utils/note-utils.js";
113
- // 8 是隐形占位,9 是只打拍不发音的节拍记号,空串表示不绘制
153
+ // 8 是隐形占位,9 是固定打击音的节拍记号,空串表示不绘制
114
154
  const NOTE_GLYPH = { "8": "", "9": "X" };
115
155
  class NoteTemporalNode extends TemporalNodeBase {
116
156
  // 时间固化后的参数
@@ -135,10 +175,10 @@ class NoteTemporalNode extends TemporalNodeBase {
135
175
  this.mergeKey = DEFAULT_KEY;
136
176
  this.name = ast.name;
137
177
  this.acc = ast.acc;
138
- this.octave = ast.octave;
178
+ this.octave = ast.octave + ast.jeOctaveOffset;
139
179
  this.numberStyle = {
140
180
  fontSize: ast.size,
141
- fontFamily: JIANPU_NUMBER_FONT,
181
+ fontFamily: ast.font,
142
182
  fontWeight: 400,
143
183
  textAlign: "center",
144
184
  fill: ast.color,
@@ -227,7 +267,7 @@ class NoteTemporalNode extends TemporalNodeBase {
227
267
  }
228
268
  onTimeState(state) {
229
269
  const keySignature = state.keySignature;
230
- this.playbackState = { bpm: state.bpm, velocity: state.velocity };
270
+ this.playbackState = { bpm: state.bpm, velocity: state.velocity, program: state.program };
231
271
  this.resolvedMidi = resolveNoteMidi(this.name, this.acc, this.octave, keySignature);
232
272
  this.transposeDiatonic = createDiatonicTranspose(this.name, this.acc, this.octave, keySignature);
233
273
  // 数字音名本身就是简谱显示形式,直接保留原始数字和相对八度
@@ -248,16 +288,18 @@ class NoteTemporalNode extends TemporalNodeBase {
248
288
  }
249
289
  }
250
290
  emitPlayback(emitter) {
251
- if (this.resolvedMidi === null || emitter.end.compare(emitter.start) <= 0)
291
+ const percussion = this.ast.name === "9";
292
+ const midi = percussion ? 37 : this.resolvedMidi;
293
+ if (midi === null || emitter.end.compare(emitter.start) <= 0)
252
294
  return;
253
295
  const noteId = emitter.nextNoteId();
254
296
  emitter.emit({
255
297
  kind: "note-on",
256
298
  at: emitter.start,
257
299
  noteId,
258
- midi: this.resolvedMidi,
300
+ midi,
259
301
  velocity: this.playbackState?.velocity ?? DEFAULT_VELOCITY,
260
- transpose: this.transposeDiatonic,
302
+ ...(percussion ? { percussion: true } : { transpose: this.transposeDiatonic }),
261
303
  });
262
304
  emitter.emit({ kind: "note-off", at: emitter.end, noteId });
263
305
  }
@@ -1,5 +1,5 @@
1
1
  import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
2
- const LETTER_NOTE_START_RE = /^[A-G]/;
2
+ const LETTER_NOTE_START_RE = /^[A-GXZ]/;
3
3
  const NUM_NOTE_START_RE = /^[0-9]/;
4
4
  const SHARP_RE = /^[#bn]+/;
5
5
  const ABS_OCTAVE_RE = /^[+-]?\d+/;
@@ -23,7 +23,7 @@ export function parseNoteName(str, start = 0, end = str.length) {
23
23
  else if (NUM_NOTE_START_RE.test(ch)) {
24
24
  name = ch;
25
25
  absOctave = false;
26
- state = 4;
26
+ state = 3;
27
27
  }
28
28
  else {
29
29
  const match = str.slice(pos, end).match(SHARP_RE);
@@ -31,26 +31,26 @@ export function parseNoteName(str, start = 0, end = str.length) {
31
31
  acc = match[0];
32
32
  pos += acc.length - 1;
33
33
  absOctave = false;
34
- state = 7;
34
+ state = 2;
35
35
  }
36
36
  else
37
- return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 首字符 "${ch}" 不合法,期望 A-G (a-g) 或 0-9 开头,或在开头直接使用升降号(#/b/n)`, { start: pos, end: pos + 1 });
37
+ return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 首字符 "${ch}" 不合法,期望 A-G (a-g)、0-9、X 或 Z 开头,或在开头直接使用升降号(#/b/n)`, { start: pos, end: pos + 1 });
38
38
  }
39
39
  break;
40
- case 1:
40
+ case 1: // [字母模式] 已获得音名
41
41
  const subs = str.slice(pos, end);
42
42
  match = subs.match(ABS_OCTAVE_RE);
43
43
  if (match) {
44
44
  octave = parseInt(match[0], 10);
45
45
  pos += match[0].length - 1;
46
- state = 2;
46
+ state = 4;
47
47
  break;
48
48
  }
49
49
  match = subs.match(SHARP_RE);
50
50
  if (match) {
51
51
  acc = match[0];
52
52
  pos += acc.length - 1;
53
- state = 3;
53
+ state = 5;
54
54
  break;
55
55
  }
56
56
  return {
@@ -60,87 +60,82 @@ export function parseNoteName(str, start = 0, end = str.length) {
60
60
  octave: null,
61
61
  absOctave,
62
62
  };
63
- case 2:
64
- match = str.slice(pos, end).match(SHARP_RE);
63
+ case 2: // [数字模式] 已获得升降号
64
+ if (NUM_NOTE_START_RE.test(ch)) {
65
+ name = ch;
66
+ state = 6;
67
+ break;
68
+ }
69
+ else
70
+ return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但发现字符 "${ch}"`, { start: pos, end: pos + 1 });
71
+ case 3: // [数字模式] 已获得音名
72
+ match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
65
73
  if (match) {
66
- acc = match[0];
74
+ const octave_str = match[0];
75
+ octave = relativeOctaveFromString(octave_str);
67
76
  return {
68
77
  name: name,
69
- next: pos + acc.length,
78
+ next: pos + octave_str.length,
70
79
  acc,
71
- octave,
80
+ octave: octave,
72
81
  absOctave,
73
82
  };
74
83
  }
84
+ match = str.slice(pos, end).match(SHARP_RE);
85
+ if (match && !NUM_NOTE_START_RE.test(str[pos + match[0].length] ?? "")) {
86
+ const accidental = match[0];
87
+ acc = accidental;
88
+ pos += accidental.length - 1;
89
+ state = 6;
90
+ break;
91
+ }
75
92
  return {
76
93
  name: name,
77
94
  next: pos,
78
- acc: null,
95
+ acc,
79
96
  octave: octave,
80
97
  absOctave,
81
98
  };
82
- case 3:
83
- match = str.slice(pos, end).match(ABS_OCTAVE_RE);
84
- if (match) {
85
- octave = parseInt(match[0], 10);
99
+ case 4: // [字母模式] 已获得音名和八度
100
+ match = str.slice(pos, end).match(SHARP_RE);
101
+ if (match && !NUM_NOTE_START_RE.test(str[pos + match[0].length] ?? "")) {
102
+ const accidental = match[0];
103
+ acc = accidental;
86
104
  return {
87
105
  name: name,
88
- next: pos + match[0].length,
106
+ next: pos + accidental.length,
89
107
  acc,
90
108
  octave,
91
109
  absOctave,
92
110
  };
93
111
  }
94
- return {
95
- name: name,
96
- next: pos,
97
- acc: acc,
98
- octave: null,
99
- absOctave,
100
- };
101
- case 4:
102
- match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
103
- if (match) {
104
- const octave_str = match[0];
105
- octave = relativeOctaveFromString(octave_str);
106
- pos += octave_str.length - 1;
107
- state = 5;
108
- break;
109
- }
110
- match = str.slice(pos, end).match(SHARP_RE);
111
- if (match) {
112
- acc = match[0];
113
- pos += acc.length - 1;
114
- state = 6;
115
- break;
116
- }
117
112
  return {
118
113
  name: name,
119
114
  next: pos,
120
115
  acc: null,
121
- octave: octave,
116
+ octave,
122
117
  absOctave,
123
118
  };
124
- case 5:
125
- match = str.slice(pos, end).match(SHARP_RE);
119
+ case 5: // [字母模式] 已获得音名和半音
120
+ match = str.slice(pos, end).match(ABS_OCTAVE_RE);
126
121
  if (match) {
127
- acc = match[0];
122
+ octave = parseInt(match[0], 10);
128
123
  return {
129
124
  name: name,
130
- next: pos + acc.length,
125
+ next: pos + match[0].length,
131
126
  acc,
132
- octave: octave,
127
+ octave,
133
128
  absOctave,
134
129
  };
135
130
  }
136
131
  return {
137
132
  name: name,
138
133
  next: pos,
139
- acc: null,
140
- octave: octave,
134
+ acc: acc,
135
+ octave: null,
141
136
  absOctave,
142
137
  };
143
- case 6:
138
+ case 6: // [数字模式] 已获得升降号和音名
144
139
  match = str.slice(pos, end).match(RELATIVE_OCTAVE_RE);
145
140
  if (match) {
146
141
  const octave_str = match[0];
@@ -148,32 +143,26 @@ export function parseNoteName(str, start = 0, end = str.length) {
148
143
  return {
149
144
  name: name,
150
145
  next: pos + octave_str.length,
151
- acc: acc,
152
- octave: octave,
146
+ acc,
147
+ octave,
153
148
  absOctave,
154
149
  };
155
150
  }
156
151
  return {
157
152
  name: name,
158
153
  next: pos,
159
- acc: acc,
160
- octave: octave,
154
+ acc,
155
+ octave,
161
156
  absOctave,
162
157
  };
163
- case 7:
164
- if (NUM_NOTE_START_RE.test(ch)) {
165
- name = ch;
166
- state = 6;
167
- break;
168
- }
169
- else
170
- return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但发现字符 "${ch}"`, { start: pos, end: pos + 1 });
171
158
  default:
172
159
  return Diagnostic.error.Bug("音符名称解析状态机进入了未定义的状态", { start: pos, end });
173
160
  }
174
161
  }
162
+ if (name === null)
163
+ return new ErrorDiagnostic("E_WRONG_NOTE_NAME", `函数 @note 的参数 [0]:"name" 格式错误: 在升降号开头时,应该接数字音名,但输入已结束`, { start, end });
175
164
  return {
176
- name: name,
165
+ name,
177
166
  next: end,
178
167
  acc,
179
168
  octave,
@@ -7,21 +7,25 @@ export declare class PageFunction extends ASTFunctionNode {
7
7
  static def: {
8
8
  name: string[];
9
9
  description: string;
10
- example: string;
10
+ details: string;
11
11
  allowExtraArgs: boolean;
12
12
  args: ({
13
13
  name: string;
14
+ description: string;
14
15
  type: "length";
15
16
  default: LengthValue;
16
17
  } | {
17
18
  name: string;
19
+ description: string;
18
20
  type: "string";
19
21
  default: string;
20
22
  })[];
21
23
  };
22
24
  readonly config: PageConfig | null;
23
25
  private readonly numbering;
26
+ readonly font: string;
24
27
  constructor(sourceSpan: SourceSpan, args: FunctionArgs, ctx: ParserContext, parent?: ASTNodeBase | null);
25
28
  loweringEnter(ctx: LoweringContext): never[];
29
+ toString(): string;
26
30
  }
27
31
  export declare const PageNode: ASTFunctionClass;
@@ -1,30 +1,35 @@
1
1
  import { DEFAULT_PAGE_CONFIG, normalizePageConfig, } from "../../layout/page.js";
2
2
  import { Diagnostic, WarningDiagnostic } from "../../diagnostic.js";
3
+ import { quote } from "../../parser/parse-utils/string-utils.js";
3
4
  import { ASTFunctionNode, } from "../ASTtypes.js";
4
5
  const px = (value) => ({ value, unit: "px" });
5
6
  /** 页码字号,同时也是放得下页码所需的最小下边距 */
6
7
  export const PAGE_NUMBER_FONT_SIZE = 16;
7
8
  const PAGE_NUMBER_STYLE = { fontSize: PAGE_NUMBER_FONT_SIZE, textAlign: "center", fill: "#606060" };
8
- /** 模式串里的 1 是计数符号,最后一个取总页数,其余取当前页 */
9
+ /** 单个 1 取当前页;多个 1 中最后一个取总页数,其余取当前页 */
9
10
  function formatPageNumber(pattern, current, total) {
10
11
  const last = pattern.lastIndexOf("1");
11
- return pattern.replace(/1/g, (_, index) => String(index === last ? total : current));
12
+ const hasTotal = pattern.indexOf("1") !== last;
13
+ return pattern.replace(/1/g, (_, index) => String(hasTotal && index === last ? total : current));
12
14
  }
13
15
  /** 浮在每页下边距带里的页码,不属于任何谱面行,因此不申报 Track 占用 */
14
16
  class PageNumberAttachment {
15
17
  sourceSpan;
16
18
  pattern;
17
19
  marginBottom;
20
+ style;
18
21
  layer = "foreground";
19
- constructor(sourceSpan, pattern, marginBottom) {
22
+ constructor(sourceSpan, pattern, marginBottom, style) {
20
23
  this.sourceSpan = sourceSpan;
21
24
  this.pattern = pattern;
22
25
  this.marginBottom = marginBottom;
26
+ this.style = style;
23
27
  }
24
28
  createGeometry(context) {
29
+ const style = this.style;
25
30
  const marks = context.pages.map((page, index) => {
26
31
  const text = formatPageNumber(this.pattern, index + 1, context.pages.length);
27
- const { w, h, baseline } = context.textMeasurer.measureText(text, PAGE_NUMBER_STYLE);
32
+ const { w, h, baseline } = context.textMeasurer.measureText(text, style);
28
33
  const center = page.y + page.h - this.marginBottom / 2;
29
34
  return { text, x: page.x + page.w / 2, y: center - h / 2 + baseline, w, h, baseline };
30
35
  });
@@ -32,7 +37,7 @@ class PageNumberAttachment {
32
37
  regions: marks.map(({ x, y, w, h, baseline }) => ({ x: x - w / 2, y: y - baseline, w, h })),
33
38
  paint(painter) {
34
39
  for (const mark of marks)
35
- painter.drawText(mark.text, mark.x, mark.y, PAGE_NUMBER_STYLE);
40
+ painter.drawText(mark.text, mark.x, mark.y, style);
36
41
  },
37
42
  };
38
43
  }
@@ -41,24 +46,34 @@ export class PageFunction extends ASTFunctionNode {
41
46
  static def = {
42
47
  name: ["page"],
43
48
  description: "设置文档页面尺寸、边距、最小谱面行间距和页码",
44
- example: "@page(width=794px, height=1123px, top=48px, bottom=48px, left=40px, right=40px, gap=1em, numbering=\"1/1\")",
49
+ details: `\
50
+ ~~~jpfun
51
+ @page(
52
+ width=794px, height=1123px, top=48px, bottom=48px,
53
+ left=40px, right=40px, gap=1em, numbering="1/1"
54
+ )
55
+ ~~~
56
+ 尺寸接受 \`px\` 或 \`em\`。页边距和行距须非负,左右边距之和须小于页宽;固定页高时,上下边距之和也须小于页高。`,
45
57
  allowExtraArgs: false,
46
58
  args: [
47
- { name: "width", type: "length", default: px(DEFAULT_PAGE_CONFIG.width) },
48
- { name: "height", type: "length", default: px(0) },
49
- { name: "top", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginTop) },
50
- { name: "bottom", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginBottom) },
51
- { name: "left", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginLeft) },
52
- { name: "right", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginRight) },
53
- { name: "gap", type: "length", default: { value: 1, unit: "em" } },
54
- // 模式串里的 1 是计数符号,最后一个取总页数
55
- { name: "numbering", type: "string", default: "" },
59
+ { name: "width", description: "页面宽度,须大于左右页边距之和", type: "length", default: px(DEFAULT_PAGE_CONFIG.width) },
60
+ { name: "height", description: "页面高度,0 表示不分页、高度随内容增长;宽度为 `794px` 时,A4 对应高度约为 `1123px`", type: "length", default: px(0) },
61
+ { name: "top", description: "页面上边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginTop) },
62
+ { name: "bottom", description: "页面下边距,启用页码时至少为 `16px`", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginBottom) },
63
+ { name: "left", description: "页面左边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginLeft) },
64
+ { name: "right", description: "页面右边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginRight) },
65
+ { name: "gap", description: "谱面行之间的最小间距", type: "length", default: { value: 1, unit: "em" } },
66
+ // 单个 1 取当前页;多个 1 中最后一个取总页数
67
+ { name: "numbering", description: "页码格式,空值隐藏;`1` 显示当前页,`1/1` 显示当前页/总页数", type: "string", default: "" },
68
+ { name: "font", description: "页码字体,空值沿用当前字体设置", type: "string", default: "" },
56
69
  ],
57
70
  };
58
71
  config;
59
72
  numbering = "";
73
+ font;
60
74
  constructor(sourceSpan, args, ctx, parent = null) {
61
75
  super(sourceSpan, parent);
76
+ this.font = ctx.variables.font;
62
77
  if (ctx.scopeDepth !== 0) {
63
78
  ctx.diagnostics.push(new WarningDiagnostic("W_PAGE_NOT_TOP_LEVEL", "@page 只能在文档顶层声明;当前声明已忽略", sourceSpan));
64
79
  this.config = null;
@@ -71,6 +86,7 @@ export class PageFunction extends ASTFunctionNode {
71
86
  }
72
87
  ctx.documentDeclarations["page"] = true;
73
88
  const values = this.getArgValue(args, ctx);
89
+ this.font = values.pop() || ctx.variables.font;
74
90
  const numbering = values.pop();
75
91
  const [width, height, top, bottom, left, right, gap] = values.map(value => ctx.length2px(value));
76
92
  if (!Number.isFinite(width) || width <= 0)
@@ -101,9 +117,15 @@ export class PageFunction extends ASTFunctionNode {
101
117
  return [];
102
118
  ctx.setPageConfig(this.config);
103
119
  if (this.numbering) {
104
- ctx.addAttachment(new PageNumberAttachment(this.sourceSpan, this.numbering, this.config.marginBottom));
120
+ ctx.addAttachment(new PageNumberAttachment(this.sourceSpan, this.numbering, this.config.marginBottom, { ...PAGE_NUMBER_STYLE, fontFamily: this.font }));
105
121
  }
106
122
  return [];
107
123
  }
124
+ toString() {
125
+ if (!this.config)
126
+ return "@page()";
127
+ const { width, height, marginTop, marginBottom, marginLeft, marginRight, lineGap } = this.config;
128
+ return `@page(width=${width}px, height=${Number.isFinite(height) ? height : 0}px, top=${marginTop}px, bottom=${marginBottom}px, left=${marginLeft}px, right=${marginRight}px, gap=${lineGap}px, numbering=${quote(this.numbering)}, font=${quote(this.font)})`;
129
+ }
108
130
  }
109
131
  export const PageNode = PageFunction;
@@ -0,0 +1,2 @@
1
+ import { type ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const ProgramNode: ASTFunctionClass;
@@ -0,0 +1,39 @@
1
+ import { ErrorDiagnostic } from "../../diagnostic.js";
2
+ import { TemporalNodeBase } from "../temporal.js";
3
+ import { ASTFunctionNode, } from "../ASTtypes.js";
4
+ class ProgramFunction extends ASTFunctionNode {
5
+ static def = {
6
+ name: ["program", "instrument"],
7
+ description: "设置当前音轨的 MIDI 音色",
8
+ details: `\
9
+ ~~~jpfun
10
+ @program(40) 1 2 3
11
+ ~~~
12
+ 切换为小提琴音色。设置将作用于当前音轨的后续音符。`,
13
+ allowExtraArgs: false,
14
+ args: [{
15
+ name: "program",
16
+ description: "从 0 开始编号的 MIDI 音色,须为 `0..127` 的整数",
17
+ type: "number",
18
+ default: null
19
+ }],
20
+ };
21
+ program;
22
+ constructor(span, args, ctx, parent = null) {
23
+ super(span, parent);
24
+ const [program] = this.getArgValue(args, ctx);
25
+ if (!Number.isSafeInteger(program) || program < 0 || program > 127) {
26
+ throw new ErrorDiagnostic("E_PROGRAM_INVALID", "@program 必须是 0..127 的整数", span);
27
+ }
28
+ this.program = program;
29
+ }
30
+ loweringEnter() {
31
+ const node = new TemporalNodeBase();
32
+ node.ast = this;
33
+ node.playbackState = { program: this.program };
34
+ node.onTimeState = (state) => { state.program = this.program; };
35
+ return [node];
36
+ }
37
+ toString() { return `@program(${this.program})`; }
38
+ }
39
+ export const ProgramNode = ProgramFunction;
@@ -1,16 +1,21 @@
1
1
  import { ASTFunctionNode, primaryName, resolveArgType } from "../ASTtypes.js";
2
2
  import { WarningDiagnostic } from "../../diagnostic.js";
3
- import { SYSTEM_VARIABLE_TYPES } from "../../parser/parserContext.js";
3
+ import { quote } from "../../parser/parse-utils/string-utils.js";
4
+ import { ParserContext } from "../../parser/parserContext.js";
4
5
  class SetFunction extends ASTFunctionNode {
5
6
  static def = {
6
7
  name: ["set"],
7
8
  description: "设置局部默认值",
8
- example: `@set(fontSize=20) 则当前块内默认字体变为20px
9
- 可以设置其他函数的默认值: {函数名}.{属性}=值,例:
10
- @set(note.octave=5, note.color=#f00)
9
+ details: `\
10
+ ~~~jpfun
11
+ @set(fontsize=20, strict=true)
12
+ { @set(note.octave=5, note.color=#f00) C D E }
13
+ ~~~
14
+ 接受命名参数,可一次设置多项:
15
+ - **系统选项**:如 \`fontsize=20\` 将字号设为 20px,\`strict=true\` 开启严格模式
16
+ - **函数默认参数**:按 \`函数主名.参数名=值\` 书写,例如 \`note.octave=5\`、\`note.color=#f00\`;显式传参优先
11
17
 
12
- 注意:@set 作用域仅在其所处 '{}' 内、其出现之后。离开当前作用域会恢复为之前的设置。此设置和时序无关,类似于局部变量。
13
- `,
18
+ 设置从当前位置起生效,限于当前作用域及其子作用域;离开内容块后恢复外层设置。它按源码作用域解析,与演奏时间无关。`,
14
19
  allowExtraArgs: true,
15
20
  args: []
16
21
  };
@@ -18,7 +23,7 @@ class SetFunction extends ASTFunctionNode {
18
23
  static resolveTarget(ctx, key, nameSpan) {
19
24
  const dot = key.lastIndexOf(".");
20
25
  if (dot < 0)
21
- return [key, SYSTEM_VARIABLE_TYPES[key] ?? "string"];
26
+ return [key, Object.hasOwn(ParserContext.systemVariables, key) ? ParserContext.systemVariables[key].type : "string"];
22
27
  const argName = key.slice(dot + 1);
23
28
  const def = ctx.functions.get(key.slice(0, dot))?.prototype.def;
24
29
  const type = def && resolveArgType(def, argName, -1);
@@ -57,7 +62,7 @@ class SetFunction extends ASTFunctionNode {
57
62
  }
58
63
  }
59
64
  toString() {
60
- return `@set(${Array.from(this.args.entries()).map(([k, v]) => `${k}=${v}`).join(", ")})`;
65
+ return `@set(${Array.from(this.args, ([key, value]) => `${key}=${typeof value === "string" ? quote(value) : value}`).join(", ")})`;
61
66
  }
62
67
  }
63
68
  export const SetNode = SetFunction;