jpfun 1.1.0 → 1.2.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 (54) hide show
  1. package/dist/diagnostic.d.ts +1 -0
  2. package/dist/diagnostic.js +1 -0
  3. package/dist/functions/ASTtypes.d.ts +2 -3
  4. package/dist/functions/ASTtypes.js +2 -3
  5. package/dist/functions/adjust/index.js +15 -67
  6. package/dist/functions/bar/index.js +8 -7
  7. package/dist/functions/beam/auto.js +15 -11
  8. package/dist/functions/beam/layout.js +22 -48
  9. package/dist/functions/br/index.d.ts +9 -3
  10. package/dist/functions/br/index.js +20 -16
  11. package/dist/functions/dash/index.js +13 -11
  12. package/dist/functions/div/index.d.ts +2 -2
  13. package/dist/functions/div/index.js +5 -9
  14. package/dist/functions/dot/index.js +5 -9
  15. package/dist/functions/dyn/index.js +125 -53
  16. package/dist/functions/grace/index.d.ts +2 -2
  17. package/dist/functions/grace/index.js +5 -9
  18. package/dist/functions/head/index.js +6 -7
  19. package/dist/functions/meter/index.js +7 -5
  20. package/dist/functions/note/index.js +5 -7
  21. package/dist/functions/ottava/index.js +8 -7
  22. package/dist/functions/page/index.js +8 -12
  23. package/dist/functions/pagebreak/index.js +2 -1
  24. package/dist/functions/rest/index.js +5 -5
  25. package/dist/functions/stack/index.js +1 -3
  26. package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
  27. package/dist/functions/symbol/symbols/dynamics.js +10 -1
  28. package/dist/functions/tempo/index.js +8 -3
  29. package/dist/functions/temporal.d.ts +18 -6
  30. package/dist/functions/temporal.js +1 -1
  31. package/dist/functions/text/index.js +1 -3
  32. package/dist/functions/up/index.js +29 -28
  33. package/dist/functions/voice/voice.d.ts +2 -1
  34. package/dist/functions/voice/voice.js +7 -13
  35. package/dist/functions/voice/voices.d.ts +4 -3
  36. package/dist/functions/voice/voices.js +8 -8
  37. package/dist/jpfun.min.js +48 -45
  38. package/dist/layout/engine.js +224 -211
  39. package/dist/layout/page.d.ts +2 -2
  40. package/dist/layout/page.js +8 -22
  41. package/dist/layout/types.d.ts +2 -4
  42. package/dist/lowering/loweringContext.d.ts +12 -7
  43. package/dist/lowering/loweringContext.js +50 -51
  44. package/dist/lowering/types.d.ts +2 -3
  45. package/dist/parser/grammarType.d.ts +4 -3
  46. package/dist/parser/parse-utils/brace-utils.js +4 -3
  47. package/dist/parser/parse-utils/call-utils.js +7 -7
  48. package/dist/parser/parserContext.d.ts +5 -9
  49. package/dist/parser/parserContext.js +24 -36
  50. package/dist/parser/types.d.ts +2 -1
  51. package/dist/playback/compile.js +23 -13
  52. package/dist/playback/types.d.ts +0 -1
  53. package/grammar.md +7 -6
  54. package/package.json +2 -2
