jpfun 0.5.0 → 0.7.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 (79) hide show
  1. package/dist/converter/midi/convert.d.ts +3 -1
  2. package/dist/converter/midi/convert.js +78 -66
  3. package/dist/converter/musicxml/features.d.ts +19 -5
  4. package/dist/converter/musicxml/features.js +46 -8
  5. package/dist/converter/musicxml/index.js +142 -23
  6. package/dist/converter/musicxml/model.d.ts +15 -0
  7. package/dist/diagnostic.js +1 -1
  8. package/dist/functions/ASTtypes.d.ts +4 -0
  9. package/dist/functions/ASTtypes.js +25 -14
  10. package/dist/functions/adjust/index.js +6 -2
  11. package/dist/functions/arpeggio/index.js +21 -17
  12. package/dist/functions/arpeggio/shape.d.ts +1 -1
  13. package/dist/functions/arpeggio/shape.js +27 -21
  14. package/dist/functions/bar/index.js +37 -36
  15. package/dist/functions/beam/auto.js +18 -24
  16. package/dist/functions/beam/layout.js +15 -23
  17. package/dist/functions/box/index.js +52 -1
  18. package/dist/functions/br/index.js +3 -4
  19. package/dist/functions/default.js +10 -5
  20. package/dist/functions/div/index.js +2 -3
  21. package/dist/functions/gliss/index.d.ts +2 -0
  22. package/dist/functions/gliss/index.js +180 -0
  23. package/dist/functions/grace/index.d.ts +4 -2
  24. package/dist/functions/grace/index.js +19 -11
  25. package/dist/functions/head/index.js +50 -62
  26. package/dist/functions/meter/index.js +45 -24
  27. package/dist/functions/meter/shape.d.ts +3 -0
  28. package/dist/functions/meter/shape.js +43 -0
  29. package/dist/functions/note/accidentals.js +10 -16
  30. package/dist/functions/note/index.js +38 -48
  31. package/dist/functions/ottava/index.d.ts +2 -0
  32. package/dist/functions/ottava/index.js +217 -0
  33. package/dist/functions/page/index.js +3 -3
  34. package/dist/functions/pagebreak/index.d.ts +2 -0
  35. package/dist/functions/pagebreak/index.js +36 -0
  36. package/dist/functions/rest/index.d.ts +2 -0
  37. package/dist/functions/rest/index.js +231 -0
  38. package/dist/functions/slide/index.d.ts +2 -0
  39. package/dist/functions/slide/index.js +132 -0
  40. package/dist/functions/stack/index.js +8 -15
  41. package/dist/functions/symbol/index.d.ts +9 -0
  42. package/dist/functions/symbol/index.js +41 -12
  43. package/dist/functions/symbol/ornament.js +15 -20
  44. package/dist/functions/symbol/shape.js +4 -1
  45. package/dist/functions/symbol/symbols/fine.js +1 -1
  46. package/dist/functions/symbol/symbols/tr.js +46 -1
  47. package/dist/functions/temporal.d.ts +36 -12
  48. package/dist/functions/temporal.js +29 -9
  49. package/dist/functions/tie/index.js +46 -32
  50. package/dist/functions/tuplet/index.js +1 -1
  51. package/dist/functions/up/index.js +51 -33
  52. package/dist/functions/voice/connections.d.ts +19 -0
  53. package/dist/functions/voice/connections.js +90 -0
  54. package/dist/functions/voice/index.js +245 -99
  55. package/dist/functions/volta/index.js +41 -21
  56. package/dist/jpfun.core.min.js +48 -55
  57. package/dist/jpfun.from-midi.min.js +5 -5
  58. package/dist/jpfun.from-musicxml.min.js +8 -8
  59. package/dist/jpfun.min.js +57 -64
  60. package/dist/layout/engine.d.ts +4 -1
  61. package/dist/layout/engine.js +232 -75
  62. package/dist/layout/model.js +22 -66
  63. package/dist/layout/page.d.ts +3 -2
  64. package/dist/layout/page.js +7 -6
  65. package/dist/layout/types.d.ts +34 -45
  66. package/dist/lowering/loweringContext.d.ts +1 -1
  67. package/dist/lowering/loweringContext.js +41 -171
  68. package/dist/parser/parserContext.d.ts +2 -0
  69. package/dist/parser/parserContext.js +31 -14
  70. package/dist/parser/preprocess.js +4 -6
  71. package/dist/playback/compile.js +12 -3
  72. package/dist/playback/types.d.ts +4 -2
  73. package/dist/render/paint.js +1 -1
  74. package/dist/render/svg.js +1 -0
  75. package/dist/render/text.d.ts +1 -1
  76. package/dist/render/text.js +3 -2
  77. package/dist/render/types.d.ts +1 -0
  78. package/grammar.md +221 -257
  79. package/package.json +1 -1
