jpfun 0.5.0 → 0.6.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 (41) hide show
  1. package/dist/functions/ASTtypes.d.ts +4 -0
  2. package/dist/functions/ASTtypes.js +25 -14
  3. package/dist/functions/adjust/index.js +6 -2
  4. package/dist/functions/arpeggio/index.js +19 -12
  5. package/dist/functions/beam/auto.js +2 -11
  6. package/dist/functions/box/index.js +52 -1
  7. package/dist/functions/default.js +2 -1
  8. package/dist/functions/div/index.js +2 -3
  9. package/dist/functions/grace/index.d.ts +4 -2
  10. package/dist/functions/grace/index.js +16 -5
  11. package/dist/functions/head/index.js +11 -4
  12. package/dist/functions/meter/index.js +26 -6
  13. package/dist/functions/meter/shape.d.ts +3 -0
  14. package/dist/functions/meter/shape.js +43 -0
  15. package/dist/functions/note/index.js +25 -45
  16. package/dist/functions/rest/index.d.ts +2 -0
  17. package/dist/functions/rest/index.js +233 -0
  18. package/dist/functions/symbol/ornament.js +1 -1
  19. package/dist/functions/temporal.d.ts +21 -7
  20. package/dist/functions/temporal.js +14 -4
  21. package/dist/functions/tie/index.js +14 -10
  22. package/dist/functions/tuplet/index.js +1 -1
  23. package/dist/functions/up/index.js +47 -27
  24. package/dist/functions/voice/connections.d.ts +19 -0
  25. package/dist/functions/voice/connections.js +88 -0
  26. package/dist/functions/voice/index.js +244 -99
  27. package/dist/jpfun.core.min.js +56 -39
  28. package/dist/jpfun.min.js +65 -48
  29. package/dist/layout/engine.d.ts +3 -1
  30. package/dist/layout/engine.js +190 -64
  31. package/dist/layout/model.js +22 -66
  32. package/dist/layout/page.js +1 -1
  33. package/dist/layout/types.d.ts +30 -45
  34. package/dist/lowering/loweringContext.d.ts +1 -1
  35. package/dist/lowering/loweringContext.js +34 -4
  36. package/dist/parser/parserContext.js +4 -8
  37. package/dist/parser/preprocess.js +4 -6
  38. package/dist/playback/compile.js +1 -1
  39. package/dist/render/paint.js +1 -1
  40. package/grammar.md +200 -257
  41. package/package.json +1 -1
@@ -64,6 +64,8 @@ export declare class ASTBraceNode extends ASTNodeBase {
64
64
  }
65
65
  export interface FunctionArgDef {
66
66
  name?: string;
67
+ /** 仅接受命名传参,不占用位置参数序号 */
68
+ namedOnly?: boolean;
67
69
  description?: string;
68
70
  type: paramType;
69
71
  /** 参数默认值 null表示必填 否则报错 */
@@ -77,6 +79,8 @@ export interface FunctionDef {
77
79
  extraArgType?: paramType;
78
80
  args: FunctionArgDef[];
79
81
  }
82
+ /** 按名称或位置查找参数,位置查找跳过仅命名参数 */
83
+ export declare function resolveArgDef(def: FunctionDef, name: string | undefined, index: number): FunctionArgDef | undefined;
80
84
  /** 参数名 -> 类型的规则:命名参数只认 def.args,位置参数可回落到 extraArgType */
81
85
  export declare function resolveArgType(def: FunctionDef, name: string | undefined, index: number): paramType | undefined;
82
86
  /** 别名里的第一个是主名;`@set` 的 key 和 `getArgValue` 的前缀必须用同一个 */
@@ -97,11 +97,22 @@ export class ASTBraceNode extends ASTNodeBase {
97
97
  return `{${this.content.map(item => item.toString(source)).join("")}}`;
98
98
  }
99
99
  }
100
+ /** 按名称或位置查找参数,位置查找跳过仅命名参数 */
101
+ export function resolveArgDef(def, name, index) {
102
+ if (name) {
103
+ const key = name.toLowerCase();
104
+ return def.args.find(arg => arg.name?.toLowerCase() === key);
105
+ }
106
+ for (const arg of def.args) {
107
+ if (!arg.namedOnly && index-- === 0)
108
+ return arg;
109
+ }
110
+ return undefined;
111
+ }
100
112
  /** 参数名 -> 类型的规则:命名参数只认 def.args,位置参数可回落到 extraArgType */