@@ -47,6 +47,7 @@ class TempoFunction extends ASTFunctionNode {
47
47
  export const TempoNode = TempoFunction;
48
48
  class TempoTemporalNode extends TemporalNodeBase {
49
49
  /** 字号和 bpm 在解析时已经固化,字形、文本、样式随之不再变 */
50
+ bpm;
50
51
  glyph;
51
52
  text;
52
53
  style;
@@ -61,13 +62,17 @@ class TempoTemporalNode extends TemporalNodeBase {
61
62
  this.style = { fontSize: ast.size, fontFamily: ast.font, fill: "#000" };
62
63
  // 非法值不入时间状态,也不告知播放系统,效果是保持上一个速度
63
64
  if (Number.isFinite(ast.bpm) && ast.bpm > 0)
64
- this.playbackState = { bpm: ast.bpm };
65
+ this.bpm = ast.bpm;
65
66
  this.initLayoutBox();
66
67
  }
67
68
  onTimeState(state) {
68
- const bpm = this.playbackState?.bpm;
69
+ if (this.bpm !== undefined)
70
+ state.bpm = this.bpm;
71
+ }
72
+ emitPlayback(emitter) {
73
+ const bpm = this.bpm;
69
74
  if (bpm !== undefined)
70
- state.bpm = bpm;
75
+ emitter.control(emitter.start, state => { state.bpm = bpm; });
71
76
  }
72
77
  prepareLayout(context) {
73
78
  const glyph = this.glyph;
@@ -26,16 +26,24 @@ export interface TimeState {
26
26
  accidentals: AccidentalMemory;
27
27
  /** 当前小节时长(QN);尚无显式拍号或散板时为 undefined */
28
28
  meter?: Fraction;
29
+ /** 当前可输出的拍号;尚无显式拍号或散板时为 undefined */
30
+ timeSignature?: {
31
+ readonly numerator: number;
32
+ readonly denominator: number;
33
+ };
29
34
  [key: string]: any;
30
35
  }
31
36
  export declare const DEFAULT_BPM = 120;
32
37
  export declare const DEFAULT_VELOCITY = 80;
33
38
  export declare const DEFAULT_TONALITY = "C4";
34
39
  export declare const DEFAULT_PROGRAM = 0;
35
- /** 每个事件的可选语义声明;核心字段与函数私有的 @键分开 */
40
+ /** 每个事件的可选语义声明;核心字段与函数私有的 @ 键分开 */
36
41
  export interface TemporalAddon {
37
- /** 请求在当前时间列之前换几行,缺省为 0 */
38
- breakBefore?: number;
42
+ /** 在当前时间列之前换一行,值为与上一行的间距(px);同列逐项取较大值,同轨连续多列各换一行 */
43
+ breakBefore?: {
44
+ gap: number;
45
+ minGap: number;
46
+ };
39
47
  /** 在所属行之前强制换页;配合 breakBefore 使用,布尔值控制上一页的余白分配 */
40
48
  pageBreakBefore?: boolean;
41
49
  [key: `@${string}`]: unknown;
@@ -88,7 +96,10 @@ export declare class TemporalNodeBase implements TimeLineEvent {
88
96
  layoutLine: number;
89
97
  /** 不可见状态事件保持 undefined */
90
98
  box?: LayoutBox;
91
- /** 显式微调时才创建后置平移,折叠成员还会继承外层对象的平移 */
99
+ /**
100
+ * 后置平移:在 lowering 阶段声明,引擎在占用测量后叠加到 box 上并再次调用 onPlaced,不参与占用求解
101
+ * 折叠成员经复合体的 onPlaced 跟随外层平移,再叠加自己的平移
102
+ */
92
103
  placementOffset?: LayoutPoint;
93
104
  /**
94
105
  * 横向弹簧布局参数
@@ -142,7 +153,7 @@ export declare class TemporalNodeBase implements TimeLineEvent {
142
153
  emitPlayback?(emitter: PlaybackEmitter): void;
143
154
  /** 给自己所在的时间列贴播放标记,供控制流游标查询 */
144
155
  playbackMarks?(): readonly string[];
145
- /** 向播放系统告知自己这个记谱位置上的速度与力度 */
156
+ /** 向播放系统告知自己这个记谱位置上的力度、音色等状态 */
146
157
  playbackState?: PlaybackState;
147
158
  /**
148
159
  * 根据已经固化的语义生成固有尺寸
@@ -158,8 +169,9 @@ export declare class TemporalNodeBase implements TimeLineEvent {
158
169
  */
159
170
  finalizeLayout?(_ctx: LayoutPrepareContext): void;
160
171
  /**
161
- * 布局器每次改变 x 或 y 后调用
172
+ * 布局器每次改变 x 或 y 后调用,包括施加后置平移之后
162
173
  * 普通叶节点不需要处理,具有内部几何的复合节点可用它同步子对象坐标
174
+ * 只按当前 box 同步:成员自己的后置平移由引擎随后叠加,所以只能由引擎调度
163
175
  */
164
176
  onPlaced?(): void;
165
177
  /** 只执行绘制,不允许在这里重新测量或改变 LayoutBox */
@@ -114,7 +114,7 @@ export class TemporalNodeBase {
114
114
  anchor: 0, visualAxis: 0,
115
115
  };
116
116
  }
117
- /** 向播放系统告知自己这个记谱位置上的速度与力度 */
117
+ /** 向播放系统告知自己这个记谱位置上的力度、音色等状态 */
118
118
  playbackState;
119
119
  /**
120
120
  * 根据已经固化的语义生成固有尺寸
@@ -58,16 +58,14 @@ class TextFunction extends ASTFunctionNode {
58
58
  return null; // 未闭合就不去糖,与预处理“配对才算字符串”一致
59
59
  const node = {
60
60
  kind: "sugar",
61
- data: TextFunction,
62
61
  span: { start, end: close + 1 },
63
62
  syntaxKind: "string",
63
+ relation: TextFunction.deSugarRelation,
64
64
  };
65
65
  return { next: close + 1, node };
66
66
  }
67
67
  static deSugarRelation(ctx, nodes, at) {
68
68
  const node = nodes[at];
69
- if (node.data !== TextFunction)
70
- return null;
71
69
  // 之所以不在 deSugarAtom 里直接解码返回 typed call,是因为字符串转义可能产生诊断(如非法 \u 转义):
72
70
  // 第一轮 deSugarAtom 没有 ctx(抛异常则会中断词法扫描),放到第二轮才能复用 parseArgWithType 记录警告并回退原文
73
71
  ctx.pushNode(new TextFunction(node.span, new Map([[0, ctx.parseArgWithType(node.span, "string")]]), ctx, null));
@@ -92,7 +92,7 @@ class FoldFunction extends ASTFunctionNode {
92
92
  for (const content of this.children) {
93
93
  const columns = ctx.trackedEvents(content, new Fraction(), track);
94
94
  const member = columns.length === 1 && columns[0].length === 1 ? columns[0][0] : undefined;
95
- if (!member || !isVisualTemporalNode(member) || (member.addon?.breakBefore ?? 0) > 0) {
95
+ if (!member || !isVisualTemporalNode(member) || member.addon?.breakBefore) {
96
96
  throw new ErrorDiagnostic("E_UP_INVALID_CHILD", "@up 的每个参数必须恰好产生一个可见 Temporal,且不能换行或包含多声部结构", content.sourceSpan);
97
97
  }
98
98
  members.push(member);
@@ -140,20 +140,15 @@ class FoldFunction extends ASTFunctionNode {
140
140
  ctx.labelableNodes.splice(stale, 1);
141
141
  return this;
142
142
  }
143
- /**
144
- * `this` 是发起调用的那个子类,所以方向和构造都从它取。子类必须写成
145
- * `UpFunction.desugar(...)` 这种带显式接收者的形式 —— 解析器是把
146
- * `deSugarRelation` 摘成游离函数引用来调的,`this` 到不了这里。
147
- */
148
- static desugar(ctx, nodes, at) {
143
+ /** `^` / `_` 共用的关系去糖;sugar 的 data 是发出它的子类,方向和构造都从它取 */
144
+ static deSugarRelation(ctx, nodes, at) {
149
145
  const n = nodes[at++];
150
- if (!(n.kind === "sugar" && n.data === this))
151
- return null;
152
- const label = this.side === "above" ? "@up" : "@down";
146
+ const cls = n.data;
147
+ const label = cls.side === "above" ? "@up" : "@down";
153
148
  const leftNode = ctx.takeLeftOperand();
154
149
  if (!leftNode)
155
150
  throw new ErrorDiagnostic("E_FOLD_NO_TARGET", "{fn}语法糖错误: 左边没有找到可叠加的目标", n.span, { fn: label });
156
- const wrapper = new this({ ...n.span }, new Map(), ctx);
151
+ const wrapper = new cls({ ...n.span }, new Map(), ctx);
157
152
  const host = leftNode instanceof FoldFunction
158
153
  ? leftNode
159
154
  : wrapper.addContent(leftNode, "above", ctx);
@@ -163,8 +158,8 @@ class FoldFunction extends ASTFunctionNode {
163
158
  // 右操作数可能已被 @tie() 等按引用取走(它就在本层剩余部分里,先于合成解析),
164
159
  // 而新建的左包装无人引用,所以合并时让右边存活;接收者就是存活的那一方
165
160
  const merged = right instanceof FoldFunction
166
- ? right.flatten(host, this.side, ctx, wrapper.gap, host !== wrapper)
167
- : host.addContent(right, this.side, ctx, wrapper.gap);
161
+ ? right.flatten(host, cls.side, ctx, wrapper.gap, host !== wrapper)
162
+ : host.addContent(right, cls.side, ctx, wrapper.gap);
168
163
  // 语法糖不走 pushNode,这里补登记;null 同样入表,和显式调用一样成为边界
169
164
  ctx.labelableNodes.push(merged.labelable());
170
165
  return merged;
@@ -192,12 +187,9 @@ class UpFunction extends FoldFunction {
192
187
  static deSugarAtom(source, start, _end) {
193
188
  if (source[start] !== "^")
194
189
  return null;
195
- const node = { kind: "sugar", data: UpFunction, span: { start, end: start + 1 } };
190
+ const node = { kind: "sugar", data: UpFunction, span: { start, end: start + 1 }, relation: FoldFunction.deSugarRelation };
196
191
  return { next: start + 1, node };
197
192
  }
198
- static deSugarRelation(ctx, nodes, at) {
199
- return UpFunction.desugar(ctx, nodes, at);
200
- }
201
193
  }
202
194
  class DownFunction extends FoldFunction {
203
195
  static side = "below";
@@ -218,12 +210,9 @@ class DownFunction extends FoldFunction {
218
210
  static deSugarAtom(source, start, _end) {
219
211
  if (source[start] !== "_")
220
212
  return null;
221
- const node = { kind: "sugar", data: DownFunction, span: { start, end: start + 1 } };
213
+ const node = { kind: "sugar", data: DownFunction, span: { start, end: start + 1 }, relation: FoldFunction.deSugarRelation };
222
214
  return { next: start + 1, node };
223
215
  }
224
- static deSugarRelation(ctx, nodes, at) {
225
- return DownFunction.desugar(ctx, nodes, at);
226
- }
227
216
  }
228
217
  export const UpNode = UpFunction;
229
218
  export const DownNode = DownFunction;
@@ -268,15 +257,27 @@ class FoldTemporal extends TemporalNodeBase {
268
257
  }
269
258
  }
270
259
  /**
271
- * 成员共享全局时间状态,从写在最后的成员开始固化
260
+ * 固化与播放共用的成员顺序:零时长成员(记号、设置、文字)→ 有时长成员 → 宿主,组内倒序。
261
+ * 记号因此先写入状态、先注册效果,上下两侧的音都能读到;宿主始终最后
262
+ */
263
+ memberOrder() {
264
+ const marks = [];
265
+ const notes = [];
266
+ for (let i = this.members.length - 1; i > 0; i--) {
267
+ const member = this.members[i];
268
+ (member.T.isZero() ? marks : notes).push(member);
269
+ }
270
+ const host = this.members[0];
271
+ return host ? [...marks, ...notes, host] : [...marks, ...notes];
272
+ }
273
+ /**
274
+ * 成员共享全局时间状态,按 memberOrder 固化
272
275
  */
273
276
  onTimeState(state) {
274
277
  const duration = this.T.clone();
275
278
  let offset = undefined;
276
279
  let durationChanged = false;
277
- // 宿主写在最前面,标记写在它后面;标记先写入状态,宿主才读得到
278
- for (let i = this.members.length - 1; i >= 0; i--) {
279
- const member = this.members[i];
280
+ for (const member of this.memberOrder()) {
280
281
  // 堆叠在一起的成员共享同一个时值,由第一个成员决定;
281
282
  // 本来就没有时长的成员(标注、小节线等)保持 0,不会被拉长
282
283
  if (!member.T.isZero() && !this.T.isZero())
@@ -300,9 +301,9 @@ class FoldTemporal extends TemporalNodeBase {
300
301
  return offset;
301
302
  }
302
303
  emitPlayback(emitter) {
303
- // 倒序发布,宿主(members[0])最后:附属成员注册的局部变换因此作用到宿主
304
- for (let i = this.members.length - 1; i >= 0; i--)
305
- emitter.play(this.members[i]);
304
+ // 记号先发布,注册的局部变换因此作用到所有有声成员和宿主
305
+ for (const member of this.memberOrder())
306
+ emitter.play(member);
306
307
  }
307
308
  prepareHorizontal(line) {
308
309
  this.members[0]?.prepareHorizontal?.(line);
@@ -30,7 +30,8 @@ export declare class VoiceFunction extends ASTFunctionNode {
30
30
  next: number;
31
31
  node: GrammarSugarNode;
32
32
  } | null;
33
- static deSugarRelation(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number, members?: VoiceFunction[]): number | null;
33
+ /** `N:` / `L:` 的关系去糖;V 声明分组时直接调用并传入 members 收集成员 */
34
+ static deSugarLine(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number, members?: VoiceFunction[]): number;
34
35
  content: ASTBraceNode;
35
36
  name: string;
36
37
  size: number;
@@ -75,11 +75,9 @@ L: la la la
75
75
  // 等到第二轮寻找该层级的终止符 \n 来确定内容范围
76
76
  const node = {
77
77
  kind: "sugar",
78
- data: {
79
- class: VoiceFunction,
80
- name,
81
- },
78
+ data: { name },
82
79
  span: { start, end: pos },
80
+ relation: VoiceFunction.deSugarLine,
83
81
  };
84
82
  return { next: pos, node };
85
83
  }
@@ -105,23 +103,19 @@ L: la la la
105
103
  }
106
104
  const node = {
107
105
  kind: "sugar",
108
- data: {
109
- class: VoiceFunction,
110
- lyric,
111
- name,
112
- },
106
+ data: { lyric, name },
113
107
  span: { start, end: lyric.end },
114
108
  syntaxKind: [
115
109
  { kind: "operator", span: { start, end: pos } },
116
110
  { kind: "string", span: lyric },
117
111
  ],
112
+ relation: VoiceFunction.deSugarLine,
118
113
  };
119
114
  return { next: lyric.end, node };
120
115
  }
121
- static deSugarRelation(ctx, nodes, at, members) {
116
+ /** `N:` / `L:` 的关系去糖;V 声明分组时直接调用并传入 members 收集成员 */
117
+ static deSugarLine(ctx, nodes, at, members) {
122
118
  const n = nodes[at++];
123
- if (n.data?.class !== VoiceFunction)
124
- return null;
125
119
  const parseStr = (span) => ctx.parseArgWithType(span, "string") ?? "";
126
120
  if (n.data?.lyric !== void 0) {
127
121
  // 歌词 需要找到最近的voice节点并添加歌词
@@ -165,7 +159,7 @@ L: la la la
165
159
  }
166
160
  }
167
161
  else if (n.kind === "sugar"
168
- && (n.data?.class === VoiceFunction || n.data?.class === VoicesFunction))
162
+ && (n.relation === VoiceFunction.deSugarLine || n.relation === VoicesFunction.deSugarGroup))
169
163
  break;
170
164
  }
171
165
  // 解析后面的内容 得到 VoiceFunction
@@ -50,20 +50,19 @@ export declare class VoicesFunction extends ASTFunctionNode {
50
50
  kind: "sugar";
51
51
  data: {
52
52
  name?: undefined;
53
- class: typeof VoicesFunction;
54
53
  kind: "line";
55
54
  };
56
55
  span: {
57
56
  start: number;
58
57
  end: number;
59
58
  };
59
+ relation: typeof VoicesFunction.deSugarGroup;
60
60
  };
61
61
  } | {
62
62
  next: number;
63
63
  node: {
64
64
  kind: "sugar";
65
65
  data: {
66
- class: typeof VoicesFunction;
67
66
  kind: "brace" | "bracket";
68
67
  name: {
69
68
  start: number;
@@ -74,9 +73,11 @@ export declare class VoicesFunction extends ASTFunctionNode {
74
73
  start: number;
75
74
  end: number;
76
75
  };
76
+ relation: typeof VoicesFunction.deSugarGroup;
77
77
  };
78
78
  } | null;
79
- static deSugarRelation(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number): number | null;
79
+ /** `V|:`、`V{name}:`、`V[name]:` 的关系去糖;向后连续收集 V 声明及其 N/L 行 */
80
+ static deSugarGroup(ctx: ParserContext, nodes: (GrammarNode | number)[], at: number): number;
80
81
  voices: VoiceFunction[];
81
82
  createdBySugar: boolean;
82
83
  size: number;
@@ -104,8 +104,9 @@ N(女低): 1 2 3
104
104
  next: start + 3,
105
105
  node: {
106
106
  kind: "sugar",
107
- data: { class: VoicesFunction, kind: "line" },
107
+ data: { kind: "line" },
108
108
  span: { start, end: start + 3 },
109
+ relation: VoicesFunction.deSugarGroup,
109
110
  },
110
111
  };
111
112
  }
@@ -120,18 +121,17 @@ N(女低): 1 2 3
120
121
  node: {
121
122
  kind: "sugar",
122
123
  data: {
123
- class: VoicesFunction,
124
124
  kind: open === "{" ? "brace" : "bracket",
125
125
  name: { start: start + 2, end: at },
126
126
  },
127
127
  span: { start, end: at + 2 },
128
+ relation: VoicesFunction.deSugarGroup,
128
129
  },
129
130
  };
130
131
  }
131
- static deSugarRelation(ctx, nodes, at) {
132
+ /** `V|:`、`V{name}:`、`V[name]:` 的关系去糖;向后连续收集 V 声明及其 N/L 行 */
133
+ static deSugarGroup(ctx, nodes, at) {
132
134
  const first = nodes[at];
133
- if (typeof first === "number" || first.kind !== "sugar" || first.data?.class !== VoicesFunction)
134
- return null;
135
135
  let previousIndex = ctx.nodes.length - 1;
136
136
  while (previousIndex >= 0 && ctx.nodes[previousIndex] instanceof ASTTextNode) {
137
137
  const { sourceSpan } = ctx.nodes[previousIndex];
@@ -148,7 +148,7 @@ N(女低): 1 2 3
148
148
  let cursor = at;
149
149
  while (cursor < nodes.length) {
150
150
  const marker = nodes[cursor];
151
- if (typeof marker === "number" || marker.kind !== "sugar" || marker.data?.class !== VoicesFunction)
151
+ if (typeof marker === "number" || marker.kind !== "sugar" || marker.relation !== VoicesFunction.deSugarGroup)
152
152
  break;
153
153
  cursor++;
154
154
  const carriageReturn = nodes[cursor];
@@ -162,9 +162,9 @@ N(女低): 1 2 3
162
162
  const from = members.length + 1;
163
163
  while (cursor < nodes.length) {
164
164
  const node = nodes[cursor];
165
- if (typeof node === "number" || node.kind !== "sugar" || node.data?.class !== VoiceFunction)
165
+ if (typeof node === "number" || node.kind !== "sugar" || node.relation !== VoiceFunction.deSugarLine)
166
166
  break;
167
- cursor = VoiceFunction.deSugarRelation(section, nodes, cursor, members);
167
+ cursor = VoiceFunction.deSugarLine(section, nodes, cursor, members);
168
168
  }
169
169
  if (members.length < from) {
170
170
  throw new ErrorDiagnostic("E_VOICES_GROUP_EMPTY", "V 声部分组必须至少包含一条 N: 声部声明", marker.span);