jpfun 0.8.0 → 0.9.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.
- package/dist/converter/midi/convert.js +8 -55
- package/dist/converter/musicxml/features.d.ts +1 -1
- package/dist/converter/musicxml/features.js +1 -1
- package/dist/converter/musicxml/index.js +8 -18
- package/dist/converter/source.d.ts +4 -7
- package/dist/converter/source.js +11 -9
- package/dist/functions/ASTtypes.d.ts +1 -14
- package/dist/functions/ASTtypes.js +5 -36
- package/dist/functions/adjust/index.js +45 -33
- package/dist/functions/arpeggio/index.js +42 -45
- package/dist/functions/arpeggio/shape.d.ts +2 -3
- package/dist/functions/arpeggio/shape.js +20 -50
- package/dist/functions/bar/index.js +3 -8
- package/dist/functions/beam/auto.js +54 -123
- package/dist/functions/beam/index.js +4 -9
- package/dist/functions/box/index.js +0 -6
- package/dist/functions/dash/index.js +3 -4
- package/dist/functions/div/index.d.ts +2 -4
- package/dist/functions/div/index.js +10 -10
- package/dist/functions/dot/index.js +3 -9
- package/dist/functions/dyn/index.js +6 -26
- package/dist/functions/gliss/index.js +95 -89
- package/dist/functions/grace/index.d.ts +2 -5
- package/dist/functions/grace/index.js +44 -89
- package/dist/functions/head/index.js +9 -6
- package/dist/functions/key/index.js +6 -6
- package/dist/functions/meter/index.js +3 -3
- package/dist/functions/note/index.js +1 -1
- package/dist/functions/note/source-transform.js +2 -5
- package/dist/functions/ottava/index.js +11 -32
- package/dist/functions/page/index.js +13 -16
- package/dist/functions/rest/index.js +2 -4
- package/dist/functions/slide/index.js +0 -1
- package/dist/functions/stack/index.js +8 -57
- package/dist/functions/temporal.d.ts +2 -2
- package/dist/functions/temporal.js +7 -0
- package/dist/functions/text/index.js +24 -30
- package/dist/functions/tie/index.js +14 -37
- package/dist/functions/tremolo/index.js +0 -1
- package/dist/functions/tuplet/index.js +0 -6
- package/dist/functions/up/index.js +79 -83
- package/dist/functions/voice/connections.d.ts +2 -1
- package/dist/functions/voice/connections.js +48 -10
- package/dist/functions/voice/index.d.ts +2 -3
- package/dist/functions/voice/index.js +2 -970
- package/dist/functions/voice/lyrics.d.ts +30 -0
- package/dist/functions/voice/lyrics.js +289 -0
- package/dist/functions/voice/voice.d.ts +63 -0
- package/dist/functions/voice/voice.js +373 -0
- package/dist/functions/voice/voices.d.ts +106 -0
- package/dist/functions/voice/voices.js +423 -0
- package/dist/functions/volta/index.js +5 -23
- package/dist/jpfun.core.min.js +65 -56
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +7 -7
- package/dist/jpfun.min.js +68 -59
- package/dist/layout/engine.js +30 -18
- package/dist/layout/model.d.ts +0 -1
- package/dist/layout/model.js +1 -4
- package/dist/layout/page.d.ts +2 -1
- package/dist/layout/page.js +14 -14
- package/dist/layout/range.d.ts +7 -6
- package/dist/layout/range.js +23 -14
- package/dist/layout/types.d.ts +9 -28
- package/dist/lowering/loweringContext.d.ts +8 -11
- package/dist/lowering/loweringContext.js +7 -13
- package/dist/lowering/track.d.ts +8 -1
- package/dist/lowering/types.d.ts +12 -8
- package/dist/parser/parse-utils/call-utils.d.ts +1 -2
- package/dist/parser/parse-utils/call-utils.js +6 -34
- package/dist/parser/parse-utils/note-utils.d.ts +3 -3
- package/dist/parser/parse-utils/note-utils.js +17 -28
- package/dist/parser/parse-utils/string-utils.d.ts +1 -1
- package/dist/parser/parse-utils/string-utils.js +8 -6
- package/dist/parser/parserContext.d.ts +9 -9
- package/dist/parser/parserContext.js +54 -10
- package/dist/pipeline.js +2 -2
- package/dist/playback/compile.js +13 -14
- package/dist/playback/event.d.ts +6 -6
- package/dist/playback/event.js +13 -49
- package/dist/render/canvas.d.ts +0 -2
- package/dist/render/canvas.js +0 -21
- package/dist/render/svg.d.ts +0 -1
- package/dist/render/svg.js +33 -31
- package/dist/render/text.d.ts +36 -1
- package/dist/render/text.js +134 -0
- package/dist/render/types.d.ts +0 -2
- package/grammar.md +16 -13
- package/package.json +1 -1
- package/dist/iife/core.d.ts +0 -1
- package/dist/iife/core.js +0 -3
- package/dist/iife/from-midi.d.ts +0 -1
- package/dist/iife/from-midi.js +0 -15
- package/dist/iife/from-musicxml.d.ts +0 -1
- package/dist/iife/from-musicxml.js +0 -3
- package/dist/iife/full.d.ts +0 -1
- package/dist/iife/full.js +0 -3
|
@@ -3,7 +3,7 @@ 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 { transferFunctionAddons, isVisualTemporalNode, TemporalNodeBase, } from "../temporal.js";
|
|
6
|
-
import {
|
|
6
|
+
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
7
7
|
import { createBeamLayoutAttachment } from "../beam/layout.js";
|
|
8
8
|
import { DIV_ADDON_KEY } from "../div/index.js";
|
|
9
9
|
/** 倚音相对宿主的字号比例 */
|
|
@@ -22,9 +22,17 @@ const SHOULDER_PORT = "shoulder";
|
|
|
22
22
|
* 所以只能在此刻回头改。这发生在 parse 进行中,不违反「AST parse 后只读」。
|
|
23
23
|
*/
|
|
24
24
|
function scaleFontSize(node, factor) {
|
|
25
|
+
if (factor === 1)
|
|
26
|
+
return;
|
|
25
27
|
const sized = node;
|
|
26
28
|
if (typeof sized.size === "number")
|
|
27
29
|
sized.size *= factor;
|
|
30
|
+
if (typeof sized.gap === "number")
|
|
31
|
+
sized.gap *= factor;
|
|
32
|
+
if (typeof sized.minGap === "number")
|
|
33
|
+
sized.minGap *= factor;
|
|
34
|
+
sized.aboveGaps?.forEach((_, i, a) => a[i] *= factor);
|
|
35
|
+
sized.belowGaps?.forEach((_, i, a) => a[i] *= factor);
|
|
28
36
|
for (const child of node.children ?? [])
|
|
29
37
|
scaleFontSize(child, factor);
|
|
30
38
|
}
|
|
@@ -97,51 +105,17 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
97
105
|
if (n.kind !== "sugar" || n.data?.class !== GraceFunction)
|
|
98
106
|
return null;
|
|
99
107
|
const side = n.data.side;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
while (left >= 0 && ctx.nodes[left] instanceof ASTTextNode)
|
|
103
|
-
left--;
|
|
104
|
-
if (left < 0) {
|
|
108
|
+
const leftNode = ctx.takeLeftOperand();
|
|
109
|
+
if (!leftNode) {
|
|
105
110
|
throw new ErrorDiagnostic("E_GRACE_NO_TARGET", "@grace语法糖错误: 左边没有找到目标", n.span);
|
|
106
111
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
tgt = tgt.parent;
|
|
115
|
-
for (let j = left - 1; j >= 0; j--) {
|
|
116
|
-
if (ctx.nodes[j] === tgt) {
|
|
117
|
-
leftNode = new ASTBraceNode({
|
|
118
|
-
start: tgt.sourceSpan.start,
|
|
119
|
-
end: leftNode.sourceSpan.end,
|
|
120
|
-
}, ctx.nodes.slice(j, left + 1), null);
|
|
121
|
-
replaceFrom = j;
|
|
122
|
-
break;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
// 找到下一个非文本节点 通过全量后续解析的方式进行
|
|
127
|
-
const storage = ctx.nodes;
|
|
128
|
-
ctx.nodes = [];
|
|
129
|
-
ctx.makeNodes(nodes, at);
|
|
130
|
-
for (let i = 0; i < ctx.nodes.length; i++) {
|
|
131
|
-
const right = ctx.nodes[i];
|
|
132
|
-
if (right instanceof ASTTextNode)
|
|
133
|
-
continue;
|
|
134
|
-
// '<' 的倚音在右侧,右结合的解析结果本来就是嵌套语义,不必像 '>' 那样重排
|
|
135
|
-
const node = side === "pre"
|
|
136
|
-
? GraceFunction.attachPre(leftNode, right, n.span, ctx)
|
|
137
|
-
: GraceFunction.create(ctx, n.span, "post", leftNode, right);
|
|
138
|
-
storage.length = replaceFrom;
|
|
139
|
-
storage.push(node);
|
|
140
|
-
while (++i < ctx.nodes.length)
|
|
141
|
-
storage.push(ctx.nodes[i]);
|
|
142
|
-
ctx.nodes = storage;
|
|
143
|
-
return nodes.length;
|
|
144
|
-
}
|
|
112
|
+
const next = ctx.consumeRightOperand(nodes, at, right =>
|
|
113
|
+
// '<' 的倚音在右侧,右结合的解析结果本来就是嵌套语义,不必像 '>' 那样重排
|
|
114
|
+
side === "pre"
|
|
115
|
+
? GraceFunction.attachPre(leftNode, right, n.span, ctx)
|
|
116
|
+
: GraceFunction.create(ctx, n.span, "post", leftNode, right));
|
|
117
|
+
if (next !== null)
|
|
118
|
+
return next;
|
|
145
119
|
throw new ErrorDiagnostic("E_GRACE_NO_TARGET", "@grace语法糖错误: 右边没有找到目标", n.span);
|
|
146
120
|
}
|
|
147
121
|
host;
|
|
@@ -161,33 +135,25 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
161
135
|
deepest = deepest.grace;
|
|
162
136
|
depth++;
|
|
163
137
|
}
|
|
164
|
-
const argMap = new Map();
|
|
165
|
-
argMap.set(0, deepest.grace);
|
|
166
|
-
argMap.set(1, left);
|
|
167
138
|
const scale = GRACE_SCALE ** depth;
|
|
168
|
-
const inner =
|
|
139
|
+
const inner = GraceFunction.create(ctx, span, "pre", deepest.grace, left, GRACE_SCALE * scale);
|
|
169
140
|
inner.size *= scale;
|
|
170
141
|
deepest.setGrace(inner, 1); // inner 内部已按深度缩放,不能再缩
|
|
171
142
|
return right;
|
|
172
143
|
}
|
|
173
144
|
return GraceFunction.create(ctx, span, "pre", right, left);
|
|
174
145
|
}
|
|
175
|
-
static create(ctx, span, side, host, grace) {
|
|
146
|
+
static create(ctx, span, side, host, grace, scale = GRACE_SCALE) {
|
|
176
147
|
const argMap = new Map();
|
|
177
148
|
argMap.set(0, host);
|
|
178
149
|
argMap.set(1, grace);
|
|
179
150
|
argMap.set("side", side);
|
|
180
|
-
return new GraceFunction(span, argMap, ctx);
|
|
151
|
+
return new GraceFunction(span, argMap, ctx, null, scale);
|
|
181
152
|
}
|
|
182
|
-
/** 宿主在前:findActiveDivs 下潜子树时要先命中宿主的 @div,而不是倚音的 */
|
|
183
153
|
get children() {
|
|
184
|
-
|
|
185
|
-
if (this.host)
|
|
186
|
-
out.push(this.host);
|
|
187
|
-
if (this.grace)
|
|
188
|
-
out.push(this.grace);
|
|
189
|
-
return out;
|
|
154
|
+
return [this.host, this.grace];
|
|
190
155
|
}
|
|
156
|
+
timeFlowModel() { return null; }
|
|
191
157
|
constructor(span, args, ctx, parent = null, scale = GRACE_SCALE) {
|
|
192
158
|
super(span, parent);
|
|
193
159
|
this.size = ctx.variables.fontsize;
|
|
@@ -210,11 +176,7 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
210
176
|
}
|
|
211
177
|
/** 宿主收敛成一个可见 Temporal,倚音展开成局部时间列,然后整体折叠进一个复合节点 */
|
|
212
178
|
loweringEnter(ctx, track) {
|
|
213
|
-
const host = this
|
|
214
|
-
const grace = this.grace;
|
|
215
|
-
if (!host || !grace) {
|
|
216
|
-
throw new ErrorDiagnostic("E_GRACE_MISSING_ARGS", "@grace 需要同时给出宿主和倚音", this.sourceSpan);
|
|
217
|
-
}
|
|
179
|
+
const { host, grace } = this;
|
|
218
180
|
// 倚音内容是一个子域:在私有根轨上展开,根轨不挂进宿主轨的分组,其中的并行结构另开声部
|
|
219
181
|
const root = new Track(track);
|
|
220
182
|
let hostEvents = [];
|
|
@@ -240,20 +202,10 @@ class GraceFunction extends ASTFunctionNode {
|
|
|
240
202
|
if (!events.some(isVisualTemporalNode) || events.some(event => (event.addon?.breakBefore ?? 0) > 0)) {
|
|
241
203
|
throw new ErrorDiagnostic("E_GRACE_INVALID_CONTENT", "@grace 的倚音至少产生一个可见对象,且不能换行", grace.sourceSpan);
|
|
242
204
|
}
|
|
243
|
-
|
|
244
|
-
// 倚音同段无条件全部相连,只被零时长标记切断,所以 grace 一定认识 beam,允许耦合
|
|
245
|
-
// 折叠成员不进全局时间列,autobeam 看不到它们,要手动调用且强制连接
|
|
246
|
-
for (const run of composite.graceRuns) {
|
|
247
|
-
if (run.length < 2)
|
|
248
|
-
continue;
|
|
249
|
-
ctx.addAttachment(createBeamLayoutAttachment([...run], false, grace.sourceSpan));
|
|
250
|
-
}
|
|
251
|
-
return [composite];
|
|
205
|
+
return [new GraceTemporal(this, hostNode, columns, root, attachments, ctx)];
|
|
252
206
|
}
|
|
253
207
|
toString(source) {
|
|
254
|
-
|
|
255
|
-
const grace = this.grace?.toString(source) ?? "";
|
|
256
|
-
return `@grace(${host}, ${grace}, side=${this.side})`;
|
|
208
|
+
return `@grace(${this.host.toString(source)}, ${this.grace.toString(source)}, side=${this.side})`;
|
|
257
209
|
}
|
|
258
210
|
}
|
|
259
211
|
export const GraceNode = GraceFunction;
|
|
@@ -274,9 +226,6 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
274
226
|
graceColumns;
|
|
275
227
|
/** graceColumns 按列展开 */
|
|
276
228
|
graces;
|
|
277
|
-
/** 承担节奏的倚音成员,按声部与零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线 */
|
|
278
|
-
graceRuns;
|
|
279
|
-
side;
|
|
280
229
|
/** 倚音子域的根轨:由宿主轨分出,不挂进它的分组,子域之外不可见 */
|
|
281
230
|
root;
|
|
282
231
|
/** 倚音内容里声明的附件,随子域排版 */
|
|
@@ -290,13 +239,12 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
290
239
|
graceOffsets = [];
|
|
291
240
|
/** 倚音线起点,同样是局部偏移 */
|
|
292
241
|
hookOrigin = null;
|
|
293
|
-
constructor(ast, host, columns, root, attachments,
|
|
242
|
+
constructor(ast, host, columns, root, attachments, ctx) {
|
|
294
243
|
super();
|
|
295
244
|
this.ast = ast;
|
|
296
245
|
this.host = host;
|
|
297
246
|
this.root = root;
|
|
298
247
|
this.attachments = attachments;
|
|
299
|
-
this.side = side;
|
|
300
248
|
this.T.copyFrom(host.T);
|
|
301
249
|
this.mergeKey = host.mergeKey;
|
|
302
250
|
this.initLayoutBox();
|
|
@@ -327,6 +275,8 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
327
275
|
this.graces = graceColumns.flat();
|
|
328
276
|
this.events = events;
|
|
329
277
|
this.starts = starts;
|
|
278
|
+
// 承担节奏的倚音成员按声部与零时长标记切段;同段视觉上首尾相接,无条件连成一束减时线
|
|
279
|
+
// 折叠成员不进全局时间列,autobeam 看不到它们,要手动调用且强制连接
|
|
330
280
|
const runs = new Map();
|
|
331
281
|
const graceRuns = [];
|
|
332
282
|
for (const grace of this.graces) {
|
|
@@ -346,14 +296,17 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
346
296
|
addon[DIV_ADDON_KEY] = (Number(addon[DIV_ADDON_KEY]) || 0) + 1;
|
|
347
297
|
grace.T.divPow2();
|
|
348
298
|
}
|
|
349
|
-
|
|
299
|
+
for (const run of graceRuns) {
|
|
300
|
+
if (run.length >= 2)
|
|
301
|
+
ctx.addAttachment(createBeamLayoutAttachment(run, false, ast.grace.sourceSpan));
|
|
302
|
+
}
|
|
350
303
|
}
|
|
351
304
|
/** 成员共享全局时间状态,按发声顺序固化 */
|
|
352
305
|
onTimeState(state) {
|
|
353
306
|
// 同步时间的修改。比如tuplet修改的是GraceTemporal.T,这里要把它传给成员
|
|
354
307
|
this.host.T.copyFrom(this.T);
|
|
355
308
|
let offset = undefined;
|
|
356
|
-
for (const member of this.side === "pre" ? [...this.events, this.host] : [this.host, ...this.events]) {
|
|
309
|
+
for (const member of this.ast.side === "pre" ? [...this.events, this.host] : [this.host, ...this.events]) {
|
|
357
310
|
member.t.copyFrom(this.t);
|
|
358
311
|
member.layoutLine = this.layoutLine;
|
|
359
312
|
const result = member.onTimeState?.(state);
|
|
@@ -385,13 +338,14 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
385
338
|
steal.copyFrom(limit);
|
|
386
339
|
}
|
|
387
340
|
const hostDuration = total.sub(steal);
|
|
388
|
-
const
|
|
389
|
-
|
|
341
|
+
const pre = this.ast.side === "pre";
|
|
342
|
+
const base = pre ? emitter.start.clone() : emitter.start.clone().add(hostDuration);
|
|
343
|
+
if (!pre)
|
|
390
344
|
emitter.play(this.host, emitter.start.clone(), hostDuration);
|
|
391
345
|
this.events.forEach((event, index) => {
|
|
392
346
|
emitter.play(event, this.starts[index].clone().mul(scale).add(base), event.T.clone().mul(scale));
|
|
393
347
|
});
|
|
394
|
-
if (
|
|
348
|
+
if (pre)
|
|
395
349
|
emitter.play(this.host, base.add(steal), hostDuration);
|
|
396
350
|
}
|
|
397
351
|
/**
|
|
@@ -406,19 +360,20 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
406
360
|
transferFunctionAddons(this, this.host);
|
|
407
361
|
const block = context.layoutSubdomain(this, this.graceColumns, this.root, this.attachments);
|
|
408
362
|
const hostWidth = layoutLocalSequence([this.host], context);
|
|
409
|
-
this.springConfig = { ...this.host.springConfig
|
|
363
|
+
this.springConfig = { ...this.host.springConfig };
|
|
410
364
|
const graceEm = this.ast.size * GRACE_SCALE;
|
|
411
365
|
const sideGap = graceEm * 0.2;
|
|
412
366
|
const rise = graceEm * GRACE_RISE;
|
|
367
|
+
const pre = this.ast.side === "pre";
|
|
413
368
|
// 倚音贴着宿主的肩线而不是宿主盒顶:宿主本身也是复合体时,
|
|
414
369
|
// 肩线由它转发上来,所以两侧倚音会落在同一高度而不是层层叠高
|
|
415
370
|
const shoulder = this.host.ports[SHOULDER_PORT]?.y ?? 0;
|
|
416
371
|
const graceTop = shoulder - rise - block.height;
|
|
417
372
|
const hostBounds = getLayoutBounds(this.host);
|
|
418
373
|
const lift = Math.max(0, -graceTop, -hostBounds.y);
|
|
419
|
-
const graceX =
|
|
374
|
+
const graceX = pre ? 0 : hostWidth + sideGap;
|
|
420
375
|
this.hostOffset = {
|
|
421
|
-
x: this.host.box.x + (
|
|
376
|
+
x: this.host.box.x + (pre ? block.width + sideGap : 0),
|
|
422
377
|
y: lift,
|
|
423
378
|
};
|
|
424
379
|
this.graceOffsets = block.positions.map(position => ({
|
|
@@ -442,7 +397,7 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
442
397
|
this.ports[SHOULDER_PORT] ??= { x: this.box.anchor, y: this.hostOffset.y };
|
|
443
398
|
// 钩形曲线挂在靠近宿主的那个倚音成员下方,零时长标记不承接它;高度取成员的最低下沿,不含块内附件
|
|
444
399
|
const rhythmic = this.graces.filter(grace => !grace.T.isZero());
|
|
445
|
-
const near =
|
|
400
|
+
const near = pre ? rhythmic.at(-1) : rhythmic[0];
|
|
446
401
|
this.hookOrigin = near ? {
|
|
447
402
|
x: this.graceOffsets[this.graces.indexOf(near)].x + near.box.anchor,
|
|
448
403
|
y: Math.max(...this.graces.map((grace, index) => this.graceOffsets[index].y + grace.box.h)),
|
|
@@ -469,7 +424,7 @@ export class GraceTemporal extends TemporalNodeBase {
|
|
|
469
424
|
return;
|
|
470
425
|
const graceEm = this.ast.size * GRACE_SCALE;
|
|
471
426
|
const hookOrigin = this.hookOrigin;
|
|
472
|
-
const dir = this.side === "pre" ? 1 : -1;
|
|
427
|
+
const dir = this.ast.side === "pre" ? 1 : -1;
|
|
473
428
|
painter.drawPath(HOOK_COMMANDS, { stroke: "#000", strokeWidth: Math.max(0.8, graceEm * HOOK_WIDTH) }, {
|
|
474
429
|
x: this.box.x + hookOrigin.x,
|
|
475
430
|
y: this.box.y + hookOrigin.y,
|
|
@@ -57,6 +57,7 @@ class HeadBoundaryTemporal extends TemporalNodeBase {
|
|
|
57
57
|
members = [];
|
|
58
58
|
slots = [];
|
|
59
59
|
titleBottom = 0;
|
|
60
|
+
aligned = false; // 侧栏横向重排在一轮布局中只执行一次,避免二次 onPlaced 把成员微调后的 box.x 当成未偏移基准
|
|
60
61
|
sideExtents = { left: null, right: null };
|
|
61
62
|
constructor(ast) {
|
|
62
63
|
super();
|
|
@@ -70,15 +71,20 @@ class HeadBoundaryTemporal extends TemporalNodeBase {
|
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
}
|
|
73
|
-
prepareLayout() {
|
|
74
|
+
prepareLayout() {
|
|
75
|
+
this.box.w = this.box.h = this.box.anchor = this.box.visualAxis = 0;
|
|
76
|
+
this.aligned = false;
|
|
77
|
+
}
|
|
74
78
|
prepareHorizontal(line) {
|
|
75
79
|
// 只由第一个边界协调一次整块 Head,避免六个边界重复改弹簧
|
|
76
80
|
if (this.slots.length > 0)
|
|
77
81
|
this.prepareHead(line);
|
|
78
82
|
}
|
|
79
83
|
onPlaced() {
|
|
80
|
-
if (this.slots.length > 0)
|
|
84
|
+
if (this.slots.length > 0 && !this.aligned) {
|
|
85
|
+
this.aligned = true;
|
|
81
86
|
this.alignHead();
|
|
87
|
+
}
|
|
82
88
|
}
|
|
83
89
|
onTemporal(node) { this.members.push(node); }
|
|
84
90
|
createTracks(slot) {
|
|
@@ -152,7 +158,7 @@ class HeadBoundaryTemporal extends TemporalNodeBase {
|
|
|
152
158
|
cells.set(cell, [member]);
|
|
153
159
|
}
|
|
154
160
|
const [left, , right] = this.slots;
|
|
155
|
-
this.packRows(left.ast, left.start.box.x, cells);
|
|
161
|
+
this.packRows(left.ast, left.start.box.x - (left.start.placementOffset?.x ?? 0), cells);
|
|
156
162
|
this.packRows(right.ast, right.end.box.x, cells);
|
|
157
163
|
}
|
|
158
164
|
stretch(node, side) {
|
|
@@ -325,9 +331,6 @@ H.tempo: 94
|
|
|
325
331
|
gap;
|
|
326
332
|
createdBySugar = false;
|
|
327
333
|
get children() { return this.slots; }
|
|
328
|
-
timeFlowModel() {
|
|
329
|
-
return { mode: "sequence", children: this.slots };
|
|
330
|
-
}
|
|
331
334
|
static deSugarAtom(source, start, end) {
|
|
332
335
|
if (source[start] !== "H" || source[start + 1] !== ".")
|
|
333
336
|
return null;
|
|
@@ -2,17 +2,15 @@ import { DEFAULT_TONALITY, TemporalNodeBase } from "../temporal.js";
|
|
|
2
2
|
import { ASTFunctionNode } from "../ASTtypes.js";
|
|
3
3
|
import { ErrorDiagnostic, WarningDiagnostic } from "../../diagnostic.js";
|
|
4
4
|
import { parseNoteName } from "../note/noteName.js";
|
|
5
|
-
import { acc2Offset, NoteNameMap
|
|
5
|
+
import { acc2Offset, NoteNameMap } from "../../parser/parse-utils/note-utils.js";
|
|
6
6
|
import { paintAccidental, placeAccidentals } from "../note/accidentals.js";
|
|
7
7
|
/**
|
|
8
8
|
* 把宽松的调性写法收敛成 tonality2Midi 认得的 [音名][最多一个 #b][绝对八度]
|
|
9
9
|
*
|
|
10
|
-
*
|
|
10
|
+
* 彻底读不懂返回 null
|
|
11
11
|
* `C###` 这种超过一个变音记号的写法按实际音高换字母(→ D#4),落在黑键上则跟随原来的升降方向拼写
|
|
12
12
|
*/
|
|
13
13
|
function normalizeTonality(tonality) {
|
|
14
|
-
if (tonality2Midi(tonality) !== null)
|
|
15
|
-
return tonality;
|
|
16
14
|
const normalizedInput = tonality.replace(/^[a-g]/, letter => letter.toUpperCase());
|
|
17
15
|
const parsed = parseNoteName(normalizedInput, 0, normalizedInput.length, false);
|
|
18
16
|
if (!parsed || parsed.next !== normalizedInput.length)
|
|
@@ -23,6 +21,8 @@ function normalizeTonality(tonality) {
|
|
|
23
21
|
const offset = acc2Offset(parsed.acc ?? "", false);
|
|
24
22
|
const octave = parsed.absOctave ? parsed.octave ?? 4 : 4 + (parsed.octave ?? 0);
|
|
25
23
|
const midi = (octave + 1) * 12 + pitchClass + offset;
|
|
24
|
+
if (!Number.isSafeInteger(midi))
|
|
25
|
+
return null;
|
|
26
26
|
// NoteNameMap 里同一音高的升号拼写在前、降号在后,正好用来挑方向;数字音名不参与拼写
|
|
27
27
|
const spellings = Object.keys(NoteNameMap).filter(name => NoteNameMap[name] === ((midi % 12) + 12) % 12 && /^[A-G]/.test(name));
|
|
28
28
|
return (offset < 0 ? spellings.at(-1) : spellings[0]) + (Math.floor(midi / 12) - 1);
|
|
@@ -68,10 +68,10 @@ class KeyFunction extends ASTFunctionNode {
|
|
|
68
68
|
this.tonality = tonality;
|
|
69
69
|
this.size = ctx.length2px(size);
|
|
70
70
|
const normalized = normalizeTonality(tonality);
|
|
71
|
-
|
|
71
|
+
this.tonality = normalized ?? DEFAULT_TONALITY;
|
|
72
|
+
if (normalized !== tonality && normalized !== `${tonality}4`) {
|
|
72
73
|
if (ctx.variables.strict)
|
|
73
74
|
throw new ErrorDiagnostic("E_KEY_TONALITY", '@key 无法解析调性 "{tonality}"', sourceSpan, { tonality });
|
|
74
|
-
this.tonality = normalized ?? DEFAULT_TONALITY;
|
|
75
75
|
ctx.diagnostics.push(new WarningDiagnostic("W_KEY_TONALITY", '@key 的调性 "{input}" 不是规范写法,已自动切换到 {tonality}', sourceSpan, { input: tonality, tonality: this.tonality }));
|
|
76
76
|
}
|
|
77
77
|
// 归一化保证了格式是 [音名][最多一个 #b][八度]
|
|
@@ -9,8 +9,8 @@ const changesByPosition = new WeakMap();
|
|
|
9
9
|
export function meterChangeAt(node) {
|
|
10
10
|
return changesByPosition.get(node)?.meter;
|
|
11
11
|
}
|
|
12
|
-
function recordMeasureChange(
|
|
13
|
-
const position =
|
|
12
|
+
function recordMeasureChange(node, meter) {
|
|
13
|
+
const position = node.rootHost;
|
|
14
14
|
const span = node.ast.sourceSpan;
|
|
15
15
|
let change = changesByPosition.get(position);
|
|
16
16
|
if (!change)
|
|
@@ -154,7 +154,7 @@ class MeterTemporal extends TemporalNodeBase {
|
|
|
154
154
|
}
|
|
155
155
|
onTimeState(state) {
|
|
156
156
|
state.meter = this.ast.measureDuration;
|
|
157
|
-
recordMeasureChange(
|
|
157
|
+
recordMeasureChange(this, this.ast);
|
|
158
158
|
}
|
|
159
159
|
emitPlayback(emitter) {
|
|
160
160
|
if (!this.ast.measureDuration)
|
|
@@ -275,7 +275,6 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
275
275
|
const keySignature = this.keySignature = state.keySignature;
|
|
276
276
|
this.playbackState = { bpm: state.bpm, velocity: state.velocity, program: state.program };
|
|
277
277
|
this.resolvedMidi = resolveNoteMidi(this.name, this.acc, this.octave, keySignature);
|
|
278
|
-
this.transposeDiatonic = createDiatonicTranspose(this.name, this.acc, this.octave, keySignature);
|
|
279
278
|
// 数字音名本身就是简谱显示形式,直接保留原始数字和相对八度
|
|
280
279
|
if (this.name >= "0" && this.name <= "9") {
|
|
281
280
|
if (this.name === "0" || this.name >= "8") {
|
|
@@ -292,6 +291,7 @@ class NoteTemporalNode extends TemporalNodeBase {
|
|
|
292
291
|
this.octave = jianpuPitch.renderOctave;
|
|
293
292
|
}
|
|
294
293
|
}
|
|
294
|
+
this.transposeDiatonic = createDiatonicTranspose(this.name, this.octave, keySignature);
|
|
295
295
|
}
|
|
296
296
|
/** 发布完整音段或无声区间,实际 NoteOn/NoteOff 留到声音展开完成后生成 */
|
|
297
297
|
emitPlayback(emitter) {
|
|
@@ -21,8 +21,7 @@ function targetPitch(note, operation) {
|
|
|
21
21
|
const spelling = operation === "semitone-down" ? FLAT_NOTE_NAMES : SHARP_NOTE_NAMES;
|
|
22
22
|
let name, acc, octave;
|
|
23
23
|
if (operation === "to-absolute") {
|
|
24
|
-
const
|
|
25
|
-
const base = tonicLetter < 0 ? LETTERS.indexOf(SHARP_NOTE_NAMES[mod(tonic, 12)][0]) : tonicLetter;
|
|
24
|
+
const base = LETTERS.indexOf(note.keySignature[0]);
|
|
26
25
|
const degree = Number(note.ast.name) - 1;
|
|
27
26
|
name = LETTERS[(base + degree) % 7];
|
|
28
27
|
const tonicAcc = mod(tonic - NoteNameMap[LETTERS[base]] + 6, 12) - 6;
|
|
@@ -32,9 +31,7 @@ function targetPitch(note, operation) {
|
|
|
32
31
|
}
|
|
33
32
|
else if (operation === "to-relative") {
|
|
34
33
|
const ast = note.ast;
|
|
35
|
-
const
|
|
36
|
-
: SHARP_NOTE_NAMES[mod(tonic, 12)] + (Math.floor(tonic / 12) - 1);
|
|
37
|
-
const relative = resolveLetterNameToJianpu(ast.name, ast.acc, ast.octave + ast.jeOctaveOffset, key);
|
|
34
|
+
const relative = resolveLetterNameToJianpu(ast.name, ast.acc, ast.octave + ast.jeOctaveOffset, note.keySignature);
|
|
38
35
|
if (!relative)
|
|
39
36
|
fail(ast.sourceSpan);
|
|
40
37
|
({ renderName: name, renderAcc: acc, renderOctave: octave } = relative);
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { Diagnostic, ErrorDiagnostic } from "../../diagnostic.js";
|
|
2
2
|
import { unionLayoutBoxes } from "../../layout/engine.js";
|
|
3
|
+
import { lineEntryX, lineRightX } from "../../layout/range.js";
|
|
3
4
|
import { ASTFunctionNode, } from "../ASTtypes.js";
|
|
4
5
|
import { isVisualTemporalNode } from "../temporal.js";
|
|
5
|
-
function outerHost(node) {
|
|
6
|
-
while (node.foldedInto)
|
|
7
|
-
node = node.foldedInto;
|
|
8
|
-
return node;
|
|
9
|
-
}
|
|
10
6
|
class OttavaFunction extends ASTFunctionNode {
|
|
11
7
|
static def = {
|
|
12
8
|
name: ["ottava"],
|
|
@@ -59,7 +55,7 @@ class OttavaFunction extends ASTFunctionNode {
|
|
|
59
55
|
for (const node of nodes) {
|
|
60
56
|
if (!node.playbackState)
|
|
61
57
|
continue;
|
|
62
|
-
const host =
|
|
58
|
+
const host = node.rootHost;
|
|
63
59
|
const ottavas = ottavasByTrack.get(host.track);
|
|
64
60
|
if (!ottavas)
|
|
65
61
|
continue;
|
|
@@ -98,10 +94,8 @@ class OttavaFunction extends ASTFunctionNode {
|
|
|
98
94
|
this.font = ctx.variables.numberfont;
|
|
99
95
|
}
|
|
100
96
|
loweringEnter(ctx) {
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
const head = from && outerHost(from);
|
|
104
|
-
const tail = to && outerHost(to);
|
|
97
|
+
const head = ctx.getTemporalNodes(this.from).at(0)?.rootHost;
|
|
98
|
+
const tail = ctx.getTemporalNodes(this.to).at(-1)?.rootHost;
|
|
105
99
|
if (!head || !tail || !isVisualTemporalNode(head) || !isVisualTemporalNode(tail)) {
|
|
106
100
|
ctx.diagnostics.push(Diagnostic.warning.UnresolvedEndpoint("ottava", this.sourceSpan));
|
|
107
101
|
return [];
|
|
@@ -121,22 +115,16 @@ class OttavaFunction extends ASTFunctionNode {
|
|
|
121
115
|
export const OttavaNode = OttavaFunction;
|
|
122
116
|
class OttavaAttachment {
|
|
123
117
|
ast;
|
|
124
|
-
from;
|
|
125
|
-
to;
|
|
126
118
|
layer = "foreground";
|
|
119
|
+
endPoints;
|
|
127
120
|
constructor(ast, from, to) {
|
|
128
121
|
this.ast = ast;
|
|
129
|
-
this.
|
|
130
|
-
|
|
122
|
+
this.endPoints = (from.t.compare(to.t) || from.order - to.order) > 0
|
|
123
|
+
? [to, from] : [from, to];
|
|
131
124
|
}
|
|
132
125
|
get sourceSpan() { return this.ast.sourceSpan; }
|
|
133
|
-
get endPoints() {
|
|
134
|
-
return (this.from.t.compare(this.to.t) || this.from.order - this.to.order) > 0
|
|
135
|
-
? [this.to, this.from] : [this.from, this.to];
|
|
136
|
-
}
|
|
137
126
|
createGeometry(context) {
|
|
138
|
-
const
|
|
139
|
-
const [from, to] = endpoints;
|
|
127
|
+
const [from, to] = this.endPoints;
|
|
140
128
|
const firstLine = from.layoutLine;
|
|
141
129
|
const lastLine = to.layoutLine;
|
|
142
130
|
if (context.lines[firstLine].columnOf(from) < 0 || context.lines[lastLine].columnOf(to) < 0) {
|
|
@@ -168,17 +156,8 @@ class OttavaAttachment {
|
|
|
168
156
|
const line = firstLine + offset;
|
|
169
157
|
const first = line === firstLine;
|
|
170
158
|
const last = line === lastLine;
|
|
171
|
-
|
|
172
|
-
let
|
|
173
|
-
: runs.get(track)?.find(host => !host.T.isZero())?.box.x ?? context.originX;
|
|
174
|
-
let right = to.box.x + (to.ports["body.right"]?.x ?? to.box.w);
|
|
175
|
-
if (!last) {
|
|
176
|
-
right = context.originX + (runs.size === 0 ? context.width : 0);
|
|
177
|
-
for (const run of runs.values()) {
|
|
178
|
-
for (const host of run)
|
|
179
|
-
right = Math.max(right, host.box.x + host.box.w);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
159
|
+
let left = first ? from.box.x + (from.ports["body.left"]?.x ?? 0) : lineEntryX(context, line, track);
|
|
160
|
+
let right = last ? to.box.x + (to.ports["body.right"]?.x ?? to.box.w) : lineRightX(context, line);
|
|
182
161
|
if (left > right)
|
|
183
162
|
[left, right] = [right, left];
|
|
184
163
|
const lineStart = first ? left + metrics.w + labelGap : left;
|
|
@@ -201,7 +180,7 @@ class OttavaAttachment {
|
|
|
201
180
|
}
|
|
202
181
|
}
|
|
203
182
|
if (!Number.isFinite(lineY)) {
|
|
204
|
-
const extent = context.getRangeExtents(line, first || last ?
|
|
183
|
+
const extent = context.getRangeExtents(line, first || last ? this.endPoints : undefined, track);
|
|
205
184
|
const axis = context.getVisualAxis(line, track);
|
|
206
185
|
lineY = above
|
|
207
186
|
? axis + (extent?.top ?? 0) - gap - (region.y + region.h)
|
|
@@ -51,7 +51,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
51
51
|
~~~jpfun
|
|
52
52
|
@page(
|
|
53
53
|
width=794px, height=1123px, top=48px, bottom=48px,
|
|
54
|
-
left=40px, right=40px, gap=1em, fillRatio=0.5,
|
|
54
|
+
left=40px, right=40px, gap=1em, minGap=0.5em, fillRatio=0.5,
|
|
55
55
|
numbering="1/1", barNumbering="(1)"
|
|
56
56
|
)
|
|
57
57
|
~~~
|
|
@@ -64,9 +64,9 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
64
64
|
{ name: "bottom", description: "页面下边距,启用页码时至少为 `16px`", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginBottom) },
|
|
65
65
|
{ name: "left", description: "页面左边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginLeft) },
|
|
66
66
|
{ name: "right", description: "页面右边距", type: "length", default: px(DEFAULT_PAGE_CONFIG.marginRight) },
|
|
67
|
-
{ name: "gap", description: "
|
|
68
|
-
|
|
69
|
-
{ name: "numbering", description: "页码格式,空值隐藏;`1
|
|
67
|
+
{ name: "gap", description: "谱面行主旋律之间的标准间距", type: "length", default: { value: 1, unit: "em" } },
|
|
68
|
+
{ name: "minGap", description: "行间保留的最小间距", type: "length", default: { value: 0.5, unit: "em" } },
|
|
69
|
+
{ name: "numbering", description: "页码格式,空值隐藏;`1`:当前页,`1/1`:显示当前页/总页数", type: "string", default: "" },
|
|
70
70
|
{ name: "barNumbering", description: "行首小节号格式,默认空值隐藏;`1` 显示当前小节号,`(1)` 显示带括号的小节号", type: "string", default: "" },
|
|
71
71
|
{ name: "font", description: "页码/小节号字体,空值沿用当前字体设置", type: "string", default: "" },
|
|
72
72
|
{ name: "fillRatio", description: "自然宽度达到该比例后撑满", type: "number", default: DEFAULT_PAGE_CONFIG.fillRatio },
|
|
@@ -90,18 +90,15 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
90
90
|
return;
|
|
91
91
|
}
|
|
92
92
|
ctx.documentDeclarations["page"] = true;
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const barNumbering = values.pop();
|
|
97
|
-
const numbering = values.pop();
|
|
98
|
-
const [width, height, top, bottom, left, right, gap] = values.map(value => ctx.length2px(value));
|
|
93
|
+
const [w, h, t, b, l, r, g, mg, numbering, barNumbering, font, fillRatio,] = this.getArgValue(args, ctx);
|
|
94
|
+
this.font = font || ctx.variables.font;
|
|
95
|
+
const [width, height, top, bottom, left, right, gap, minGap] = [w, h, t, b, l, r, g, mg].map(value => ctx.length2px(value));
|
|
99
96
|
if (!Number.isFinite(width) || width <= 0)
|
|
100
97
|
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的 width 必须是正有限长度", sourceSpan);
|
|
101
98
|
if (!Number.isFinite(height))
|
|
102
99
|
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的 height 必须是有限长度;小于等于 0 表示不自动分页", sourceSpan);
|
|
103
|
-
if ([top, bottom, left, right, gap].some(value => !Number.isFinite(value) || value < 0))
|
|
104
|
-
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page
|
|
100
|
+
if ([top, bottom, left, right, gap, minGap].some(value => !Number.isFinite(value) || value < 0))
|
|
101
|
+
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的边距、gap 和 minGap 必须是非负有限长度", sourceSpan);
|
|
105
102
|
if (left + right >= width)
|
|
106
103
|
throw new ErrorDiagnostic("E_INVALID_PAGE_CONFIG", "@page 的左右边距没有留下可用内容宽度", sourceSpan);
|
|
107
104
|
if (height > 0 && top + bottom >= height)
|
|
@@ -116,6 +113,7 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
116
113
|
marginLeft: left,
|
|
117
114
|
marginRight: right,
|
|
118
115
|
lineGap: gap,
|
|
116
|
+
minLineGap: minGap,
|
|
119
117
|
fillRatio,
|
|
120
118
|
});
|
|
121
119
|
this.numbering = numbering;
|
|
@@ -128,16 +126,15 @@ export class PageFunction extends ASTFunctionNode {
|
|
|
128
126
|
if (this.numbering) {
|
|
129
127
|
ctx.addAttachment(new PageNumberAttachment(this.sourceSpan, this.numbering, this.config.marginBottom, { ...PAGE_NUMBER_STYLE, fontFamily: this.font }));
|
|
130
128
|
}
|
|
131
|
-
if (this.barNumbering)
|
|
129
|
+
if (this.barNumbering)
|
|
132
130
|
registerBarNumbering(ctx, this.barNumbering, this.font);
|
|
133
|
-
}
|
|
134
131
|
return [];
|
|
135
132
|
}
|
|
136
133
|
toString() {
|
|
137
134
|
if (!this.config)
|
|
138
135
|
return "@page()";
|
|
139
|
-
const { width, height, marginTop, marginBottom, marginLeft, marginRight, lineGap, fillRatio } = this.config;
|
|
140
|
-
return `@page(width=${width}px, height=${Number.isFinite(height) ? height : 0}px, top=${marginTop}px, bottom=${marginBottom}px, left=${marginLeft}px, right=${marginRight}px, gap=${lineGap}px, numbering=${quote(this.numbering)}, barNumbering=${quote(this.barNumbering)}, font=${quote(this.font)}, fillRatio=${fillRatio})`;
|
|
136
|
+
const { width, height, marginTop, marginBottom, marginLeft, marginRight, lineGap, minLineGap, fillRatio } = this.config;
|
|
137
|
+
return `@page(width=${width}px, height=${Number.isFinite(height) ? height : 0}px, top=${marginTop}px, bottom=${marginBottom}px, left=${marginLeft}px, right=${marginRight}px, gap=${lineGap}px, minGap=${minLineGap}px, numbering=${quote(this.numbering)}, barNumbering=${quote(this.barNumbering)}, font=${quote(this.font)}, fillRatio=${fillRatio})`;
|
|
141
138
|
}
|
|
142
139
|
}
|
|
143
140
|
export const PageNode = PageFunction;
|
|
@@ -75,10 +75,8 @@ class RestFunction extends ASTFunctionNode {
|
|
|
75
75
|
if (!(node.ast instanceof ASTFunctionNode)
|
|
76
76
|
|| (node.ast.callName !== "note" && node.ast.callName !== "dash"))
|
|
77
77
|
continue;
|
|
78
|
-
let host = node;
|
|
79
78
|
// 沿折叠链找到最外层宿主,将内部成员归到同一主体
|
|
80
|
-
|
|
81
|
-
host = host.foldedInto;
|
|
79
|
+
const host = node.rootHost;
|
|
82
80
|
// 私有倚音属于独立轨道,不代表外层宿主的休止或起音
|
|
83
81
|
if (node.track !== host.track)
|
|
84
82
|
continue;
|
|
@@ -153,7 +151,7 @@ class RestTemporal extends TemporalNodeBase {
|
|
|
153
151
|
}
|
|
154
152
|
this.measureDuration.copyFrom(measure);
|
|
155
153
|
this.T.copyFrom(measure).mul(this.ast.count);
|
|
156
|
-
const host =
|
|
154
|
+
const host = this.rootHost;
|
|
157
155
|
// 只登记全局休止宿主的同轨成员,私有倚音不接管宿主。
|
|
158
156
|
if (host.mergeKey === REST_KEY && this.track === host.track) {
|
|
159
157
|
const rest = restsByHost.get(host);
|
|
@@ -22,7 +22,6 @@ class SlideFunction extends ASTFunctionNode {
|
|
|
22
22
|
side;
|
|
23
23
|
direction;
|
|
24
24
|
get children() { return [this.content]; }
|
|
25
|
-
timeFlowModel() { return { children: this.children, mode: "sequence" }; }
|
|
26
25
|
labelable() { return ASTFunctionNode.findLabelable(this.content); }
|
|
27
26
|
constructor(span, args, ctx, parent = null) {
|
|
28
27
|
super(span, parent);
|