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
@@ -21,7 +21,7 @@ export interface Extent {
21
21
  bottom: number;
22
22
  }
23
23
  /**
24
- * 一个排版对象的完整矩形和两个对齐基准
24
+ * 一个排版对象的定位矩形和两个对齐基准
25
25
  *
26
26
  * prepareLayout 负责填写 w、h、anchor、visualAxis
27
27
  * layout 负责原地修改 x、y
@@ -88,6 +88,8 @@ export interface SubdomainLayout {
88
88
  export type LayoutRange = readonly [from: number, to: number] | readonly [from: LayoutHost, to: LayoutHost];
89
89
  /** attachment 根据当前视觉轴生成几何时需要的完整页面信息 */
90
90
  export interface AttachmentLayoutContext extends LayoutPrepareContext {
91
+ /** 占用测量使用布局位置,最终绘制才应用后置平移 */
92
+ readonly layoutOnly?: boolean;
91
93
  width: number;
92
94
  originX: number;
93
95
  pages: readonly Rect[];
@@ -111,6 +113,8 @@ export interface AttachmentLayoutContext extends LayoutPrepareContext {
111
113
  getRangeExtents(line: number, columns: LayoutRange | undefined, track: Track): Readonly<Extent> | undefined;
112
114
  /** 读取本轮已完成的 attachment 边界;分组在 endLoweringGroup 才注册,因而组内对象必然排在分组之前 */
113
115
  getAttachmentBox(attachment: LayoutAttachment): Readonly<Rect>;
116
+ /** 读取已测附件的有效占用,保留显式空数组;尚未测量时返回 undefined */
117
+ getAttachmentOccupancy(attachment: LayoutAttachment): readonly LayoutRegion[] | undefined;
114
118
  /** 内容的实际几何并集;只包含作用域内主体与此前已测附件,不代表撑行占用 */
115
119
  getContentBounds(content: LoweringContent): Readonly<Rect> | undefined;
116
120
  }
@@ -126,6 +130,10 @@ export interface AttachmentLayoutContext extends LayoutPrepareContext {
126
130
  export interface LayoutHost extends TimeLineEvent {
127
131
  box: LayoutBox;
128
132
  springConfig: HorizontalSpringConfig;
133
+ /**
134
+ * 内置端口:body.left / body.right、decoration.below、shoulder、tie.top、
135
+ * div.{level}.left / div.{level}.right、dot、lyric;用途与缺省值见 {@link TemporalNodeBase.ports}。
136
+ */
129
137
  ports: Record<string, LayoutPoint>;
130
138
  readonly mergeKey: number;
131
139
  readonly layoutLine: number;
@@ -153,7 +161,7 @@ export interface HorizontalLineView {
153
161
  }
154
162
  /**
155
163
  * 相对于对象 LayoutBox 左上角的局部坐标
156
- * tie、beam 等关系函数通过命名端口获取几何位置
164
+ * 装饰布局与 tie、beam 等关系函数通过命名端口获取几何位置
157
165
  */
158
166
  export interface LayoutPoint {
159
167
  x: number;
@@ -167,58 +175,33 @@ context: LayoutPrepareContext) => LayoutDecoration | null;
167
175
  *
168
176
  * 实例有两种来源:Temporal.prepareLayout 可以直接加入;addon 对应的 LayoutDecorationHandler 也可以在主体 prepareLayout 后创建
169
177
  * 两者都在创建时就拿到了宿主,因此回调不再重复传入它;实例可以在返回前调整 host.box,例如 dot 先扩张主体宽度
170
- * 被 arrangeBelowDecorations 使用
178
+ * 由引擎统一分配上下空间,place 的坐标相对主体定位矩形
171
179
  *
172
- * 通用部分只有 paint:主体绘制完成后,引擎调用它绘制当前装饰。
173
- *
174
- * below 是可选的“主体下方空间”子协议,只向下扩张 box.h,不管理主体上方空间
175
- * 上方内容若会改变主体 visualAxis 或内部绘制坐标,应由具体 Temporal 在 prepareLayout 中处理。
180
+ * paint 可省略,此时只预留空间,不进入内容边界
176
181
  *
177
182
  * 当前内置示例:
178
183
  * - dot:handler 生成只负责横向扩宽和绘制的装饰,不声明 below;
179
184
  * - div:handler 生成减时线装饰,使用 order=0,排在主体下方最内层;
180
- * - note 下八度点:prepareLayout 直接加入装饰,使用 order=100,排在减时线之后;
181
- * - note 上八度点不使用 LayoutDecoration,而是主体 prepareLayout 的一部分。
185
+ * - note 八度点:prepareLayout 直接加入装饰,使用 order=100
182
186
  *
183
187
  * LayoutDecoration 不是 addon 语义本身。实例可以用闭包保存本次测量参数和 place 结果,因此必须由 Temporal.decorations 持有到当前 paint 结束。
184
188
  */
185
189
  export interface LayoutDecoration {
186
- paint(painter: Painter): void;
187
- /** 声明后进入主体下方空间管理,引擎只理解这四个字段,不知道空间来自哪个函数 */
188
- below?: {
189
- order: number;
190
- gap?: number;
191
- height?: number;
192
- /**
193
- * 引擎分配好本装饰的下方区域后调用一次,y 是该区域 **顶边到 host.box 顶部** 的距离
194
- *
195
- * 该做的:把这一刻才确定的局部几何存进闭包供 paint 使用(note 存下八度点的 y),
196
- * 以及发布依赖这个位置的端口(div 在这里建 div.N.left/right)。只占位不绘制时可以不实现。
197
- *
198
- * 不该做的:改 box.h(引擎会按 height 统一扩张)、读 box.y 或 box.h (前者尚未求解、后者正在向下累加)。
199
- * 此时只有 box.w/anchor 和 ports 是最终值,因此一律记录相对盒顶的局部坐标,绘制时再加 host.box.x / host.box.y。
200
- *
201
- * 例(一条减时线):
202
- * ```ts
203
- * let lineY = 0; // 闭包保存本轮测量结果
204
- * return {
205
- * below: {
206
- * order: 0,
207
- * height: strokeWidth,
208
- * place(y) {
209
- * lineY = y + strokeWidth / 2;
210
- * host.ports["div.0.left"] = { x: 0, y: lineY };
211
- * },
212
- * },
213
- * paint(painter) {
214
- * const y = host.box.y + lineY; // 此时 box.y 才有意义
215
- * painter.drawLine(host.box.x, y, host.box.x + host.box.w, y, { stroke: "#000", strokeWidth });
216
- * },
217
- * };
218
- * ```
219
- */
220
- place?(y: number): void;
221
- };
190
+ paint?(painter: Painter): void;
191
+ above?: LayoutDecorationSpace;
192
+ below?: LayoutDecorationSpace;
193
+ }
194
+ /** 上下装饰共用由内向外的排列协议 */
195
+ export interface LayoutDecorationSpace {
196
+ order: number;
197
+ gap?: number;
198
+ height?: number;
199
+ /**
200
+ * y 是区域顶边相对 host.box 顶部的坐标,上方区域通常为负数
201
+ * 只保存局部几何并发布端口,不读写正在排列的 box.h 或尚未放置的 box.y
202
+ * 绘制时再加 host.box.x/y,纯留白可以省略 place
203
+ */
204
+ place?(y: number): void;
222
205
  }
223
206
  /**
224
207
  * 一次 attachment 放置产生的完整几何
@@ -238,6 +221,8 @@ export interface AttachmentGeometry {
238
221
  * 实例只保存语义输入和一次横向准备状态,不保存最终 box、regions 或绘制几何。
239
222
  */
240
223
  export interface LayoutAttachment extends LoweringAttachment {
224
+ /** 后置平移仅影响绘制,不参与占用求解 */
225
+ placementOffset?: LayoutPoint;
241
226
  /** 几何端点;端点全部落在同一子域的成员上时,附件属于该子域。不表示内容包含或播放端点投影 */
242
227
  readonly endPoints?: readonly TemporalNodeBase[];
243
228
  /** 相对于 Temporal 主体的绘制层;background 比内容先绘制 */
@@ -57,7 +57,7 @@ export declare class LoweringContext {
57
57
  */
58
58
  lowerDocument(node: ASTNodeBase): LoweringResult;
59
59
  /**
60
- * 固化谱面行号并进行时间状态固化
60
+ * 固化谱面行号、时间状态和最终时长
61
61
  */
62
62
  private solidifyColumns;
63
63
  /**
@@ -75,7 +75,7 @@ export class LoweringContext {
75
75
  const rootTrack = new Track();
76
76
  const duration = new Fraction();
77
77
  const columns = this.trackedEvents(node, duration, rootTrack);
78
- this.solidifyColumns(columns);
78
+ this.solidifyColumns(columns, duration);
79
79
  return this.postprocessResult({
80
80
  diagnostics: this.diagnostics,
81
81
  columns,
@@ -88,9 +88,9 @@ export class LoweringContext {
88
88
  });
89
89
  }
90
90
  /**
91
- * 固化谱面行号并进行时间状态固化
91
+ * 固化谱面行号、时间状态和最终时长
92
92
  */
93
- solidifyColumns(columns) {
93
+ solidifyColumns(columns, duration) {
94
94
  // 同时刻跨轨的 br 已经合成一列,所以取最大;同轨连写仍是多个列,因而累加
95
95
  let line = 0;
96
96
  for (const column of columns) {
@@ -115,14 +115,44 @@ export class LoweringContext {
115
115
  const programs = new Map();
116
116
  const velocityOf = (track) => velocities.get(track) ?? (track.parent ? velocityOf(track.parent) : DEFAULT_VELOCITY);
117
117
  const programOf = (track) => programs.get(track) ?? (track.parent ? programOf(track.parent) : DEFAULT_PROGRAM);
118
+ const timeOffset = new Fraction();
118
119
  for (const column of columns) {
120
+ if (!timeOffset.isZero())
121
+ column.shiftTime(timeOffset);
122
+ let insertion;
119
123
  for (const node of column) {
120
124
  state.velocity = velocityOf(node.track);
121
125
  state.program = programOf(node.track);
122
- node.onTimeState?.(state);
126
+ const offset = node.onTimeState?.(state);
127
+ if (offset && !offset.isZero() && (!insertion || offset.compare(insertion) > 0)) {
128
+ (insertion ??= new Fraction()).copyFrom(offset);
129
+ }
123
130
  velocities.set(node.track, state.velocity);
124
131
  programs.set(node.track, state.program);
125
132
  }
133
+ // 同列声部一起开始,只在整列固化后累加一次偏移
134
+ if (insertion)
135
+ timeOffset.add(insertion);
136
+ }
137
+ if (timeOffset.isZero())
138
+ return;
139
+ // 偏移非负、同轨时序前进,只需各轨尾节点;programs 已记录全部全局轨道
140
+ const tracks = new Set(programs.keys());
141
+ const end = new Fraction();
142
+ duration.set(0);
143
+ for (let i = columns.length - 1; i >= 0 && tracks.size > 0; i--) {
144
+ const column = columns[i];
145
+ for (let j = column.length - 1; j >= 0; j--) {
146
+ const node = column[j];
147
+ if (!tracks.delete(node.track))
148
+ continue;
149
+ const T = node.T;
150
+ end.copyFrom(node.t);
151
+ if (!T.isZero())
152
+ end.add(T);
153
+ if (end.compare(duration) > 0)
154
+ duration.copyFrom(end);
155
+ }
126
156
  }
127
157
  }
128
158
  /**
@@ -408,7 +408,7 @@ export class ParserContext {
408
408
  const key = name ?? i;
409
409
  if (type === void 0) {
410
410
  if (!def.allowExtraArgs) {
411
- this.diagnostics.push(Diagnostic.warning.TooManyPosArgs(callNode.name, defArgs.length, i + 1, arg.valueSpan));
411
+ this.diagnostics.push(Diagnostic.warning.TooManyPosArgs(callNode.name, defArgs.filter(arg => !arg.namedOnly).length, i + 1, arg.valueSpan));
412
412
  }
413
413
  else
414
414
  args.set(key, arg);
@@ -445,9 +445,8 @@ export class ParserContext {
445
445
  }
446
446
  return num;
447
447
  case "boolean":
448
- const boolText = text;
449
- if (BOOL_RE.test(boolText)) {
450
- return boolText === "true";
448
+ if (BOOL_RE.test(text)) {
449
+ return text === "true";
451
450
  }
452
451
  else {
453
452
  this.diagnostics.push(Diagnostic.warning.InvalidBoolean(text, r));
@@ -509,11 +508,8 @@ export class ParserContext {
509
508
  return l;
510
509
  // 其他类型一律视为string
511
510
  default:
512
- // 去除首尾引号(单引号或双引号),如果存在
513
- let result = text;
514
- return removeQuote(result);
511
+ return removeQuote(text);
515
512
  }
516
- return null;
517
513
  }
518
514
  length2px(length) {
519
515
  // 不进行错误判断了: 来自 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;
@@ -508,7 +508,7 @@ export function compilePlayback(lowering, options) {
508
508
  }
509
509
  }
510
510
  // 稳定排序后,由统一出口检查配对、压实轨道编号并剥离编译期字段
511
- const finalEvents = events.filter(event => !removed.has(event)).sort(comparePlaybackDraftEvents);
511
+ const finalEvents = (removed.size ? events.filter(event => !removed.has(event)) : events).sort(comparePlaybackDraftEvents);
512
512
  const output = finalizePlaybackEvents(finalEvents, lowering.tracks);
513
513
  const eventEnd = finalEvents.at(-1)?.at;
514
514
  if (eventEnd && eventEnd.compare(performanceEnd) > 0)
@@ -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")