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,6 +1,6 @@
1
1
  import { ErrorDiagnostic } from "../../diagnostic.js";
2
2
  import { layoutHorizontalRegion } from "../../layout/model.js";
3
- import { isVisualTemporalNode, TemporalNodeBase, } from "../../lowering/types.js";
3
+ import { isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
4
4
  import { ParserContext, skipSpaces, skipSpacesBack } from "../../parser/parserContext.js";
5
5
  import { ASTBraceNode, ASTFunctionNode, ASTNodeBase, } from "../ASTtypes.js";
6
6
  import { KeyNode } from "../key/index.js";
@@ -72,6 +72,11 @@ function measureRows(members, gap, capture) {
72
72
  return placements;
73
73
  }
74
74
  class HeadBoundaryTemporal extends TemporalNodeBase {
75
+ members = [];
76
+ slots = [];
77
+ titleBottom = 0;
78
+ leftExtent = null;
79
+ rightExtent = null;
75
80
  constructor(ast) {
76
81
  super();
77
82
  this.ast = ast;
@@ -80,12 +85,128 @@ class HeadBoundaryTemporal extends TemporalNodeBase {
80
85
  prepareLayout() { this.box.w = this.box.h = this.box.anchor = this.box.visualAxis = 0; }
81
86
  prepareHorizontal(line) {
82
87
  // 只由第一个边界协调一次整块 Head,避免六个边界重复改弹簧
83
- if (this.ast.name === "left" && this === this.ast.start)
84
- this.ast.owner.prepareHead(line);
88
+ if (this.slots.length > 0)
89
+ this.prepareHead(line);
85
90
  }
86
91
  onPlaced() {
87
- if (this.ast.name === "left" && this === this.ast.start)
88
- this.ast.owner.alignHead();
92
+ if (this.slots.length > 0)
93
+ this.alignHead();
94
+ }
95
+ createTracks(slot) {
96
+ const { name, owner } = slot;
97
+ return {
98
+ laneKey: `head/rows/${owner.sourceSpan.start}/${name}`,
99
+ hostIndex: null,
100
+ measure: members => {
101
+ if (name === "center") {
102
+ const placements = measureRows(members, owner.gap);
103
+ const first = placements.find(placement => placement !== null);
104
+ this.titleBottom = first ? first.offset + first.extent.bottom : 0;
105
+ return placements;
106
+ }
107
+ return measureRows(members, owner.gap, extent => {
108
+ if (name === "left")
109
+ this.leftExtent = extent;
110
+ else
111
+ this.rightExtent = extent;
112
+ });
113
+ },
114
+ place: name === "center" ? undefined : () => {
115
+ const extent = name === "left" ? this.leftExtent : this.rightExtent;
116
+ if (!extent)
117
+ return this.titleBottom;
118
+ const leftHeight = this.leftExtent ? this.leftExtent.bottom - this.leftExtent.top : 0;
119
+ const rightHeight = this.rightExtent ? this.rightExtent.bottom - this.rightExtent.top : 0;
120
+ // 两侧共享底边;以较高侧定总高度,使它的顶部恰好贴标题底边
121
+ const targetBottom = this.titleBottom + Math.max(leftHeight, rightHeight);
122
+ return targetBottom - extent.bottom;
123
+ },
124
+ };
125
+ }
126
+ prepareHead(line) {
127
+ if (this.members.some(node => isVisualTemporalNode(node) && node.layoutLine !== line.index)) {
128
+ throw new ErrorDiagnostic("E_HEAD_INTERNAL_BREAK", "@head 的内容不能跨谱面行", this.ast.owner.sourceSpan);
129
+ }
130
+ const [left, center, right] = this.slots;
131
+ // 最外侧不受墙的推力,两个点才能落在纸面边缘
132
+ left.start.springConfig.alpha_L = 0;
133
+ right.end.springConfig.alpha_R = 0;
134
+ // 串联弹簧的静长是两端之和、刚度由软端主导,所以只需 center 自己向两侧撑开
135
+ this.stretch(center.start, "L");
136
+ this.stretch(center.end, "R");
137
+ for (const slot of this.slots) {
138
+ line.registerHorizontalLayoutHook(slot.start, slot.end, context => layoutHeadSlot(context, slot === center));
139
+ }
140
+ }
141
+ alignHead() {
142
+ // 全局列已经完成求解;这里不改 TimeColumn,只覆盖 left/right 主体的最终 box.x
143
+ // center 不参与后置重排,继续使用全局列坐标
144
+ const cells = new Map();
145
+ for (const member of this.members) {
146
+ if (!isVisualTemporalNode(member))
147
+ continue;
148
+ const cell = this.sideCellOf(member.ast);
149
+ if (!cell)
150
+ continue;
151
+ const members = cells.get(cell);
152
+ if (members)
153
+ members.push(member);
154
+ else
155
+ cells.set(cell, [member]);
156
+ }
157
+ const [left, , right] = this.slots;
158
+ this.packRows(left.ast, left.start.box.x, "left", cells);
159
+ this.packRows(right.ast, right.end.box.x, "right", cells);
160
+ }
161
+ stretch(node, side) {
162
+ const alpha = side === "L" ? "alpha_L" : "alpha_R";
163
+ const beta = side === "L" ? "beta_L" : "beta_R";
164
+ // alpha 撑开槽间空间;beta 反向缩放,避免弹簧刚度随 alpha 一起失控
165
+ node.springConfig[alpha] = node.springConfig[alpha] * HEAD_STRETCH;
166
+ node.springConfig[beta] = node.springConfig[beta] * 3 / HEAD_STRETCH;
167
+ }
168
+ packRows(slot, target, side, cells) {
169
+ const gap = this.ast.owner.size * ITEM_GAP_EM;
170
+ const fromLeft = side === "left";
171
+ const direction = fromLeft ? 1 : -1;
172
+ for (const row of slot.rows) {
173
+ const rowCells = row instanceof ASTBraceNode ? row.content : [row];
174
+ let cursor = target;
175
+ for (let index = fromLeft ? 0 : rowCells.length - 1; index >= 0 && index < rowCells.length; index += direction) {
176
+ const members = cells.get(rowCells[index]);
177
+ if (!members)
178
+ continue;
179
+ let left = Infinity;
180
+ let right = -Infinity;
181
+ for (const member of members) {
182
+ left = Math.min(left, member.box.x);
183
+ right = Math.max(right, member.box.x + member.box.w);
184
+ }
185
+ const offset = cursor - (fromLeft ? left : right);
186
+ for (const member of members)
187
+ member.box.x += offset;
188
+ // 复合单元先整体平移,再让其内部成员读取彼此的最终坐标
189
+ for (const member of members)
190
+ member.onPlaced?.();
191
+ cursor += direction * (right - left + gap);
192
+ }
193
+ }
194
+ }
195
+ /** 找到成员所属侧栏行的顶层单元;复合节点内部成员会归回同一个直接子节点 */
196
+ sideCellOf(ast) {
197
+ let cell = ast;
198
+ while (cell.parent) {
199
+ const parent = cell.parent;
200
+ const slot = parent instanceof HeadSlotNode
201
+ ? parent
202
+ : parent instanceof ASTBraceNode && parent.parent instanceof HeadSlotNode
203
+ ? parent.parent
204
+ : null;
205
+ if (slot?.owner === this.ast.owner)
206
+ return slot.name === "center" ? null : cell;
207
+ cell = parent;
208
+ }
209
+ return null;
89
210
  }
90
211
  }
91
212
  /** 一个槽既是 AST 容器,也是把每个顶层行分配到独立 Track 的边界 */
@@ -93,37 +214,32 @@ class HeadSlotNode extends ASTNodeBase {
93
214
  owner;
94
215
  name;
95
216
  rows;
96
- tracks;
97
- start;
98
- end;
99
- constructor(span, owner, name, rows, tracks) {
217
+ constructor(span, owner, name, rows) {
100
218
  super(span, owner);
101
219
  this.owner = owner;
102
220
  this.name = name;
103
221
  this.rows = rows;
104
- this.tracks = tracks;
105
222
  for (const row of this.rows)
106
223
  row.parent = this;
107
224
  }
108
225
  // 零宽边界需要字号契约,但字号由 Head 统一冻结,不在三个槽里重复保存
109
226
  get size() { return this.owner.size; }
110
227
  get children() { return this.rows; }
111
- timeFlowModel() {
228
+ timeFlowModel(ctx) {
112
229
  if (this.rows.length === 0)
113
230
  return null;
231
+ const head = ctx.getTemporalNodes(this.owner.slots[0])[0];
114
232
  return {
115
233
  mode: "parallel",
116
234
  children: this.rows,
117
- tracks: this.tracks,
235
+ tracks: head.createTracks(this),
118
236
  };
119
237
  }
120
238
  loweringEnter() {
121
- this.start = new HeadBoundaryTemporal(this);
122
- return [this.start];
239
+ return [new HeadBoundaryTemporal(this)];
123
240
  }
124
241
  loweringExit() {
125
- this.end = new HeadBoundaryTemporal(this);
126
- return [this.end];
242
+ return [new HeadBoundaryTemporal(this)];
127
243
  }
128
244
  addRow(row) {
129
245
  row.parent = this;
@@ -180,28 +296,32 @@ class HeadSlotNode extends ASTNodeBase {
180
296
  class HeadFunction extends ASTFunctionNode {
181
297
  static def = {
182
298
  name: ["head"],
183
- description: "提供左、中、右三块布局,常用于曲谱头部信息展示;也可用连续的 H.*: 行声明构造",
184
- example: `@head(left={{@key(C) @meter(4,4)}}, center={@text(标题)}, right={@text(作者)})
185
- 语法糖:相邻的 H.*: 声明合并为一个 head,每条声明生成对应槽的一行;空行或普通内容结束组合
186
- H.title: 标题 center 槽,预设大字号居中 Text
187
- H.subtitle: 副标题 center 槽,预设小字号居中 Text
188
- H.author: 作者 right 槽,预设小字号右对齐 Text
189
- H.signature: 1=C 4/4 left 槽,同一行生成 Key 和 Meter
190
- H.tempo: 94 left 槽,生成 Tempo
191
- H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
192
- 以 @ 或 { 开头时按 DSL 解析,否则按该字段的裸文本规则生成 Text`,
299
+ description: "左、中、右三区布局",
300
+ details: `常用于谱头:
301
+ ~~~jpfun
302
+ @head(left={{@key(C) @meter(4,4)}},
303
+ center={@text(标题)}, right={@text(作者)})
304
+ ~~~
305
+ **简写**:相邻的 \`H.*:\` 行合并为同一个谱头,每条声明生成所在区的一行;空行或普通内容结束组合
306
+ ~~~jpfun
307
+ H.title: 标题
308
+ H.subtitle: 副标题
309
+ H.author: 作者
310
+ H.signature: 1=C 4/4
311
+ H.tempo: 94
312
+ ~~~
313
+ \`title\` / \`subtitle\` 居中,分别预设大、小字号;\`author\` 小字号右对齐;\`signature\` 和 \`tempo\` 放在左区。\`H.left:\`、\`H.center:\`、\`H.right:\` 可直接写对应区的零时长内容。以 \`@\` 或 \`{\` 开头的值按 jpFun 解析,其余按字段的文本规则处理。`,
193
314
  allowExtraArgs: false,
194
315
  args: [
195
- { name: "left", type: "content", default: "" },
196
- { name: "center", type: "content", default: "" },
197
- { name: "right", type: "content", default: "" },
198
- { name: "gap", type: "length", default: { value: DEFAULT_GAP_PX, unit: "px" } },
316
+ { name: "left", description: "左区零时长内容,如调性、拍号与速度记号", type: "content", default: "" },
317
+ { name: "center", description: "中区零时长内容,如标题与副标题", type: "content", default: "" },
318
+ { name: "right", description: "右区零时长内容,如作者与署名", type: "content", default: "" },
319
+ { name: "gap", description: "每个区内相邻行的垂直间距", type: "length", default: { value: DEFAULT_GAP_PX, unit: "px" } },
199
320
  ],
200
321
  };
201
322
  slots;
202
323
  size;
203
324
  gap;
204
- members = [];
205
325
  createdBySugar = false;
206
326
  get children() { return this.slots; }
207
327
  timeFlowModel() {
@@ -320,39 +440,8 @@ H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
320
440
  }
321
441
  constructor(span, args, ctx, parent = null) {
322
442
  super(span, parent);
323
- this.size = ctx.fontSize;
443
+ this.size = ctx.variables.fontsize;
324
444
  this.gap = ctx.length2px(this.getArgValue(args, ctx)[3]);
325
- // laneKey 必须区分同一宿主轨上的多个 Head;源码起点是稳定且无需额外存储的身份
326
- const laneKey = `head/rows/${span.start}`;
327
- let titleBottom = 0;
328
- let leftExtent = null;
329
- let rightExtent = null;
330
- const sideTracks = (name) => ({
331
- laneKey: `${laneKey}/${name}`,
332
- hostIndex: null,
333
- measure: members => measureRows(members, this.gap, extent => {
334
- if (name === "left")
335
- leftExtent = extent;
336
- else
337
- rightExtent = extent;
338
- }),
339
- place: () => {
340
- const extent = name === "left" ? leftExtent : rightExtent;
341
- if (!extent)
342
- return titleBottom;
343
- const leftHeight = leftExtent ? leftExtent.bottom - leftExtent.top : 0;
344
- const rightHeight = rightExtent ? rightExtent.bottom - rightExtent.top : 0;
345
- // 两侧共享底边;以较高侧定总高度,使它的顶部恰好贴标题底边
346
- const targetBottom = titleBottom + Math.max(leftHeight, rightHeight);
347
- return targetBottom - extent.bottom;
348
- },
349
- });
350
- const centerMeasure = members => {
351
- const placements = measureRows(members, this.gap);
352
- const first = placements.find(placement => placement !== null);
353
- titleBottom = first ? first.offset + first.extent.bottom : 0;
354
- return placements;
355
- };
356
445
  const rows = (name, index) => {
357
446
  const value = args.get(name) ?? args.get(index);
358
447
  if (!(value instanceof ASTNodeBase))
@@ -360,24 +449,29 @@ H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
360
449
  return value instanceof ASTBraceNode ? value.content : [value];
361
450
  };
362
451
  this.slots = [
363
- new HeadSlotNode(span, this, "left", rows("left", 0), sideTracks("left")),
364
- new HeadSlotNode(span, this, "center", rows("center", 1), {
365
- laneKey: `${laneKey}/center`,
366
- hostIndex: null,
367
- measure: centerMeasure,
368
- }),
369
- new HeadSlotNode(span, this, "right", rows("right", 2), sideTracks("right")),
452
+ new HeadSlotNode(span, this, "left", rows("left", 0)),
453
+ new HeadSlotNode(span, this, "center", rows("center", 1)),
454
+ new HeadSlotNode(span, this, "right", rows("right", 2)),
370
455
  ];
371
456
  }
372
457
  loweringEnter(ctx) {
373
- this.members.length = 0;
374
458
  // 收集真实全局事件:退出时校验时值,放置后按 AST 行归属做横向对齐
375
- ctx.beginLoweringGroup(this, { onTemporal: node => this.members.push(node) });
459
+ const group = {
460
+ members: [],
461
+ onTemporal(node) { this.members.push(node); },
462
+ };
463
+ ctx.beginLoweringGroup(this, group);
376
464
  return [];
377
465
  }
378
466
  loweringExit(ctx) {
379
- ctx.endLoweringGroup(this);
380
- for (const node of this.members) {
467
+ const { members } = ctx.endLoweringGroup(this);
468
+ const head = ctx.getTemporalNodes(this.slots[0])[0];
469
+ head.members = members;
470
+ head.slots = this.slots.map(ast => {
471
+ const [start, end] = ctx.getTemporalNodes(ast);
472
+ return { ast, start, end };
473
+ });
474
+ for (const node of members) {
381
475
  if (!node.T.isZero()) {
382
476
  throw new ErrorDiagnostic("E_HEAD_NONZERO_DURATION", "@head 的内容必须全部为零时长", node.ast.sourceSpan);
383
477
  }
@@ -390,41 +484,6 @@ H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
390
484
  addSugarRow(name, row) {
391
485
  this.slots.find(slot => slot.name === name).addRow(row);
392
486
  }
393
- prepareHead(line) {
394
- if (this.members.some(node => isVisualTemporalNode(node) && node.layoutLine !== line.index)) {
395
- throw new ErrorDiagnostic("E_HEAD_INTERNAL_BREAK", "@head 的内容不能跨谱面行", this.sourceSpan);
396
- }
397
- const [left, center, right] = this.slots;
398
- // 最外侧不受墙的推力,两个点才能落在纸面边缘
399
- left.start.springConfig.alpha_L = 0;
400
- right.end.springConfig.alpha_R = 0;
401
- // 串联弹簧的静长是两端之和、刚度由软端主导,所以只需 center 自己向两侧撑开
402
- this.stretch(center.start, "L");
403
- this.stretch(center.end, "R");
404
- for (const slot of this.slots) {
405
- line.registerHorizontalLayoutHook(slot.start, slot.end, context => layoutHeadSlot(context, slot === center));
406
- }
407
- }
408
- alignHead() {
409
- // 全局列已经完成求解;这里不改 TimeColumn,只覆盖 left/right 主体的最终 box.x
410
- // center 不参与后置重排,继续使用全局列坐标
411
- const cells = new Map();
412
- for (const member of this.members) {
413
- if (!isVisualTemporalNode(member))
414
- continue;
415
- const cell = this.sideCellOf(member.ast);
416
- if (!cell)
417
- continue;
418
- const members = cells.get(cell);
419
- if (members)
420
- members.push(member);
421
- else
422
- cells.set(cell, [member]);
423
- }
424
- const [left, , right] = this.slots;
425
- this.packRows(left, left.start.box.x, "left", cells);
426
- this.packRows(right, right.end.box.x, "right", cells);
427
- }
428
487
  toString(source) {
429
488
  const slots = this.slots
430
489
  .filter(slot => slot.rows.length > 0)
@@ -438,55 +497,5 @@ H.left: / H.center: / H.right: 接受对应槽的任意零时长 DSL 内容
438
497
  slots.push(` gap=${this.gap}px`);
439
498
  return `@head(\n${slots.join(",\n")}\n)`;
440
499
  }
441
- stretch(node, side) {
442
- const alpha = side === "L" ? "alpha_L" : "alpha_R";
443
- const beta = side === "L" ? "beta_L" : "beta_R";
444
- // alpha 撑开槽间空间;beta 反向缩放,避免弹簧刚度随 alpha 一起失控
445
- node.springConfig[alpha] = node.springConfig[alpha] * HEAD_STRETCH;
446
- node.springConfig[beta] = node.springConfig[beta] * 3 / HEAD_STRETCH;
447
- }
448
- packRows(slot, target, side, cells) {
449
- const gap = this.size * ITEM_GAP_EM;
450
- const fromLeft = side === "left";
451
- const direction = fromLeft ? 1 : -1;
452
- for (const row of slot.rows) {
453
- const rowCells = row instanceof ASTBraceNode ? row.content : [row];
454
- let cursor = target;
455
- for (let index = fromLeft ? 0 : rowCells.length - 1; index >= 0 && index < rowCells.length; index += direction) {
456
- const members = cells.get(rowCells[index]);
457
- if (!members)
458
- continue;
459
- let left = Infinity;
460
- let right = -Infinity;
461
- for (const member of members) {
462
- left = Math.min(left, member.box.x);
463
- right = Math.max(right, member.box.x + member.box.w);
464
- }
465
- const offset = cursor - (fromLeft ? left : right);
466
- for (const member of members)
467
- member.box.x += offset;
468
- // 复合单元先整体平移,再让其内部成员读取彼此的最终坐标
469
- for (const member of members)
470
- member.onPlaced?.();
471
- cursor += direction * (right - left + gap);
472
- }
473
- }
474
- }
475
- /** 找到成员所属侧栏行的顶层单元;复合节点内部成员会归回同一个直接子节点 */
476
- sideCellOf(ast) {
477
- let cell = ast;
478
- while (cell.parent) {
479
- const parent = cell.parent;
480
- const slot = parent instanceof HeadSlotNode
481
- ? parent
482
- : parent instanceof ASTBraceNode && parent.parent instanceof HeadSlotNode
483
- ? parent.parent
484
- : null;
485
- if (slot?.owner === this)
486
- return slot.name === "center" ? null : cell;
487
- cell = parent;
488
- }
489
- return null;
490
- }
491
500
  }
492
501
  export const HeadNode = HeadFunction;
@@ -1,10 +1,9 @@
1
- import { DEFAULT_TONALITY, TemporalNodeBase } from "../../lowering/types.js";
1
+ import { DEFAULT_TONALITY, TemporalNodeBase } from "../temporal.js";
2
2
  import { ASTFunctionNode } from "../ASTtypes.js";
3
3
  import { Diagnostic, ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
4
4
  import { parseNoteName } from "../note/noteNameFSM.js";
5
5
  import { acc2Offset, NoteNameMap, tonality2Midi } from "../../parser/parse-utils/note-utils.js";
6
6
  import { paintAccidental, placeAccidentals } from "../note/accidentals.js";
7
- import { JIANPU_NUMBER_FONT } from "../../render/text.js";
8
7
  /**
9
8
  * 把宽松的调性写法收敛成 tonality2Midi 认得的 [音名][最多一个 #b][绝对八度]
10
9
  *
@@ -34,18 +33,22 @@ class KeyFunction extends ASTFunctionNode {
34
33
  static def = {
35
34
  name: ["key", "1"],
36
35
  description: "设置时间线上的 1= 调性基准",
37
- example: `@1(C4) 或 @key(F#3)
38
- 它不会修改 parser 的局部变量,而是在时间固化阶段影响其后的数字音名解释
39
- 画出来的记号只含音名和升降号,不含八度;通常叠在音符上方书写:1 ^ @1(F#)`,
36
+ details: `\
37
+ ~~~jpfun
38
+ 1 ^ @key(F#3)
39
+ ~~~
40
+ 调性沿时间线影响后续数字音名。谱面记号显示音名与升降号,省略八度;通常用 \`^\` 叠在音符上方。`,
40
41
  allowExtraArgs: false,
41
42
  args: [
42
43
  {
43
44
  name: "tonality",
45
+ description: "`1=` 的音高基准,如 `C4`、`F#3`",
44
46
  type: "string",
45
47
  default: null,
46
48
  },
47
49
  {
48
50
  name: "size",
51
+ description: "调性记号字号",
49
52
  type: "length",
50
53
  default: {
51
54
  value: 1,
@@ -56,17 +59,19 @@ class KeyFunction extends ASTFunctionNode {
56
59
  };
57
60
  tonality;
58
61
  size;
62
+ font;
59
63
  /** 只用于绘制:音名与升降号,八度不上谱 */
60
64
  displayName;
61
65
  displayAcc;
62
66
  constructor(sourceSpan, args, ctx, parent = null) {
63
67
  super(sourceSpan, parent);
64
68
  const [tonality, size] = this.getArgValue(args, ctx);
69
+ this.font = ctx.variables.numberfont;
65
70
  this.tonality = tonality;
66
71
  this.size = ctx.length2px(size);
67
72
  const normalized = normalizeTonality(tonality);
68
73
  if (normalized !== tonality) {
69
- if (ctx.strict)
74
+ if (ctx.variables.strict)
70
75
  throw new ErrorDiagnostic("E_KEY_TONALITY", `@key 无法解析调性 "${tonality}"`, sourceSpan);
71
76
  this.tonality = normalized ?? DEFAULT_TONALITY;
72
77
  ctx.diagnostics.push(new WarningDiagnostic("W_KEY_TONALITY", `@key 的调性 "${tonality}" 不是规范写法,已自动切换到 ${this.tonality}`, sourceSpan));
@@ -95,7 +100,7 @@ class KeyTemporalNode extends TemporalNodeBase {
95
100
  this.ast = ast;
96
101
  this.style = {
97
102
  fontSize: ast.size,
98
- fontFamily: JIANPU_NUMBER_FONT,
103
+ fontFamily: ast.font,
99
104
  fill: "#000",
100
105
  };
101
106
  this.initLayoutBox();
@@ -1,27 +1,33 @@
1
1
  import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
2
2
  import { Fraction } from "../../fraction.js";
3
- import { ANCHOR_KEY, TemporalNodeBase } from "../../lowering/types.js";
3
+ import { ANCHOR_KEY, TemporalNodeBase } from "../temporal.js";
4
4
  import { ASTFunctionNode, } from "../ASTtypes.js";
5
- import { JIANPU_NUMBER_FONT } from "../../render/text.js";
6
5
  class MeterFunction extends ASTFunctionNode {
7
6
  static def = {
8
7
  name: ["meter"],
9
8
  description: "设置拍号",
10
- example: `@meter(6, 8) 设置 6/8 拍号`,
9
+ details: `\
10
+ ~~~jpfun
11
+ @meter(6, 8)
12
+ ~~~
13
+ 设置 \`6/8\` 拍号:每小节含六个八分音符的时值`,
11
14
  allowExtraArgs: false,
12
15
  args: [
13
16
  {
14
17
  name: "num",
18
+ description: "拍号分子,每小节包含的单位拍数",
15
19
  type: "number",
16
20
  default: null
17
21
  },
18
22
  {
19
23
  name: "den",
24
+ description: "拍号分母,以几分音符为单位拍",
20
25
  type: "number",
21
26
  default: null
22
27
  },
23
28
  {
24
29
  name: "size",
30
+ description: "拍号字号",
25
31
  type: "length",
26
32
  default: { value: 0.7, unit: "em" },
27
33
  },
@@ -32,13 +38,15 @@ class MeterFunction extends ASTFunctionNode {
32
38
  measureDuration;
33
39
  size;
34
40
  strict;
41
+ font;
35
42
  constructor(span, args, ctx, parent = null) {
36
43
  super(span, parent);
37
44
  let [num, den, size] = this.getArgValue(args, ctx);
45
+ this.font = ctx.variables.numberfont;
38
46
  if (!Number.isSafeInteger(num) || !Number.isSafeInteger(num * 4) || num <= 0
39
47
  || !Number.isSafeInteger(den) || den <= 0) {
40
48
  const message = `@meter 的分子和分母必须是正整数`;
41
- if (ctx.strict)
49
+ if (ctx.variables.strict)
42
50
  throw new ErrorDiagnostic("E_METER_INVALID", message, span);
43
51
  ctx.diagnostics.push(new WarningDiagnostic("W_METER_INVALID", `${message},已回落为 4/4`, span));
44
52
  num = den = 4;
@@ -48,7 +56,7 @@ class MeterFunction extends ASTFunctionNode {
48
56
  // QN 以四分音符为 1;拍号不修改音符自身时值
49
57
  this.measureDuration = new Fraction(num * 4, den);
50
58
  this.size = ctx.length2px(size);
51
- this.strict = ctx.strict;
59
+ this.strict = ctx.variables.strict;
52
60
  }
53
61
  loweringEnter() {
54
62
  return [new MeterTemporal(this)];
@@ -116,7 +124,7 @@ class MeterTemporal extends TemporalNodeBase {
116
124
  this.ast = ast;
117
125
  this.style = {
118
126
  fontSize: ast.size,
119
- fontFamily: JIANPU_NUMBER_FONT,
127
+ fontFamily: ast.font,
120
128
  textAlign: "center",
121
129
  fill: "#000",
122
130
  };