101
113
  export function resolveArgType(def, name, index) {
102
- if (name)
103
- return def.args.find(arg => arg.name?.toLowerCase() === name)?.type;
104
- return def.args[index]?.type ?? (def.allowExtraArgs ? def.extraArgType : undefined);
114
+ return resolveArgDef(def, name, index)?.type
115
+ ?? (!name && def.allowExtraArgs ? def.extraArgType : undefined);
105
116
  }
106
117
  /** 别名里的第一个是主名;`@set` 的 key 和 `getArgValue` 的前缀必须用同一个 */
107
118
  export function primaryName(def) {
@@ -126,23 +137,23 @@ export class ASTFunctionNode extends ASTNodeBase {
126
137
  get deSugarAtom() { return this.constructor.deSugarAtom; }
127
138
  static deSugarRelation = () => null;
128
139
  get deSugarRelation() { return this.constructor.deSugarRelation; }
129
- // 通用的参数提取方法 从定义找传参
140
+ // 按声明顺序取值,位置编号独立于仅命名参数
130
141
  getArgValue(args, ctx) {
131
142
  const def = this.def;
132
143
  if (!def)
133
144
  return [];
134
- const defArgs = def.args;
135
145
  const prefix = primaryName(def);
136
- return defArgs.map((argDef, index) => {
137
- let argNameL = argDef.name ? argDef.name.toLowerCase() : null; // 统一小写处理
138
- // 先查询是否传递
139
- let argValue = (argNameL ? args.get(argNameL) : null)
140
- ?? args.get(index) // 优先使用命名参数,否则使用位置参数
141
- ?? (argNameL ? ctx.variables[`${prefix}.${argNameL}`.toLowerCase()] : null)
146
+ let position = 0;
147
+ return def.args.map((argDef, declaredIndex) => {
148
+ const index = argDef.namedOnly ? undefined : position++;
149
+ const name = argDef.name?.toLowerCase();
150
+ const value = (name ? args.get(name) : undefined)
151
+ ?? (index === undefined ? undefined : args.get(index))
152
+ ?? (name ? ctx.variables[`${prefix}.${name}`.toLowerCase()] : undefined)
142
153
  ?? argDef.default;
143
- if (argValue === null)
144
- throw Diagnostic.error.MissingArg(prefix, argDef.name || index, this.sourceSpan);
145
- return argValue; // 假设解析器已经保证了类型正确
154
+ if (value === null)
155
+ throw Diagnostic.error.MissingArg(prefix, argDef.name || (index ?? declaredIndex), this.sourceSpan);
156
+ return value;
146
157
  });
147
158
  }
148
159
  toString(source) {
@@ -106,6 +106,9 @@ function offsetTemporal(node, owner) {
106
106
  const { dx, dy, dw, dh } = owner;
107
107
  if (dx === 0 && dy === 0 && dw === 0 && dh === 0)
108
108
  return;
109
+ if (dx !== 0 || dy !== 0) {
110
+ node.placementOffset = { x: (node.placementOffset?.x ?? 0) + dx, y: (node.placementOffset?.y ?? 0) + dy };
111
+ }
109
112
  let placedX;
110
113
  let placedY;
111
114
  const finalize = node.finalizeLayout;
@@ -134,12 +137,13 @@ function offsetTemporal(node, owner) {
134
137
  * 作用域里只要有对象被平移过,关系对象就已经跟着端点走了,这里不能再平移第二次
135
138
  */
136
139
  function offsetAttachment(attachment, targets, dx, dy) {
137
- if (dx === 0 && dy === 0)
140
+ if (targets.temporal > 0 || (dx === 0 && dy === 0))
138
141
  return;
142
+ attachment.placementOffset = { x: (attachment.placementOffset?.x ?? 0) + dx, y: (attachment.placementOffset?.y ?? 0) + dy };
139
143
  const createGeometry = attachment.createGeometry;
140
144
  attachment.createGeometry = (context) => {
141
145
  const geometry = createGeometry.call(attachment, context);
142
- if (targets.temporal > 0)
146
+ if (targets.temporal > 0 || context.layoutOnly)
143
147
  return geometry;
144
148
  const shift = (region) => region.line === void 0
145
149
  ? { x: region.x + dx, y: region.y + dy, w: region.w, h: region.h }
@@ -1,6 +1,6 @@
1
1
  import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
2
2
  import { Fraction } from "../../fraction.js";
3
- import { prepareLayoutHost } from "../../layout/engine.js";
3
+ import { getLayoutBounds, prepareLayoutHost } from "../../layout/engine.js";
4
4
  import { ASTFunctionNode, } from "../ASTtypes.js";
5
5
  import { TemporalNodeBase } from "../temporal.js";
6
6
  import { prepareArpeggioShape } from "./shape.js";
@@ -58,13 +58,12 @@ class ArpeggioFunction extends ASTFunctionNode {
58
58
  content.parent = this;
59
59
  }
60
60
  loweringEnter(ctx, track) {
61
- let host = null;
62
- ctx.isolateFromLoweringGroups(() => {
61
+ const host = ctx.isolateFromLoweringGroups(() => {
63
62
  const events = ctx.trackedEvents(this.content, new Fraction(), track).flat();
64
63
  if (events.length !== 1 || !events[0].box) {
65
64
  throw new ErrorDiagnostic("E_ARPEGGIO_INVALID_CONTENT", "@arp 的内容必须恰好产生一个同轨和弦", this.content.sourceSpan);
66
65
  }
67
- host = events[0];
66
+ return events[0];
68
67
  });
69
68
  const fold = readFold(host);
70
69
  if (!fold || fold.members.length < 2 || fold.members.some(member => member.track !== track)) {
@@ -108,7 +107,12 @@ class ArpeggioTemporal extends TemporalNodeBase {
108
107
  this.host.T.copyFrom(this.T);
109
108
  this.host.track = this.track;
110
109
  this.host.layoutLine = this.layoutLine;
111
- this.host.onTimeState?.(state);
110
+ const offset = this.host.onTimeState?.(state);
111
+ this.T.copyFrom(this.host.T);
112
+ return offset;
113
+ }
114
+ prepareHorizontal(line) {
115
+ this.host.prepareHorizontal?.(line);
112
116
  }
113
117
  prepareLayout(context) {
114
118
  if (this.addon) {
@@ -116,24 +120,27 @@ class ArpeggioTemporal extends TemporalNodeBase {
116
120
  this.addon = void 0;
117
121
  }
118
122
  prepareLayoutHost(this.host, context);
123
+ this.springConfig = { ...this.host.springConfig, ...this.springConfig };
119
124
  const em = this.ast.size;
120
125
  const gap = em * 0.16;
121
- const { commands, bounds: markBounds } = prepareArpeggioShape(this.host.box.h, em, this.ast.direction);
126
+ const contentBounds = getLayoutBounds(this.host, true);
127
+ const bounds = getLayoutBounds(this.host);
128
+ const { commands, bounds: markBounds } = prepareArpeggioShape(contentBounds.h, em, this.ast.direction);
122
129
  this.markCommands = commands;
123
130
  this.markX = -markBounds.x;
124
131
  this.hostX = markBounds.w + gap;
125
132
  this.box.w = this.hostX + this.host.box.w;
126
- this.box.h = this.host.box.h;
133
+ this.box.h = bounds.h;
127
134
  this.box.anchor = this.hostX + this.host.box.anchor;
128
- this.box.visualAxis = this.host.box.visualAxis;
135
+ this.box.visualAxis = this.host.box.visualAxis - bounds.y;
129
136
  for (const name in this.host.ports) {
130
137
  const port = this.host.ports[name];
131
- this.ports[name] = { x: this.hostX + port.x, y: port.y };
138
+ this.ports[name] = { x: this.hostX + port.x, y: port.y - bounds.y };
132
139
  }
133
140
  }
134
141
  onPlaced() {
135
142
  this.host.box.x = this.box.x + this.hostX;
136
- this.host.box.y = this.box.y;
143
+ this.host.box.y = this.box.y - (getLayoutBounds(this.host).y - this.host.box.y);
137
144
  this.host.onPlaced?.();
138
145
  }
139
146
  /**
@@ -181,12 +188,12 @@ class ArpeggioTemporal extends TemporalNodeBase {
181
188
  }
182
189
  paint(painter) {
183
190
  const originX = this.box.x;
184
- const originY = this.box.y;
191
+ const originY = getLayoutBounds(this.host, true).y;
185
192
  painter.drawPath(this.markCommands, { fill: "#000" }, {
186
193
  x: originX + this.markX, y: originY, scaleX: 1, scaleY: 1,
187
194
  });
188
195
  this.host.paint(painter);
189
196
  for (const decoration of this.host.decorations)
190
- decoration.paint(painter);
197
+ decoration.paint?.(painter);
191
198
  }
192
199
  }
@@ -181,17 +181,8 @@ function createAdjacentBeams(result, nodeScopes, explicitEndpoints) {
181
181
  const activeStart = measureStart.compare(lineStart) >= 0 ? measureStart : lineStart;
182
182
  const beat = beatAt(beatTime.copyFrom(node.t).sub(activeStart));
183
183
  const current = groups.get(node.track);
184
- if (!current) {
185
- groups.set(node.track, {
186
- nodes: [node],
187
- line: node.layoutLine,
188
- beat,
189
- endTime: node.t.clone().add(node.T),
190
- });
191
- continue;
192
- }
193
- const isContinuous = node.t.equals(current.endTime);
194
- if (node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
184
+ const isContinuous = current && node.t.equals(current.endTime);
185
+ if (!current || node.layoutLine !== current.line || beat !== current.beat || !isContinuous) {
195
186
  flush(node.track);
196
187
  groups.set(node.track, {
197
188
  nodes: [node],
@@ -2,6 +2,7 @@ import { ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { isVisualTemporalNode } from "../temporal.js";
3
3
  import { ErrorDiagnostic } from "../../diagnostic.js";
4
4
  import { layoutHorizontalRegion } from "../../layout/model.js";
5
+ import { isLayoutAttachment } from "../../layout/types.js";
5
6
  class BoxFunction extends ASTFunctionNode {
6
7
  static def = {
7
8
  name: ["box"],
@@ -79,6 +80,14 @@ class BoxFunction extends ASTFunctionNode {
79
80
  ctx.addAttachment(attachment);
80
81
  const first = content.nodes[0];
81
82
  if (first) {
83
+ if (attachment.reservedHost) {
84
+ const prepareLayout = first.prepareLayout;
85
+ first.prepareLayout = context => {
86
+ prepareLayout.call(first, context);
87
+ const space = { order: Infinity, height: Math.max(0, this.padding + this.stroke / 2) };
88
+ first.decorations.push({ above: space, below: space });
89
+ };
90
+ }
82
91
  const prepare = first.prepareHorizontal;
83
92
  first.prepareHorizontal = line => {
84
93
  prepare?.call(first, line);
@@ -105,12 +114,18 @@ class BoxLayoutAttachment {
105
114
  content;
106
115
  owner;
107
116
  layer = "background";
117
+ reservedHost;
108
118
  fixedStart = null;
109
119
  wallOffset = 0;
110
120
  get sourceSpan() { return this.owner.sourceSpan; }
111
121
  constructor(content, owner) {
112
122
  this.content = content;
113
123
  this.owner = owner;
124
+ const [host] = content.nodes;
125
+ // 后测关系可能超出主体,只有已内含的嵌套框能提前预留完整空间
126
+ if (content.nodes.length === 1 && content.attachments.every(attachment => attachment instanceof BoxLayoutAttachment && attachment.reservedHost === host)) {
127
+ this.reservedHost = host;
128
+ }
114
129
  }
115
130
  /** 只在完整包含成员的视图注册;留白和定宽共用同一个由内向外执行的 hook */
116
131
  registerHorizontal(line) {
@@ -182,7 +197,7 @@ class BoxLayoutAttachment {
182
197
  return { regions: [], paint() { } };
183
198
  }
184
199
  const inset = padding + stroke / 2;
185
- // 边框不抢轨道纵向空间,只参与画布边界
200
+ // 单主体已提前预留空间,范围框在最终测量时申报外围占用
186
201
  const fixedX = this.fixedStart
187
202
  ? this.fixedStart.box.x + this.fixedStart.box.anchor + this.wallOffset
188
203
  : rect.x;
@@ -197,6 +212,7 @@ class BoxLayoutAttachment {
197
212
  }
198
213
  return {
199
214
  regions: [region],
215
+ occupancy: this.reservedHost ? [] : this.occupancy(context, region),
200
216
  paint(painter) {
201
217
  const strokeInset = stroke / 2;
202
218
  painter.drawRect(region.x + strokeInset, region.y + strokeInset, Math.max(0, region.w - stroke), Math.max(0, region.h - stroke), {
@@ -206,4 +222,39 @@ class BoxLayoutAttachment {
206
222
  },
207
223
  };
208
224
  }
225
+ occupancy(context, region) {
226
+ const owners = new Set();
227
+ let line;
228
+ const include = (index, track) => {
229
+ if (line !== undefined && line !== index) {
230
+ throw new ErrorDiagnostic("E_BOX_CROSS_LINE", "@box 的内容不能跨越谱面行", this.owner.sourceSpan);
231
+ }
232
+ line = index;
233
+ owners.add(track);
234
+ };
235
+ for (const node of this.content.nodes)
236
+ include(node.layoutLine, node.track);
237
+ for (const attachment of this.content.attachments) {
238
+ if (!isLayoutAttachment(attachment))
239
+ continue;
240
+ const occupancy = context.getAttachmentOccupancy(attachment);
241
+ if (!occupancy)
242
+ continue;
243
+ for (const point of attachment.endPoints ?? [])
244
+ include(point.layoutLine, point.track);
245
+ for (const part of occupancy) {
246
+ if (part.line !== undefined)
247
+ include(part.line, part.track);
248
+ }
249
+ }
250
+ if (line === undefined)
251
+ return [];
252
+ const index = line;
253
+ const sorted = [...owners].sort((left, right) => context.getVisualAxis(index, left) - context.getVisualAxis(index, right));
254
+ // 整框只申报外围上下沿,不能把内部轨距重复计入每条轨道
255
+ return [
256
+ { ...region, h: 0, line: index, track: sorted[0] },
257
+ { ...region, y: region.y + region.h, h: 0, line: index, track: sorted.at(-1) },
258
+ ];
259
+ }
209
260
  }
@@ -13,6 +13,7 @@ import { KeyNode } from "./key/index.js";
13
13
  import { MeterNode } from "./meter/index.js";
14
14
  import { BrNode } from "./br/index.js";
15
15
  import { NoteNode } from "./note/index.js";
16
+ import { RestNode } from "./rest/index.js";
16
17
  import { UpNode, DownNode } from "./up/index.js";
17
18
  import { PageNode } from "./page/index.js";
18
19
  import { ProgramNode } from "./program/index.js";
@@ -26,7 +27,7 @@ import { TieNode } from "./tie/index.js";
26
27
  import { TupletNode } from "./tuplet/index.js";
27
28
  import { VoiceNode, VoicesNode } from "./voice/index.js";
28
29
  export const defaultFunctions = [
29
- NoteNode, DashNode, BarNode, // 有实体
30
+ NoteNode, DashNode, RestNode, BarNode,
30
31
  DivNode, DotNode, TupletNode, // 装饰性
31
32
  VoiceNode, VoicesNode, // 歌词和声部
32
33
  BrNode, // 排版
@@ -135,11 +135,10 @@ class DivFunction extends ASTFunctionNode {
135
135
  let lineRight = 0;
136
136
  let firstLine = 0;
137
137
  return {
138
- // 减时线声明为靠近主体的下方项
139
- // 引擎只按通用 order 排列,不识别 @div
140
138
  below: {
141
139
  order: 0,
142
- gap: -host.ast.size * 0.08,
140
+ // 画线沿用正文盒基准,间距换算到装饰排列起点。
141
+ gap: host.box.h - (host.ports["decoration.below"]?.y ?? host.box.h) - host.ast.size * 0.08,
143
142
  height: lineBlockHeight,
144
143
  place(y) {
145
144
  lineLeft = host.ports["body.left"]?.x ?? 0;
@@ -1,6 +1,7 @@
1
- import type { LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
1
+ import type { HorizontalLineView, LayoutBox, LayoutPrepareContext } from "../../layout/types.js";
2
2
  import type { LoweringContext } from "../../lowering/loweringContext.js";
3
3
  import type { LoweringAttachment } from "../../lowering/types.js";
4
+ import { Fraction } from "../../fraction.js";
4
5
  import { Track } from "../../lowering/track.js";
5
6
  import { TemporalNodeBase, type TimeState, type VisualTemporalNode } from "../temporal.js";
6
7
  import { GrammarNode, GrammarSugarNode } from "../../parser/grammarType.js";
@@ -64,7 +65,8 @@ export declare class GraceTemporal extends TemporalNodeBase {
64
65
  private hookOrigin;
65
66
  constructor(ast: GraceFunction, host: VisualTemporalNode, columns: readonly (readonly TemporalNodeBase[])[], root: Track, attachments: readonly LoweringAttachment[], side: GraceSide);
66
67
  /** 成员共享全局时间状态,按发声顺序固化 */
67
- onTimeState(state: TimeState): void;
68
+ onTimeState(state: TimeState): void | Fraction;
69
+ prepareHorizontal(line: HorizontalLineView): void;
68
70
  /**
69
71
  * 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
70
72
  *
@@ -1,5 +1,5 @@
1
1
  import { ErrorDiagnostic } from "../../diagnostic.js";
2
- import { layoutLocalSequence } from "../../layout/engine.js";
2
+ import { getLayoutBounds, layoutLocalSequence } from "../../layout/engine.js";
3
3
  import { Fraction } from "../../fraction.js";
4
4
  import { Track } from "../../lowering/track.js";
5
5
  import { isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
@@ -354,11 +354,20 @@ export class GraceTemporal extends TemporalNodeBase {
354
354
  onTimeState(state) {
355
355
  // 同步时间的修改。比如tuplet修改的是GraceTemporal.T,这里要把它传给成员
356
356
  this.host.T.copyFrom(this.T);
357
+ let offset = undefined;
357
358
  for (const member of this.side === "pre" ? [...this.events, this.host] : [this.host, ...this.events]) {
358
359
  member.t.copyFrom(this.t);
359
360
  member.layoutLine = this.layoutLine;
360
- member.onTimeState?.(state);
361
+ const result = member.onTimeState?.(state);
362
+ if (member === this.host) {
363
+ this.T.copyFrom(member.T);
364
+ offset = result;
365
+ }
361
366
  }
367
+ return offset;
368
+ }
369
+ prepareHorizontal(line) {
370
+ this.host.prepareHorizontal?.(line);
362
371
  }
363
372
  /**
364
373
  * 播放时倚音从宿主借走的时值(前倚音从开头、后倚音从末尾)
@@ -405,6 +414,7 @@ export class GraceTemporal extends TemporalNodeBase {
405
414
  }
406
415
  const block = context.layoutSubdomain(this, this.graceColumns, this.root, this.attachments);
407
416
  const hostWidth = layoutLocalSequence([this.host], context);
417
+ this.springConfig = { ...this.host.springConfig, ...this.springConfig };
408
418
  const graceEm = this.ast.size * GRACE_SCALE;
409
419
  const sideGap = graceEm * 0.2;
410
420
  const rise = graceEm * GRACE_RISE;
@@ -412,7 +422,8 @@ export class GraceTemporal extends TemporalNodeBase {
412
422
  // 肩线由它转发上来,所以两侧倚音会落在同一高度而不是层层叠高
413
423
  const shoulder = this.host.ports[SHOULDER_PORT]?.y ?? 0;
414
424
  const graceTop = shoulder - rise - block.height;
415
- const lift = Math.max(0, -graceTop);
425
+ const hostBounds = getLayoutBounds(this.host);
426
+ const lift = Math.max(0, -graceTop, -hostBounds.y);
416
427
  const graceX = this.side === "pre" ? 0 : hostWidth + sideGap;
417
428
  this.hostOffset = {
418
429
  x: this.host.box.x + (this.side === "pre" ? block.width + sideGap : 0),
@@ -423,7 +434,7 @@ export class GraceTemporal extends TemporalNodeBase {
423
434
  y: graceTop + lift + position.y,
424
435
  }));
425
436
  this.box.w = hostWidth + sideGap + block.width;
426
- this.box.h = lift + this.host.box.h;
437
+ this.box.h = lift + hostBounds.y + hostBounds.h;
427
438
  this.box.anchor = this.hostOffset.x + this.host.box.anchor;
428
439
  this.box.visualAxis = lift + this.host.box.visualAxis;
429
440
  // 端口原样上提:减时线、歌词、连音线都接到宿主上,关系函数不需要认识倚音
@@ -460,7 +471,7 @@ export class GraceTemporal extends TemporalNodeBase {
460
471
  for (const member of [this.host, ...this.graces]) {
461
472
  member.paint(painter);
462
473
  for (const decoration of member.decorations)
463
- decoration.paint(painter);
474
+ decoration.paint?.(painter);
464
475
  }
465
476
  if (!this.hookOrigin)
466
477
  return;
@@ -82,6 +82,13 @@ class HeadBoundaryTemporal extends TemporalNodeBase {
82
82
  this.ast = ast;
83
83
  this.initLayoutBox();
84
84
  }
85
+ validateDuration() {
86
+ for (const node of this.members) {
87
+ if (!node.T.isZero()) {
88
+ throw new ErrorDiagnostic("E_HEAD_NONZERO_DURATION", "@head 的内容必须全部为零时长", node.ast.sourceSpan);
89
+ }
90
+ }
91
+ }
85
92
  prepareLayout() { this.box.w = this.box.h = this.box.anchor = this.box.visualAxis = 0; }
86
93
  prepareHorizontal(line) {
87
94
  // 只由第一个边界协调一次整块 Head,避免六个边界重复改弹簧
@@ -455,7 +462,7 @@ H.tempo: 94
455
462
  ];
456
463
  }
457
464
  loweringEnter(ctx) {
458
- // 收集真实全局事件:退出时校验时值,放置后按 AST 行归属做横向对齐
465
+ // 收集真实全局事件,供末边界校验时值和放置后的横向对齐
459
466
  const group = {
460
467
  members: [],
461
468
  onTemporal(node) { this.members.push(node); },
@@ -471,10 +478,10 @@ H.tempo: 94
471
478
  const [start, end] = ctx.getTemporalNodes(ast);
472
479
  return { ast, start, end };
473
480
  });
481
+ // right.end 是整个 Head 的末边界,按三槽的 sequence 排在所有内容之后
482
+ // 此时 rest 等延迟时值已固化,只检查当前 Head 的成员,无须再扫描全谱索引
483
+ head.slots[2].end.onTimeState = () => head.validateDuration();
474
484
  for (const node of members) {
475
- if (!node.T.isZero()) {
476
- throw new ErrorDiagnostic("E_HEAD_NONZERO_DURATION", "@head 的内容必须全部为零时长", node.ast.sourceSpan);
477
- }
478
485
  if (node.breakBefore > 0) {
479
486
  throw new ErrorDiagnostic("E_HEAD_INTERNAL_BREAK", "@head 的内部不能使用 @br;第一层内容已经表示视觉行", node.ast.sourceSpan);
480
487
  }
@@ -1,6 +1,8 @@
1
1
  import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
2
2
  import { Fraction } from "../../fraction.js";
3
3
  import { ANCHOR_KEY, TemporalNodeBase } from "../temporal.js";
4
+ import { paintSymbol } from "../symbol/shape.js";
5
+ import { sanbanBounds, sanbanShapes } from "./shape.js";
4
6
  import { ASTFunctionNode, } from "../ASTtypes.js";
5
7
  class MeterFunction extends ASTFunctionNode {
6
8
  static def = {
@@ -10,7 +12,8 @@ class MeterFunction extends ASTFunctionNode {
10
12
  ~~~jpfun
11
13
  @meter(6, 8)
12
14
  ~~~
13
- 设置 \`6/8\` 拍号:每小节含六个八分音符的时值`,
15
+ 设置 \`6/8\` 拍号:每小节含六个八分音符的时值。
16
+ 分子或分母为 0 时表示散板。`,
14
17
  allowExtraArgs: false,
15
18
  args: [
16
19
  {
@@ -43,9 +46,9 @@ class MeterFunction extends ASTFunctionNode {
43
46
  super(span, parent);
44
47
  let [num, den, size] = this.getArgValue(args, ctx);
45
48
  this.font = ctx.variables.numberfont;
46
- if (!Number.isSafeInteger(num) || !Number.isSafeInteger(num * 4) || num <= 0
47
- || !Number.isSafeInteger(den) || den <= 0) {
48
- const message = `@meter 的分子和分母必须是正整数`;
49
+ if (!Number.isSafeInteger(num) || !Number.isSafeInteger(num * 4) || num < 0
50
+ || !Number.isSafeInteger(den) || den < 0) {
51
+ const message = `@meter 的分子和分母必须是非负整数`;
49
52
  if (ctx.variables.strict)
50
53
  throw new ErrorDiagnostic("E_METER_INVALID", message, span);
51
54
  ctx.diagnostics.push(new WarningDiagnostic("W_METER_INVALID", `${message},已回落为 4/4`, span));
@@ -54,7 +57,8 @@ class MeterFunction extends ASTFunctionNode {
54
57
  this.numerator = num;
55
58
  this.denominator = den;
56
59
  // QN 以四分音符为 1;拍号不修改音符自身时值
57
- this.measureDuration = new Fraction(num * 4, den);
60
+ if (num !== 0 && den !== 0)
61
+ this.measureDuration = new Fraction(num * 4, den);
58
62
  this.size = ctx.length2px(size);
59
63
  this.strict = ctx.variables.strict;
60
64
  }
@@ -73,7 +77,7 @@ class MeterFunction extends ASTFunctionNode {
73
77
  // 诊断范围从上一边界之后延伸到当前关闭边界
74
78
  let sourceStart = 0;
75
79
  const closeMeasure = (end, sourceEnd) => {
76
- if (!active || end.equals(measureStart))
80
+ if (!active?.measureDuration || end.equals(measureStart))
77
81
  return;
78
82
  // Fraction 运算会原地修改接收者,不能直接对时间线字段做减法
79
83
  measureLength.copyFrom(end).sub(measureStart);
@@ -143,7 +147,12 @@ class MeterTemporal extends TemporalNodeBase {
143
147
  };
144
148
  this.initLayoutBox();
145
149
  }
150
+ onTimeState(state) {
151
+ state.meter = this.ast.measureDuration;
152
+ }
146
153
  emitPlayback(emitter) {
154
+ if (!this.ast.measureDuration)
155
+ return;
147
156
  emitter.emit({
148
157
  kind: "time-signature",
149
158
  at: emitter.start,
@@ -152,6 +161,13 @@ class MeterTemporal extends TemporalNodeBase {
152
161
  });
153
162
  }
154
163
  prepareLayout(context) {
164
+ if (!this.ast.measureDuration) {
165
+ this.box.w = this.ast.size * sanbanBounds.w / sanbanBounds.h;
166
+ this.box.h = this.ast.size;
167
+ this.box.anchor = this.box.w / 2;
168
+ this.box.visualAxis = this.box.h / 2;
169
+ return;
170
+ }
155
171
  const num = context.textMeasurer.measureText(String(this.ast.numerator), this.style);
156
172
  const den = context.textMeasurer.measureText(String(this.ast.denominator), this.style);
157
173
  const gap = this.ast.size * 0.08;
@@ -167,6 +183,10 @@ class MeterTemporal extends TemporalNodeBase {
167
183
  this.box.visualAxis = this.lineY + this.lineHeight / 2;
168
184
  }
169
185
  paint(painter) {
186
+ if (!this.ast.measureDuration) {
187
+ paintSymbol(painter, sanbanShapes, sanbanBounds, this.box, this.ast.size / sanbanBounds.h);
188
+ return;
189
+ }
170
190
  const center = this.box.x + this.box.anchor;
171
191
  painter.drawText(String(this.ast.numerator), center, this.box.y + this.numeratorBaseline, this.style);
172
192
  painter.drawRect(this.box.x, this.box.y + this.lineY, this.box.w, this.lineHeight, { fill: "#000" });
@@ -0,0 +1,3 @@
1
+ import { type SymbolShape } from "../symbol/shape.js";
2
+ export declare const sanbanShapes: readonly SymbolShape[];
3
+ export declare const sanbanBounds: import("../../core.js").Rect;
@@ -0,0 +1,43 @@
1
+ import { symbolBounds } from "../symbol/shape.js";
2
+ export const sanbanShapes = [
3
+ {
4
+ path: [
5
+ { op: "M", x: 1, y: 34 },
6
+ { op: "C", cx1: 23, cy1: 36, cx2: 57, cy2: 28, x: 82, y: 23 },
7
+ { op: "C", cx1: 91, cy1: 21, cx2: 98, cy2: 24, x: 102, y: 29 },
8
+ { op: "Q", cx: 106, cy: 34, x: 98, y: 34 },
9
+ { op: "C", cx1: 68, cy1: 30, cx2: 37, cy2: 39, x: 13, y: 43 },
10
+ { op: "Q", cx: 7, cy: 44, x: 1, y: 39 },
11
+ { op: "Q", cx: -3, cy: 35, x: 1, y: 34 },
12
+ { op: "Z" },
13
+ ],
14
+ style: { fill: "#000" },
15
+ },
16
+ {
17
+ path: [
18
+ { op: "M", x: 29, y: 5 },
19
+ { op: "Q", cx: 27, cy: 3, x: 31, y: 4 },
20
+ { op: "Q", cx: 44, cy: 7, x: 42, y: 12 },
21
+ { op: "C", cx1: 39, cy1: 26, cx2: 43, cy2: 43, x: 42, y: 60 },
22
+ { op: "Q", cx: 41, cy: 69, x: 38, y: 62 },
23
+ { op: "C", cx1: 34, cy1: 47, cx2: 35, cy2: 20, x: 29, y: 5 },
24
+ { op: "Z" },
25
+ ],
26
+ style: { fill: "#000" },
27
+ },
28
+ {
29
+ path: [
30
+ { op: "M", x: 59, y: 1 },
31
+ { op: "Q", cx: 56, cy: -2, x: 61, y: 0 },
32
+ { op: "Q", cx: 76, cy: 4, x: 74, y: 9 },
33
+ { op: "C", cx1: 71, cy1: 17, cx2: 74, cy2: 32, x: 69, y: 48 },
34
+ { op: "C", cx1: 64, cy1: 66, cx2: 55, cy2: 77, x: 40, y: 84 },
35
+ { op: "Q", cx: 37, cy: 85, x: 40, y: 82 },
36
+ { op: "C", cx1: 57, cy1: 66, cx2: 63, cy2: 47, x: 64, y: 29 },
37
+ { op: "C", cx1: 65, cy1: 17, cx2: 63, cy2: 7, x: 59, y: 1 },
38
+ { op: "Z" },
39
+ ],
40
+ style: { fill: "#000" },
41
+ },
42
+ ];
43
+ export const sanbanBounds = symbolBounds(sanbanShapes);