@@ -39,6 +39,8 @@ export class ParserContext {
39
39
  source;
40
40
  /** 词法层产物,只由 parseSyntax 填充;所有子上下文共享 */
41
41
  syntax;
42
+ /** syntax.tokens 前缀注释区间的扫描游标 */
43
+ commentIndex;
42
44
  /** 解析过程中产生的诊断信息,包含错误和警告 */
43
45
  diagnostics;
44
46
  /** 变量表,存储 `@set` 定义的变量,供解析过程中查询和修改,具有局部作用域 */
@@ -62,6 +64,7 @@ export class ParserContext {
62
64
  this.source = ctx.source;
63
65
  this.diagnostics = ctx.diagnostics; // 诊断信息全局共享
64
66
  this.syntax = ctx.syntax;
67
+ this.commentIndex = ctx.commentIndex;
65
68
  this.variables = { ...ctx.variables }; // 继承但不修改父上下文的变量
66
69
  this.functions = ctx.functions; // 函数定义全局共享
67
70
  this.deSugarAtomFns = ctx.deSugarAtomFns; // 去糖方法全局共享
@@ -78,6 +81,7 @@ export class ParserContext {
78
81
  tokens: (ctx.commentSpans ?? []).map(span => ({ kind: "comment", span })),
79
82
  calls: [],
80
83
  };
84
+ this.commentIndex = 0;
81
85
  this.variables = Object.fromEntries(Object.entries(ParserContext.systemVariables).map(([name, definition]) => [name, definition.default]));
82
86
  Object.assign(this.variables, ctx.variables);
83
87
  this.functions = ctx.functions ?? new Map();
@@ -159,10 +163,27 @@ export class ParserContext {
159
163
  }
160
164
  return this.syntax;
161
165
  }
162
- // syntax 路径独占这些 span,AST 路径不会读取或改写它们
166
+ // syntax 路径独占这些 span,AST 路径不会读取或改写它们;跨注释区间按前缀 comment token 切开以保证互不重叠
163
167
  addSyntax(kind, span) {
164
- if (span && span.start < span.end) {
165
- this.syntax.tokens.push({ kind, span });
168
+ if (!span || span.start >= span.end)
169
+ return;
170
+ let start = span.start;
171
+ const tokens = this.syntax.tokens;
172
+ while (tokens[this.commentIndex]?.kind === "comment" && tokens[this.commentIndex].span.end <= start) {
173
+ this.commentIndex++;
174
+ }
175
+ for (let i = this.commentIndex; i < tokens.length; i++) {
176
+ const token = tokens[i];
177
+ if (token.kind !== "comment" || token.span.start >= span.end)
178
+ break;
179
+ if (start < token.span.start) {
180
+ tokens.push({ kind, span: { start, end: token.span.start } });
181
+ }
182
+ start = token.span.end;
183
+ this.commentIndex = i + 1;
184
+ }
185
+ if (start < span.end) {
186
+ tokens.push({ kind, span: { start, end: span.end } });
166
187
  }
167
188
  }
168
189
  /** 把已识别的 GrammarNode 映射为源码 token;content/brace 在这里继续递归 */
@@ -170,9 +191,9 @@ export class ParserContext {
170
191
  if (node.kind === "brace") {
171
192
  this.addSyntax("punctuation", { start: node.span.start, end: node.span.start + 1 });
172
193
  const close = Math.min(this.source.length, node.span.end - 1);
194
+ new ParserContext(this).parseSyntax(node.span.start + 1, close);
173
195
  if (this.source[close] === "}")
174
196
  this.addSyntax("punctuation", { start: close, end: close + 1 });
175
- new ParserContext(this).parseSyntax(node.span.start + 1, close);
176
197
  return;
177
198
  }
178
199
  if (node.kind === "label") {
@@ -188,13 +209,11 @@ export class ParserContext {
188
209
  this.syntax.calls.push(call);
189
210
  this.addSyntax("function", call.nameSpan);
190
211
  this.addSyntax("punctuation", call.openParenSpan);
191
- this.addSyntax("punctuation", call.closeParenSpan);
192
212
  const def = this.functions.get(call.name.toLowerCase())?.def;
193
213
  for (let index = 0; index < call.args.length; index++) {
194
214
  const arg = call.args[index];
195
215
  this.addSyntax("property", arg.nameSpan);
196
216
  this.addSyntax("punctuation", arg.equalsSpan);
197
- this.addSyntax("punctuation", arg.commaSpan);
198
217
  const name = arg.nameSpan
199
218
  ? this.source.slice(arg.nameSpan.start, arg.nameSpan.end).toLowerCase()
200
219
  : void 0;
@@ -208,7 +227,9 @@ export class ParserContext {
208
227
  if (kind)
209
228
  this.addSyntax(kind, arg.valueSpan);
210
229
  }
230
+ this.addSyntax("punctuation", arg.commaSpan);
211
231
  }
232
+ this.addSyntax("punctuation", call.closeParenSpan);
212
233
  }
213
234
  /**
214
235
  * 第二轮去糖 & 函数节点创建
@@ -408,7 +429,7 @@ export class ParserContext {
408
429
  const key = name ?? i;
409
430
  if (type === void 0) {
410
431
  if (!def.allowExtraArgs) {
411
- this.diagnostics.push(Diagnostic.warning.TooManyPosArgs(callNode.name, defArgs.length, i + 1, arg.valueSpan));
432
+ this.diagnostics.push(Diagnostic.warning.TooManyPosArgs(callNode.name, defArgs.filter(arg => !arg.namedOnly).length, i + 1, arg.valueSpan));
412
433
  }
413
434
  else
414
435
  args.set(key, arg);
@@ -445,9 +466,8 @@ export class ParserContext {
445
466
  }
446
467
  return num;
447
468
  case "boolean":
448
- const boolText = text;
449
- if (BOOL_RE.test(boolText)) {
450
- return boolText === "true";
469
+ if (BOOL_RE.test(text)) {
470
+ return text === "true";
451
471
  }
452
472
  else {
453
473
  this.diagnostics.push(Diagnostic.warning.InvalidBoolean(text, r));
@@ -509,11 +529,8 @@ export class ParserContext {
509
529
  return l;
510
530
  // 其他类型一律视为string
511
531
  default:
512
- // 去除首尾引号(单引号或双引号),如果存在
513
- let result = text;
514
- return removeQuote(result);
532
+ return removeQuote(text);
515
533
  }
516
- return null;
517
534
  }
518
535
  length2px(length) {
519
536
  // 不进行错误判断了: 来自 parseArgWithType 的不会出问题
@@ -7,6 +7,7 @@ import { findClosingQuote } from "./parse-utils/string-utils.js";
7
7
  * - 设行尾连续 `\` 个数为 n;
8
8
  * - 第 1/3/5... 个 `\` 替换为空格,第 2/4/6... 个保留;
9
9
  * - 若 n 为奇数,则该行换行符也替换为空格(续行)
10
+ * 4. 字符串外 CRLF 中的 CR 等长替换为空格,词法层只消费 LF
10
11
  */
11
12
  const CHAR_LF = 10;
12
13
  const CHAR_CR = 13;
@@ -79,7 +80,6 @@ export function preprocessSource(source) {
79
80
  * - inQuote:0=不在字符串;其他值为当前引号字符码
80
81
  * - escaped:仅在字符串内有效,表示上一个字符是 `\`
81
82
  * - pendingCommentStart:>=0 表示已遇到 `%`,等待到行尾/EOF 收口注释
82
- * - lastSignificantIndex:当前行最后一个“非换行空白”字符位置
83
83
  * - tailBackslashRunStart/tailBackslashRunCount:
84
84
  * 当前行末尾候选连续 `\` 片段(允许后面跟空白),用于换行处一次性判定
85
85
  */
@@ -87,23 +87,20 @@ export function preprocessSource(source) {
87
87
  let noPairedQuoteAfter = false;
88
88
  let escaped = false;
89
89
  let pendingCommentStart = -1;
90
- let lastSignificantIndex = -1;
91
90
  let tailBackslashRunStart = -1;
92
91
  let tailBackslashRunCount = 0;
93
92
  const updateLineTailState = (index, ch) => {
94
93
  if (ch === CHAR_BACKSLASH) {
95
- if (tailBackslashRunCount > 0 && lastSignificantIndex + 1 === index)
94
+ if (tailBackslashRunCount > 0 && tailBackslashRunStart + tailBackslashRunCount === index)
96
95
  tailBackslashRunCount++;
97
96
  else {
98
97
  tailBackslashRunStart = index;
99
98
  tailBackslashRunCount = 1;
100
99
  }
101
- lastSignificantIndex = index;
102
100
  return;
103
101
  }
104
102
  tailBackslashRunStart = -1;
105
103
  tailBackslashRunCount = 0;
106
- lastSignificantIndex = index;
107
104
  };
108
105
  for (let i = 0; i < sourceLength;) {
109
106
  const ch = source.charCodeAt(i);
@@ -121,8 +118,9 @@ export function preprocessSource(source) {
121
118
  }
122
119
  if (isOddBackslashRun)
123
120
  pushReplaceRange(i, i + newlineWidth);
121
+ else if (inQuote === 0 && newlineWidth === 2)
122
+ pushReplaceRange(i, i + 1);
124
123
  // 新行重置行内状态
125
- lastSignificantIndex = -1;
126
124
  tailBackslashRunStart = -1;
127
125
  tailBackslashRunCount = 0;
128
126
  escaped = false;
@@ -21,11 +21,20 @@ function linearizeColumns(lowering, diagnostics, maxFlowSteps) {
21
21
  }
22
22
  // 折叠成员不单独占列,沿 foldedInto 找到最近的已入列宿主
23
23
  // 标记和关系端点都使用这个映射,保持控制流与实际时间列一致
24
- const resolveColumn = (node) => {
24
+ const resolveColumn = (node, edge) => {
25
25
  for (let at = node; at; at = at.foldedInto) {
26
26
  const index = columnOf.get(at);
27
- if (index !== void 0)
27
+ if (index === undefined)
28
+ continue;
29
+ if (edge !== "end" || at.T.compare(0) <= 0)
28
30
  return index;
31
+ // 一个被标签引用的事件,其完整时值覆盖了多个时间列;终点查询不包含结束时刻的列。
32
+ const finish = at.t.clone().add(at.T);
33
+ let end = index;
34
+ // 用顺序向后找而不是二分因为基本就在后一个(@rest除外)
35
+ while (end + 1 < columns.length && columns[end + 1][0].t.compare(finish) < 0)
36
+ end++;
37
+ return end;
29
38
  }
30
39
  return void 0;
31
40
  };
@@ -508,7 +517,7 @@ export function compilePlayback(lowering, options) {
508
517
  }
509
518
  }
510
519
  // 稳定排序后,由统一出口检查配对、压实轨道编号并剥离编译期字段
511
- const finalEvents = events.filter(event => !removed.has(event)).sort(comparePlaybackDraftEvents);
520
+ const finalEvents = (removed.size ? events.filter(event => !removed.has(event)) : events).sort(comparePlaybackDraftEvents);
512
521
  const output = finalizePlaybackEvents(finalEvents, lowering.tracks);
513
522
  const eventEnd = finalEvents.at(-1)?.at;
514
523
  if (eventEnd && eventEnd.compare(performanceEnd) > 0)
@@ -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 {
@@ -12,7 +12,7 @@ export function paintLayout(result, painter) {
12
12
  for (const node of result.objects) {
13
13
  node.paint(painter);
14
14
  for (const decoration of node.decorations)
15
- decoration.paint(painter);
15
+ decoration.paint?.(painter);
16
16
  }
17
17
  for (const attachment of result.attachments) {
18
18
  if (attachment.layer === "foreground")
@@ -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();
@@ -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;