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
@@ -46,20 +46,6 @@ const ACCIDENTALS = {
46
46
  ],
47
47
  },
48
48
  };
49
- /** size 是数字字号,升降号自己决定缩小多少 */
50
- function prepareAccidental(symbol, size) {
51
- const definition = ACCIDENTALS[symbol];
52
- if (!definition)
53
- return null;
54
- const glyphSize = size * GLYPH_SCALE;
55
- return {
56
- w: definition.w * glyphSize,
57
- h: definition.h * glyphSize,
58
- baseline: definition.baseline * glyphSize,
59
- strokeWidth: size * STROKE_RATIO,
60
- commands: definition.commands,
61
- };
62
- }
63
49
  /**
64
50
  * 把升降号串按同一基线依次排在 x 右侧
65
51
  * @param size 宿主数字的字号
@@ -69,13 +55,21 @@ function prepareAccidental(symbol, size) {
69
55
  */
70
56
  export function placeAccidentals(symbols, size, x, baselineY, gap) {
71
57
  const placed = [];
58
+ const glyphSize = size * GLYPH_SCALE;
72
59
  const raise = size * RAISE_RATIO;
73
60
  let right = x;
74
61
  let top = 0;
75
62
  for (const symbol of symbols) {
76
- const shape = prepareAccidental(symbol, size);
77
- if (!shape)
63
+ const definition = ACCIDENTALS[symbol];
64
+ if (!definition)
78
65
  continue;
66
+ const shape = {
67
+ w: definition.w * glyphSize,
68
+ h: definition.h * glyphSize,
69
+ baseline: definition.baseline * glyphSize,
70
+ strokeWidth: size * STROKE_RATIO,
71
+ commands: definition.commands,
72
+ };
79
73
  const y = baselineY - shape.baseline - raise;
80
74
  placed.push({ shape, x: right, y });
81
75
  if (y < top)
@@ -1,6 +1,7 @@
1
1
  import { ASTTextNode, ASTFunctionNode } from "../ASTtypes.js";
2
2
  import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
3
3
  import { parseNoteName } from "./noteNameFSM.js";
4
+ import { getLayoutBounds } from "../../layout/engine.js";
4
5
  import { paintAccidental, placeAccidentals } from "./accidentals.js";
5
6
  import { rewriteNotes } from "./source-transform.js";
6
7
  /** 根据同一源码的 lowering 生成局部修改;传入 ranges 时只处理完整落入任一选区的音符 */
@@ -170,7 +171,7 @@ import { createDiatonicTranspose, resolveLetterNameToJianpu, resolveNoteMidi, }
170
171
  const NOTE_GLYPH = { "8": "", "9": "X" };
171
172
  class NoteTemporalNode extends TemporalNodeBase {
172
173
  // 时间固化后的参数
173
- resolvedMidi = null; // MIDI音高 name是数字则需要在onTimeState中基于当前调性偏移 播放的音高
174
+ resolvedMidi = null; // 按调性解析的记谱音高,不含播放偏移
174
175
  keySignature = "C4"; // 用于转调时记录大环境
175
176
  transposeDiatonic;
176
177
  name; // 数字
@@ -180,10 +181,6 @@ class NoteTemporalNode extends TemporalNodeBase {
180
181
  accidentals = [];
181
182
  /** 字号和颜色在解析时已经固化,样式随之不再变 */
182
183
  numberStyle;
183
- /** 上八度点属于主体,下八度点通过通用装饰流排在其他下方符号之后 */
184
- upperOctaveDotY = [];
185
- lowerOctaveDotY = [];
186
- octaveDotRadius = 0;
187
184
  numberY = 0;
188
185
  constructor(ast) {
189
186
  super();
@@ -203,72 +200,66 @@ class NoteTemporalNode extends TemporalNodeBase {
203
200
  this.initLayoutBox();
204
201
  }
205
202
  prepareLayout(context) {
206
- this.upperOctaveDotY.length = 0;
207
- this.lowerOctaveDotY.length = 0;
208
203
  const { size, color } = this.ast;
209
204
  const mainMetrics = context.textMeasurer.measureText("0", this.numberStyle);
210
- const dotCount = Math.abs(this.octave);
211
- this.octaveDotRadius = size * 0.075;
212
- const dotGap = size * 0.08;
213
- const dotStep = this.octaveDotRadius * 2 + dotGap;
214
- const octaveSpace = dotCount === 0
215
- ? 0
216
- : dotCount * this.octaveDotRadius * 2 + (dotCount - 1) * dotGap + dotGap;
217
- const mainY = this.octave > 0 ? octaveSpace : 0;
218
- this.numberY = mainY + mainMetrics.baseline;
205
+ this.numberY = mainMetrics.baseline;
219
206
  // 升降号贴近数字左上角,按同一 baseline 排在数字左侧;
220
207
  // 缩小右侧间隔会在全局 anchor 不变时把升降号向右移动
221
208
  const accidentalGap = size * 0.01;
222
209
  const { placed, right: mainX } = placeAccidentals(this.acc, size, 0, this.numberY, accidentalGap);
223
210
  this.accidentals = placed;
224
211
  this.box.w = mainX + mainMetrics.w;
225
- this.box.h = mainY + mainMetrics.h;
212
+ this.box.h = mainMetrics.h;
226
213
  this.box.anchor = mainX + mainMetrics.w / 2;
227
- this.box.visualAxis = mainY + mainMetrics.h / 2;
214
+ this.box.visualAxis = mainMetrics.h / 2;
228
215
  // 升降号和附点参与完整盒布局,但不计入数字主体范围,减时线因此只画在数字下
229
216
  this.ports["body.left"] = { x: this.box.anchor - mainMetrics.w / 2, y: this.box.visualAxis };
230
217
  this.ports["body.right"] = { x: this.box.anchor + mainMetrics.w / 2, y: this.box.visualAxis };
231
- this.ports["shoulder"] = { x: this.box.anchor, y: mainY };
218
+ this.ports["shoulder"] = { x: this.box.anchor, y: 0 };
219
+ // 下方装饰从数字基线附近起排,不把 em 盒底部的留白算作可见正文。
220
+ this.ports["decoration.below"] = { x: this.box.anchor, y: this.numberY + size * 0.04 };
232
221
  // 覆盖 dot 的默认“右边界 + 视觉轴”,让附点贴合数字字形的视觉位置
233
222
  this.ports["dot"] = {
234
223
  x: this.box.w,
235
- y: mainY + mainMetrics.h * 0.64,
224
+ y: mainMetrics.h * 0.64,
236
225
  };
237
- if (this.octave > 0) {
238
- // 上八度点从靠近数字的位置向上依次排列
239
- for (let i = 0; i < dotCount; i++) {
240
- this.upperOctaveDotY.push(mainY - dotGap - this.octaveDotRadius - i * dotStep);
241
- }
242
- return;
243
- }
244
- if (this.octave >= 0)
226
+ const dotCount = Math.abs(this.octave);
227
+ if (dotCount === 0)
245
228
  return;
246
- const lowerDotHeight = dotCount * this.octaveDotRadius * 2 + (dotCount - 1) * dotGap;
247
- const lowerDots = {
248
- below: {
229
+ const above = this.octave > 0;
230
+ const dotRadius = size * 0.075;
231
+ const dotGap = size * 0.08;
232
+ const dotStep = dotRadius * 2 + dotGap;
233
+ const dotStyle = { fill: color };
234
+ let dotY = 0;
235
+ const height = dotCount * dotRadius * 2 + (dotCount - 1) * dotGap;
236
+ const dots = {
237
+ [above ? "above" : "below"]: {
249
238
  // order 只表达通用的由近到远顺序
250
239
  // 时值类装饰可以选择较小 order,下八度点选择较大 order
251
240
  order: 100,
252
241
  gap: dotGap,
253
- height: lowerDotHeight,
254
- place: y => {
255
- // 下八度点从引擎分配的外层区域顶部向下排列
256
- for (let i = 0; i < dotCount; i++) {
257
- this.lowerOctaveDotY.push(y + this.octaveDotRadius + i * dotStep);
258
- }
259
- },
242
+ height,
243
+ place: (y) => { dotY = y; },
260
244
  },
261
245
  paint: painter => {
262
- // 下八度点读取已经冻结的局部圆心位置
263
- for (const y of this.lowerOctaveDotY) {
264
- painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, this.octaveDotRadius, { fill: color });
246
+ // 每列八度点共用一个装饰,上方从靠近数字的位置向外绘制
247
+ for (let i = 0; i < dotCount; i++) {
248
+ const y = above ? dotY + height - dotRadius - i * dotStep : dotY + dotRadius + i * dotStep;
249
+ painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, dotRadius, dotStyle);
265
250
  }
266
251
  },
267
252
  };
268
- this.decorations.push(lowerDots);
253
+ this.decorations.push(dots);
269
254
  }
270
255
  finalizeLayout() {
271
256
  this.ports["lyric"] = { x: this.box.anchor, y: this.box.h };
257
+ if (this.octave > 0) { // 八度点上方留空间
258
+ this.ports["tie.top"] = {
259
+ x: this.box.anchor,
260
+ y: getLayoutBounds(this, true).y - this.box.y - this.ast.size * 0.05,
261
+ };
262
+ }
272
263
  }
273
264
  paint(painter) {
274
265
  const color = this.ast.color;
@@ -278,10 +269,6 @@ class NoteTemporalNode extends TemporalNodeBase {
278
269
  if (glyph) {
279
270
  painter.drawText(glyph, this.box.x + this.box.anchor, this.box.y + this.numberY, this.numberStyle);
280
271
  }
281
- // 上八度点属于主体绘制,不参与下方装饰流
282
- for (const y of this.upperOctaveDotY) {
283
- painter.drawCircle(this.box.x + this.box.anchor, this.box.y + y, this.octaveDotRadius, { fill: color });
284
- }
285
272
  }
286
273
  /** 固化记谱位置的音高、力度和音色,并保留基于该调性的相对移调能力 */
287
274
  onTimeState(state) {
@@ -310,6 +297,8 @@ class NoteTemporalNode extends TemporalNodeBase {
310
297
  emitPlayback(emitter) {
311
298
  const percussion = this.ast.name === "9";
312
299
  const midi = percussion ? 37 : this.resolvedMidi;
300
+ const shift = percussion ? 0 : this.playbackState?.pitchShift ?? 0;
301
+ const transpose = this.transposeDiatonic;
313
302
  if (emitter.end.compare(emitter.start) <= 0)
314
303
  return;
315
304
  // 休止符和占位符也要保留结构边界,速度效果与后续延长才能覆盖这段时间。
@@ -321,9 +310,10 @@ class NoteTemporalNode extends TemporalNodeBase {
321
310
  emitter.note({
322
311
  start: emitter.start,
323
312
  end: emitter.end,
324
- midi,
313
+ midi: midi + shift,
325
314
  velocity: this.playbackState?.velocity ?? DEFAULT_VELOCITY,
326
- ...(percussion ? { percussion: true } : { transpose: this.transposeDiatonic }),
315
+ ...(percussion ? { percussion: true }
316
+ : { transpose: shift && transpose ? steps => transpose(steps) + shift : transpose }),
327
317
  });
328
318
  }
329
319
  }
@@ -0,0 +1,2 @@
1
+ import { type ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const OttavaNode: ASTFunctionClass;
@@ -0,0 +1,217 @@
1
+ import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
2
+ import { unionLayoutBoxes } from "../../layout/engine.js";
3
+ import { ASTFunctionNode, } from "../ASTtypes.js";
4
+ import { isVisualTemporalNode } from "../temporal.js";
5
+ function outerHost(node) {
6
+ while (node.foldedInto)
7
+ node = node.foldedInto;
8
+ return node;
9
+ }
10
+ class OttavaFunction extends ASTFunctionNode {
11
+ static def = {
12
+ name: ["ottava"],
13
+ description: "八度号",
14
+ details: `\
15
+ ~~~jpfun
16
+ 1@a 2 3 -@b | 4
17
+ @ottava(a, b, 1)
18
+ ~~~
19
+ \`shift\` 为非零整数八度数,正数在上方标高八度号,负数在下方标低八度号,默认 \`1\`。标签使用谱面数字字体的斜体。`,
20
+ allowExtraArgs: false,
21
+ args: [
22
+ {
23
+ description: "起点标签",
24
+ type: "label",
25
+ default: null
26
+ },
27
+ {
28
+ description: "终点标签",
29
+ type: "label",
30
+ default: null
31
+ },
32
+ {
33
+ name: "shift",
34
+ description: "非零整数八度数,正数为高,负数为低",
35
+ type: "number",
36
+ default: 1
37
+ },
38
+ ],
39
+ };
40
+ static loweringFinalize = (result) => {
41
+ const ottavas = result.attachments.filter((attachment) => attachment instanceof OttavaAttachment).map(attachment => {
42
+ const [from, to] = attachment.endPoints;
43
+ return { from, end: to.t.clone().add(to.T), shift: attachment.ast.shift * 12 };
44
+ });
45
+ if (ottavas.length === 0)
46
+ return;
47
+ for (const nodes of result.astToTemporal.values()) {
48
+ for (const node of nodes) {
49
+ if (!node.playbackState)
50
+ continue;
51
+ const host = outerHost(node);
52
+ let shift = 0;
53
+ for (const ottava of ottavas) {
54
+ if (host.track === ottava.from.track && host.t.compare(ottava.from.t) >= 0
55
+ && host.t.compare(ottava.end) < 0) {
56
+ shift += ottava.shift;
57
+ }
58
+ }
59
+ if (shift !== 0) {
60
+ node.playbackState = {
61
+ ...node.playbackState,
62
+ pitchShift: (node.playbackState.pitchShift ?? 0) + shift,
63
+ };
64
+ }
65
+ }
66
+ }
67
+ };
68
+ from;
69
+ to;
70
+ shift;
71
+ size;
72
+ font;
73
+ constructor(span, args, ctx, parent = null) {
74
+ super(span, parent);
75
+ const [from, to, shift] = this.getArgValue(args, ctx);
76
+ if (!Number.isSafeInteger(shift) || shift === 0 || !Number.isSafeInteger(shift * 12)) {
77
+ throw new ErrorDiagnostic("E_OTTAVA_INVALID_SHIFT", "@ottava 的 shift 必须为可安全换算为半音的非零整数", span);
78
+ }
79
+ this.from = from;
80
+ this.to = to;
81
+ this.shift = shift;
82
+ this.size = ctx.variables.fontsize;
83
+ this.font = ctx.variables.numberfont;
84
+ }
85
+ loweringEnter(ctx) {
86
+ const from = ctx.getTemporalNodes(this.from).at(0);
87
+ const to = ctx.getTemporalNodes(this.to).at(-1);
88
+ const head = from && outerHost(from);
89
+ const tail = to && outerHost(to);
90
+ if (!head || !tail || !isVisualTemporalNode(head) || !isVisualTemporalNode(tail)) {
91
+ ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("ottava", this.sourceSpan));
92
+ return [];
93
+ }
94
+ if (head.track !== tail.track) {
95
+ throw new ErrorDiagnostic("E_OTTAVA_CROSS_TRACK", "@ottava 的两个端点必须位于同一音轨", this.sourceSpan);
96
+ }
97
+ ctx.addAttachment(new OttavaAttachment(this, head, tail));
98
+ return [];
99
+ }
100
+ toString(source) {
101
+ const name = (node) => (node instanceof ASTFunctionNode ? node.label : undefined) ?? node.toString(source);
102
+ return `@ottava(${name(this.from)}, ${name(this.to)}, ${this.shift})`;
103
+ }
104
+ }
105
+ export const OttavaNode = OttavaFunction;
106
+ class OttavaAttachment {
107
+ ast;
108
+ from;
109
+ to;
110
+ layer = "foreground";
111
+ constructor(ast, from, to) {
112
+ this.ast = ast;
113
+ this.from = from;
114
+ this.to = to;
115
+ }
116
+ get sourceSpan() { return this.ast.sourceSpan; }
117
+ get endPoints() {
118
+ return (this.from.t.compare(this.to.t) || this.from.order - this.to.order) > 0
119
+ ? [this.to, this.from] : [this.from, this.to];
120
+ }
121
+ createGeometry(context) {
122
+ const endpoints = this.endPoints;
123
+ const [from, to] = endpoints;
124
+ const firstLine = from.layoutLine;
125
+ const lastLine = to.layoutLine;
126
+ if (context.lines[firstLine].columnOf(from) < 0 || context.lines[lastLine].columnOf(to) < 0) {
127
+ return { regions: [], paint() { } };
128
+ }
129
+ const { size, font, shift } = this.ast;
130
+ const above = shift > 0;
131
+ const track = from.track;
132
+ const strokeWidth = Math.max(0.75, size * 0.035);
133
+ const lineStyle = { stroke: "#000", strokeWidth };
134
+ const dash = size * 0.22;
135
+ const dashStep = dash + size * 0.14;
136
+ const halfStroke = strokeWidth / 2;
137
+ const hook = size * 0.45;
138
+ const gap = size * 0.2;
139
+ const labelGap = size * 0.08;
140
+ const text = String(Math.abs(shift) * 7 + 1);
141
+ const style = {
142
+ fontSize: size * 0.6,
143
+ fontFamily: font,
144
+ fontStyle: "italic",
145
+ textAlign: "left",
146
+ fill: "#000",
147
+ };
148
+ const metrics = context.textMeasurer.measureText(text, style);
149
+ const labelTop = above ? -halfStroke : halfStroke - metrics.h;
150
+ const labelY = labelTop + metrics.baseline;
151
+ const draws = Array.from({ length: lastLine - firstLine + 1 }, (_, offset) => {
152
+ const line = firstLine + offset;
153
+ const first = line === firstLine;
154
+ const last = line === lastLine;
155
+ const runs = context.lines[line].trackRuns;
156
+ let left = first ? from.box.x + (from.ports["body.left"]?.x ?? 0)
157
+ : runs.get(track)?.find(host => !host.T.isZero())?.box.x ?? context.originX;
158
+ let right = to.box.x + (to.ports["body.right"]?.x ?? to.box.w);
159
+ if (!last) {
160
+ right = context.originX + (runs.size === 0 ? context.width : 0);
161
+ for (const run of runs.values()) {
162
+ for (const host of run)
163
+ right = Math.max(right, host.box.x + host.box.w);
164
+ }
165
+ }
166
+ if (left > right)
167
+ [left, right] = [right, left];
168
+ const lineStart = first ? left + metrics.w + labelGap : left;
169
+ const part = (x, y, w, h) => ({ x, y, w, h, line, track });
170
+ const parts = lineStart < right
171
+ ? [part(lineStart - halfStroke, -halfStroke, right - lineStart + strokeWidth, strokeWidth)] : [];
172
+ if (first)
173
+ parts.push(part(left, labelTop, metrics.w, metrics.h));
174
+ if (last)
175
+ parts.push(part(right - halfStroke, above ? -halfStroke : -hook - halfStroke, strokeWidth, hook + strokeWidth));
176
+ const region = part(0, 0, 0, 0);
177
+ unionLayoutBoxes(region, parts);
178
+ let lineY = above ? Infinity : -Infinity;
179
+ for (const part of parts) {
180
+ for (const [lane, extent] of context.getHorizontalExtents(line, part.x, part.x + part.w)) {
181
+ const axis = context.getVisualAxis(line, lane);
182
+ lineY = above
183
+ ? Math.min(lineY, axis + extent.top - gap - part.y - part.h)
184
+ : Math.max(lineY, axis + extent.bottom + gap - part.y);
185
+ }
186
+ }
187
+ if (!Number.isFinite(lineY)) {
188
+ const extent = context.getRangeExtents(line, first || last ? endpoints : undefined, track);
189
+ const axis = context.getVisualAxis(line, track);
190
+ lineY = above
191
+ ? axis + (extent?.top ?? 0) - gap - (region.y + region.h)
192
+ : axis + (extent?.bottom ?? 0) + gap - region.y;
193
+ }
194
+ for (const part of parts)
195
+ part.y += lineY;
196
+ region.y += lineY;
197
+ return { left, right, lineStart, lineY, first, last, parts, region };
198
+ });
199
+ return {
200
+ regions: draws.map(draw => draw.region),
201
+ occupancy: draws.flatMap(draw => draw.parts),
202
+ paint(painter) {
203
+ for (const draw of draws) {
204
+ const { left, right, lineStart, lineY, first, last } = draw;
205
+ for (let x = lineStart; x < right; x += dashStep) {
206
+ painter.drawLine(x, lineY, Math.min(x + dash, right), lineY, lineStyle);
207
+ }
208
+ if (first)
209
+ painter.drawText(text, left, lineY + labelY, style);
210
+ if (last) {
211
+ painter.drawLine(right, lineY, right, lineY + (above ? hook : -hook), lineStyle);
212
+ }
213
+ }
214
+ },
215
+ };
216
+ }
217
+ }
@@ -57,7 +57,7 @@ export class PageFunction extends ASTFunctionNode {
57
57
  allowExtraArgs: false,
58
58
  args: [
59
59
  { name: "width", description: "页面宽度,须大于左右页边距之和", type: "length", default: px(DEFAULT_PAGE_CONFIG.width) },
60
- { name: "height", description: "页面高度,0 表示不分页、高度随内容增长;宽度为 `794px` 时,A4 对应高度约为 `1123px`", type: "length", default: px(0) },
60
+ { name: "height", description: "页面高度,小于等于 0 表示不自动分页、高度随内容增长;宽度为 `794px` 时,A4 对应高度约为 `1123px`", type: "length", default: px(0) },
61
61
  { name: "top", description: "页面上边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginTop) },
62
62
  { name: "bottom", description: "页面下边距,启用页码时至少为 `16px`", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginBottom) },
63
63
  { name: "left", description: "页面左边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginLeft) },
@@ -93,8 +93,8 @@ export class PageFunction extends ASTFunctionNode {
93
93
  const [width, height, top, bottom, left, right, gap] = values.map(value => ctx.length2px(value));
94
94
  if (!Number.isFinite(width) || width <= 0)
95
95
  throw Diagnostic.error.InvalidPageConfig("@page 的 width 必须是正有限长度", sourceSpan);
96
- if (!Number.isFinite(height) || height < 0)
97
- throw Diagnostic.error.InvalidPageConfig("@page 的 height 必须是非负有限长度;0 表示不分页", sourceSpan);
96
+ if (!Number.isFinite(height))
97
+ throw Diagnostic.error.InvalidPageConfig("@page 的 height 必须是有限长度;小于等于 0 表示不自动分页", sourceSpan);
98
98
  if ([top, bottom, left, right, gap].some(value => !Number.isFinite(value) || value < 0))
99
99
  throw Diagnostic.error.InvalidPageConfig("@page 的边距和 gap 必须是非负有限长度", sourceSpan);
100
100
  if (left + right >= width)
@@ -0,0 +1,2 @@
1
+ import { type ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const PageBreakNode: ASTFunctionClass;
@@ -0,0 +1,36 @@
1
+ import { TemporalNodeBase } from "../temporal.js";
2
+ import { BrNode } from "../br/index.js";
3
+ import { ASTFunctionNode, } from "../ASTtypes.js";
4
+ class PageBreakFunction extends ASTFunctionNode {
5
+ static def = {
6
+ name: ["pagebreak", "pb"],
7
+ description: "强制换页",
8
+ details: `\
9
+ ~~~jpfun
10
+ 1 2 @br() 3 4 @pagebreak() 5 6
11
+ ~~~
12
+ 同时换行并开启新页,不占音乐时值。同一时刻的并行调用只换一次页,\`justify=false\` 优先。`,
13
+ allowExtraArgs: false,
14
+ args: [{
15
+ name: "justify",
16
+ description: "是否分配当前页的剩余空间;无限页高时不拉伸",
17
+ type: "boolean",
18
+ default: true,
19
+ }],
20
+ };
21
+ static loweringFinalize = BrNode.loweringFinalize;
22
+ justify;
23
+ constructor(span, args, ctx, parent = null) {
24
+ super(span, parent);
25
+ [this.justify] = this.getArgValue(args, ctx);
26
+ }
27
+ loweringEnter() {
28
+ const node = new TemporalNodeBase();
29
+ node.ast = this;
30
+ node.mergeKey = -1;
31
+ node.addon = { breakBefore: 1, pageBreakBefore: this.justify };
32
+ return [node];
33
+ }
34
+ toString() { return `@pagebreak(justify=${this.justify})`; }
35
+ }
36
+ export const PageBreakNode = PageBreakFunction;
@@ -0,0 +1,2 @@
1
+ import { type ASTFunctionClass } from "../ASTtypes.js";
2
+ export declare const RestNode: ASTFunctionClass;