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.
- package/dist/functions/ASTtypes.d.ts +4 -0
- package/dist/functions/ASTtypes.js +25 -14
- package/dist/functions/adjust/index.js +6 -2
- package/dist/functions/arpeggio/index.js +19 -12
- package/dist/functions/beam/auto.js +2 -11
- package/dist/functions/box/index.js +52 -1
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.js +2 -3
- package/dist/functions/grace/index.d.ts +4 -2
- package/dist/functions/grace/index.js +16 -5
- package/dist/functions/head/index.js +11 -4
- package/dist/functions/meter/index.js +26 -6
- package/dist/functions/meter/shape.d.ts +3 -0
- package/dist/functions/meter/shape.js +43 -0
- package/dist/functions/note/index.js +25 -45
- package/dist/functions/rest/index.d.ts +2 -0
- package/dist/functions/rest/index.js +233 -0
- package/dist/functions/symbol/ornament.js +1 -1
- package/dist/functions/temporal.d.ts +21 -7
- package/dist/functions/temporal.js +14 -4
- package/dist/functions/tie/index.js +14 -10
- package/dist/functions/tuplet/index.js +1 -1
- package/dist/functions/up/index.js +47 -27
- package/dist/functions/voice/connections.d.ts +19 -0
- package/dist/functions/voice/connections.js +88 -0
- package/dist/functions/voice/index.js +244 -99
- package/dist/jpfun.core.min.js +56 -39
- package/dist/jpfun.min.js +65 -48
- package/dist/layout/engine.d.ts +3 -1
- package/dist/layout/engine.js +190 -64
- package/dist/layout/model.js +22 -66
- package/dist/layout/page.js +1 -1
- package/dist/layout/types.d.ts +30 -45
- package/dist/lowering/loweringContext.d.ts +1 -1
- package/dist/lowering/loweringContext.js +34 -4
- package/dist/parser/parserContext.js +4 -8
- package/dist/parser/preprocess.js +4 -6
- package/dist/playback/compile.js +1 -1
- package/dist/render/paint.js +1 -1
- package/grammar.md +200 -257
- package/package.json +1 -1
package/dist/layout/engine.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type VisualTemporalNode } from "../functions/temporal.js";
|
|
|
3
3
|
import type { LoweringResult } from "../lowering/types.js";
|
|
4
4
|
import { type SolverOptions } from "./model.js";
|
|
5
5
|
import { type DocumentLayoutPage } from "./page.js";
|
|
6
|
-
import type { HorizontalLineView, LayoutPrepareContext, PlacedAttachment, Rect } from "./types.js";
|
|
6
|
+
import type { HorizontalLineView, LayoutHost, LayoutPrepareContext, PlacedAttachment, Rect } from "./types.js";
|
|
7
7
|
export interface DocumentLayoutOptions extends SolverOptions {
|
|
8
8
|
rowGap?: number;
|
|
9
9
|
}
|
|
@@ -17,6 +17,8 @@ export interface DocumentLayoutResult {
|
|
|
17
17
|
lineCount: number;
|
|
18
18
|
pages: DocumentLayoutPage[];
|
|
19
19
|
}
|
|
20
|
+
/** 内容边界不含纯留白,完整占用还包含装饰预留的上下空间 */
|
|
21
|
+
export declare function getLayoutBounds(host: LayoutHost, contentOnly?: boolean): Rect;
|
|
20
22
|
/**
|
|
21
23
|
* 把多个已经定位的盒子合并到 target
|
|
22
24
|
*/
|
package/dist/layout/engine.js
CHANGED
|
@@ -9,6 +9,18 @@ import { TranslatingPainter } from "../render/translate.js";
|
|
|
9
9
|
export { PageLayoutError } from "./page.js";
|
|
10
10
|
/** 轨道间距相对行内最大字号的比例;正文行与子域共用 */
|
|
11
11
|
const ROW_GAP_RATIO = 0.75;
|
|
12
|
+
// 装饰边界是本轮排列结果,不作为主体上的第二套空间协议
|
|
13
|
+
const decorationExtents = new WeakMap();
|
|
14
|
+
/** 内容边界不含纯留白,完整占用还包含装饰预留的上下空间 */
|
|
15
|
+
export function getLayoutBounds(host, contentOnly = false) {
|
|
16
|
+
const { box } = host;
|
|
17
|
+
const extent = decorationExtents.get(host);
|
|
18
|
+
if (!extent)
|
|
19
|
+
return box;
|
|
20
|
+
const top = contentOnly ? extent.contentTop : extent.top;
|
|
21
|
+
const bottom = contentOnly ? 0 : extent.bottom;
|
|
22
|
+
return { x: box.x, y: box.y + top, w: box.w, h: box.h - top + bottom };
|
|
23
|
+
}
|
|
12
24
|
/**
|
|
13
25
|
* 把多个已经定位的盒子合并到 target
|
|
14
26
|
*/
|
|
@@ -48,21 +60,25 @@ export function unionLayoutBoxes(target, boxes) {
|
|
|
48
60
|
*/
|
|
49
61
|
export function prepareLayoutHost(node, context) {
|
|
50
62
|
node.springConfig ??= {};
|
|
63
|
+
if (node.decorations)
|
|
64
|
+
decorationExtents.delete(node);
|
|
51
65
|
node.decorations = [];
|
|
52
66
|
node.ports = {};
|
|
53
67
|
node.box.x = node.box.y = 0;
|
|
54
68
|
node.prepareLayout(context); // 计算大小等 layout 需要的参数
|
|
55
69
|
// 把 lowering 固化在 addon 中的语义交给已注册 handler,生成本轮函数装饰
|
|
56
|
-
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
70
|
+
if (node.addon) {
|
|
71
|
+
for (const [key, value] of Object.entries(node.addon)) {
|
|
72
|
+
const handler = context.decorationHandlers.get(key);
|
|
73
|
+
if (!handler)
|
|
74
|
+
continue;
|
|
75
|
+
const decoration = handler(node, value, context);
|
|
76
|
+
if (decoration)
|
|
77
|
+
node.decorations.push(decoration);
|
|
78
|
+
}
|
|
63
79
|
}
|
|
64
80
|
// 处理 decoration
|
|
65
|
-
|
|
81
|
+
arrangeDecorations(node);
|
|
66
82
|
// 让节点在最终盒尺寸确定后发布依赖 box 的端口
|
|
67
83
|
node.finalizeLayout?.(context);
|
|
68
84
|
}
|
|
@@ -78,10 +94,19 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
78
94
|
// 子域在主体准备时就排版完毕;它认领的附件保留块内几何,随块平移
|
|
79
95
|
const order = new Map(layoutAttachments.map((attachment, index) => [attachment, index]));
|
|
80
96
|
const relations = groupRelations(layoutAttachments);
|
|
97
|
+
let hosts = [];
|
|
98
|
+
if (layoutAttachments.length) {
|
|
99
|
+
for (const nodes of result.astToTemporal.values()) {
|
|
100
|
+
if (!nodes.some(node => node.placementOffset?.x || node.placementOffset?.y))
|
|
101
|
+
continue;
|
|
102
|
+
hosts = [...new Set([...result.astToTemporal.values()].flat().filter(isVisualTemporalNode))];
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
81
106
|
const subdomainItems = new Map();
|
|
82
|
-
const
|
|
107
|
+
const subdomainGeometry = (attachment, layoutOnly = false) => {
|
|
83
108
|
const item = subdomainItems.get(attachment);
|
|
84
|
-
return item && moveSubdomainItem(item)
|
|
109
|
+
return item && moveSubdomainItem(item, layoutOnly);
|
|
85
110
|
};
|
|
86
111
|
context = {
|
|
87
112
|
...context,
|
|
@@ -103,7 +128,7 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
103
128
|
own.push(relation);
|
|
104
129
|
}
|
|
105
130
|
own.sort((left, right) => order.get(left) - order.get(right));
|
|
106
|
-
const block = layoutSubdomainBlock(columns, line, root, own, context,
|
|
131
|
+
const block = layoutSubdomainBlock(columns, line, root, own, context, subdomainGeometry, hosts.length ? hosts.filter(within) : hosts, owner);
|
|
107
132
|
own.forEach((attachment, index) => subdomainItems.set(attachment, block.items[index]));
|
|
108
133
|
return { width: line.width, height: block.height, positions: block.positions };
|
|
109
134
|
},
|
|
@@ -136,8 +161,7 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
136
161
|
// 主体占用只依赖固有尺寸,整个纵向布局中保持不变
|
|
137
162
|
for (const node of objects) {
|
|
138
163
|
const line = lines[node.layoutLine];
|
|
139
|
-
|
|
140
|
-
includeTrackExtent(line.hostExtents, node.track, top, top + node.box.h);
|
|
164
|
+
includeHostExtent(line.hostExtents, node);
|
|
141
165
|
}
|
|
142
166
|
// 根据配置得到行距
|
|
143
167
|
const rowGaps = measureRowGaps(lines.length, objects, options.rowGap);
|
|
@@ -192,10 +216,10 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
192
216
|
};
|
|
193
217
|
// 3. 首次纵向放置后测量 attachment;只有有效轨道占用扩张时才重新求解
|
|
194
218
|
let placement = placeVertically();
|
|
195
|
-
let measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift,
|
|
219
|
+
let measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainGeometry, hosts);
|
|
196
220
|
if (measured.needsRelayout) {
|
|
197
221
|
placement = placeVertically();
|
|
198
|
-
measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift,
|
|
222
|
+
measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainGeometry, hosts);
|
|
199
223
|
}
|
|
200
224
|
const pages = placement.pages;
|
|
201
225
|
let next = 0;
|
|
@@ -219,7 +243,7 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
219
243
|
for (const pageResult of pages)
|
|
220
244
|
yield pageResult.bounds;
|
|
221
245
|
for (const node of objects)
|
|
222
|
-
yield node
|
|
246
|
+
yield getLayoutBounds(node);
|
|
223
247
|
for (const attachment of attachments)
|
|
224
248
|
yield attachment.box;
|
|
225
249
|
}
|
|
@@ -234,24 +258,50 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
234
258
|
};
|
|
235
259
|
}
|
|
236
260
|
/**
|
|
237
|
-
*
|
|
261
|
+
* 上下分别由内向外排列,纯留白只进入占用,不改变主体绘制坐标
|
|
238
262
|
*/
|
|
239
|
-
function
|
|
240
|
-
|
|
241
|
-
const below = node.decorations
|
|
242
|
-
.map(decoration => decoration.below)
|
|
243
|
-
.filter(item => item !== void 0)
|
|
244
|
-
.sort((left, right) => left.order - right.order);
|
|
245
|
-
if (below.length === 0)
|
|
263
|
+
function arrangeDecorations(node) {
|
|
264
|
+
if (node.decorations.length === 0)
|
|
246
265
|
return;
|
|
247
|
-
|
|
248
|
-
let
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
266
|
+
let contentTop = 0;
|
|
267
|
+
let contentBottom = -Infinity;
|
|
268
|
+
let top = 0;
|
|
269
|
+
let bottom = -Infinity;
|
|
270
|
+
for (const side of ["above", "below"]) {
|
|
271
|
+
const above = side === "above";
|
|
272
|
+
const decorations = node.decorations.length === 1 ? node.decorations : node.decorations.filter(item => item[side]);
|
|
273
|
+
if (decorations.length > 1)
|
|
274
|
+
decorations.sort((left, right) => left[side].order - right[side].order);
|
|
275
|
+
// 下方可覆盖起点,避免把字体盒的留白当成可见正文;两侧仍独立排列
|
|
276
|
+
let cursor = above ? 0 : (node.ports["decoration.below"]?.y ?? node.box.h);
|
|
277
|
+
let sideBottom = above ? -Infinity : node.box.h;
|
|
278
|
+
let sideContentBottom = sideBottom;
|
|
279
|
+
for (const decoration of decorations) {
|
|
280
|
+
const item = decoration[side];
|
|
281
|
+
if (!item)
|
|
282
|
+
continue;
|
|
283
|
+
const height = Math.max(0, item.height ?? 0);
|
|
284
|
+
// 纯留白包围完整内容,不能被正文盒内的装饰起点吞掉
|
|
285
|
+
if (!above && !decoration.paint)
|
|
286
|
+
cursor = Math.max(cursor, sideContentBottom);
|
|
287
|
+
const y = above ? cursor - (item.gap ?? 0) - height : cursor + (item.gap ?? 0);
|
|
288
|
+
item.place?.(y);
|
|
289
|
+
cursor = above ? y : y + height;
|
|
290
|
+
top = Math.min(top, y);
|
|
291
|
+
sideBottom = above ? Math.max(sideBottom, y + height) : cursor;
|
|
292
|
+
if (decoration.paint) {
|
|
293
|
+
contentTop = Math.min(contentTop, y);
|
|
294
|
+
sideContentBottom = above ? Math.max(sideContentBottom, y + height) : cursor;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
bottom = Math.max(bottom, sideBottom);
|
|
298
|
+
contentBottom = Math.max(contentBottom, sideContentBottom);
|
|
299
|
+
}
|
|
300
|
+
node.box.h = contentBottom;
|
|
301
|
+
const extraBottom = Math.max(0, bottom - contentBottom);
|
|
302
|
+
if (top !== 0 || extraBottom !== 0) {
|
|
303
|
+
decorationExtents.set(node, { top, bottom: extraBottom, contentTop });
|
|
253
304
|
}
|
|
254
|
-
node.box.h = y;
|
|
255
305
|
}
|
|
256
306
|
/**
|
|
257
307
|
* 按 lowering 已固化的 layoutLine 把可见事件拆成谱面行
|
|
@@ -379,13 +429,12 @@ function layoutLocalLine(columns, context) {
|
|
|
379
429
|
* 与正文行的流程相同:沿 root 的 Track 树求轴放置,按登记顺序测量附件,
|
|
380
430
|
* 附件扩张占用时重排一次。positions 是放置时写入的块内位置,不含成员 onPlaced 追加的平移
|
|
381
431
|
*/
|
|
382
|
-
function layoutSubdomainBlock(columns, { width, view }, root, attachments, context, measuredElsewhere) {
|
|
432
|
+
function layoutSubdomainBlock(columns, { width, view }, root, attachments, context, measuredElsewhere, hosts, owner) {
|
|
383
433
|
const members = columns.flat();
|
|
384
434
|
const extents = { hostExtents: new Map(), attachmentExtents: new Map() };
|
|
385
435
|
let gap = 0;
|
|
386
436
|
for (const member of members) {
|
|
387
|
-
|
|
388
|
-
includeTrackExtent(extents.hostExtents, member.track, top, top + member.box.h);
|
|
437
|
+
includeHostExtent(extents.hostExtents, member);
|
|
389
438
|
gap = Math.max(gap, member.ast.size * ROW_GAP_RATIO);
|
|
390
439
|
}
|
|
391
440
|
const positions = members.map(member => ({ x: member.box.x, y: 0 }));
|
|
@@ -415,14 +464,21 @@ function layoutSubdomainBlock(columns, { width, view }, root, attachments, conte
|
|
|
415
464
|
getVisualAxis: (_line, track) => solved.axes.get(lift(track)) ?? 0,
|
|
416
465
|
getHostExtent: (line, track) => lines[line]?.hostExtents.get(lift(track)),
|
|
417
466
|
getRangeColumns: (line, range, track) => rangeColumns(views[line], range, track, lift),
|
|
418
|
-
}, lines, lift, measuredElsewhere);
|
|
467
|
+
}, lines, lift, measuredElsewhere, hosts, owner);
|
|
419
468
|
let measured = measure();
|
|
420
469
|
if (measured.needsRelayout) {
|
|
421
470
|
solved = place();
|
|
422
471
|
measured = measure();
|
|
423
472
|
}
|
|
424
473
|
const reference = members[0];
|
|
425
|
-
const
|
|
474
|
+
const position = { x: reference.box.x, y: reference.box.y };
|
|
475
|
+
const offset = measured.layoutItems && placementOffset(reference, owner);
|
|
476
|
+
const layoutPosition = offset && (offset.x !== 0 || offset.y !== 0)
|
|
477
|
+
? { x: position.x - offset.x, y: position.y - offset.y } : position;
|
|
478
|
+
const items = measured.items.map((item, index) => ({
|
|
479
|
+
...item, reference, position,
|
|
480
|
+
...(measured.layoutItems && { layout: { ...measured.layoutItems[index], position: layoutPosition } }),
|
|
481
|
+
}));
|
|
426
482
|
return { height: solved.height, positions, items };
|
|
427
483
|
}
|
|
428
484
|
/**
|
|
@@ -455,17 +511,19 @@ function encloses(owner, node) {
|
|
|
455
511
|
return false;
|
|
456
512
|
}
|
|
457
513
|
/** 子域是刚性块:按参照成员的位移平移块内测得的几何 */
|
|
458
|
-
function moveSubdomainItem(item) {
|
|
459
|
-
const
|
|
460
|
-
const
|
|
514
|
+
function moveSubdomainItem(item, layoutOnly = false) {
|
|
515
|
+
const measured = layoutOnly ? item.layout ?? item : item;
|
|
516
|
+
const { position, geometry, box } = measured;
|
|
517
|
+
const dx = item.reference.box.x - position.x;
|
|
518
|
+
const dy = item.reference.box.y - position.y;
|
|
461
519
|
if (dx === 0 && dy === 0)
|
|
462
|
-
return
|
|
520
|
+
return measured;
|
|
463
521
|
const move = (rect) => ({ ...rect, x: rect.x + dx, y: rect.y + dy });
|
|
464
|
-
const { geometry } = item;
|
|
465
522
|
return {
|
|
466
|
-
box: move(
|
|
523
|
+
box: move(box),
|
|
467
524
|
geometry: {
|
|
468
525
|
regions: geometry.regions.map(move),
|
|
526
|
+
occupancy: geometry.occupancy?.map(move),
|
|
469
527
|
paint: painter => geometry.paint(new TranslatingPainter(painter, dx, dy)),
|
|
470
528
|
},
|
|
471
529
|
};
|
|
@@ -487,20 +545,72 @@ function measureRowGaps(lineCount, objects, override) {
|
|
|
487
545
|
gaps[i] ||= documentGap;
|
|
488
546
|
return gaps;
|
|
489
547
|
}
|
|
490
|
-
/**
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
548
|
+
/** 折叠成员继承外层平移,子域只累加当前复合体内部的平移 */
|
|
549
|
+
function placementOffset(node, owner) {
|
|
550
|
+
let x = 0;
|
|
551
|
+
let y = 0;
|
|
552
|
+
for (let host = node; host && host !== owner; host = host.foldedInto) {
|
|
553
|
+
x += host.placementOffset?.x ?? 0;
|
|
554
|
+
y += host.placementOffset?.y ?? 0;
|
|
555
|
+
}
|
|
556
|
+
return { x, y };
|
|
557
|
+
}
|
|
558
|
+
/** 后置平移只改变绘制几何,占用始终在未平移的布局位置测量 */
|
|
559
|
+
function measureAttachments(attachments, baseContext, lines, lift, measuredElsewhere, hosts, owner) {
|
|
560
|
+
if (attachments.length === 0)
|
|
561
|
+
return { items: [], needsRelayout: false };
|
|
562
|
+
const shifts = [];
|
|
563
|
+
for (const node of hosts) {
|
|
564
|
+
const { x, y } = placementOffset(node, owner);
|
|
565
|
+
if (x !== 0 || y !== 0)
|
|
566
|
+
shifts.push({ node, x, y, boxX: node.box.x, boxY: node.box.y });
|
|
567
|
+
}
|
|
568
|
+
const displaced = shifts.length > 0 || attachments.some(attachment => attachment.placementOffset?.x || attachment.placementOffset?.y);
|
|
569
|
+
let layout;
|
|
570
|
+
if (displaced) {
|
|
571
|
+
for (const { node, x, y } of shifts) {
|
|
572
|
+
node.box.x -= x;
|
|
573
|
+
node.box.y -= y;
|
|
574
|
+
}
|
|
575
|
+
try {
|
|
576
|
+
layout = measureAttachmentPass(attachments, { ...baseContext, layoutOnly: true }, lines, lift, measuredElsewhere, true);
|
|
577
|
+
}
|
|
578
|
+
finally {
|
|
579
|
+
for (const { node, boxX, boxY } of shifts) {
|
|
580
|
+
node.box.x = boxX;
|
|
581
|
+
node.box.y = boxY;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
else
|
|
586
|
+
layout = measureAttachmentPass(attachments, baseContext, lines, lift, measuredElsewhere, true);
|
|
587
|
+
if (!displaced)
|
|
588
|
+
return layout;
|
|
589
|
+
// 试测结果若被重排丢弃,就不再为它生成绘制几何
|
|
590
|
+
let painted;
|
|
591
|
+
return {
|
|
592
|
+
needsRelayout: layout.needsRelayout,
|
|
593
|
+
layoutItems: layout.items,
|
|
594
|
+
get items() {
|
|
595
|
+
return painted ??= measureAttachmentPass(attachments, baseContext, lines, lift, measuredElsewhere, false).items;
|
|
596
|
+
},
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
/** 按声明顺序测量几何,后注册的附件可以查询此前的区域和占用 */
|
|
600
|
+
function measureAttachmentPass(attachments, baseContext, lines, lift, measuredElsewhere, reserve) {
|
|
500
601
|
const measured = new Map();
|
|
501
|
-
const own = new Set(attachments);
|
|
502
602
|
const occupancy = [];
|
|
503
603
|
let needsRelayout = false;
|
|
604
|
+
// 同一测量轮坐标不变,子域几何只需平移一次
|
|
605
|
+
function resolveAttachment(dependency) {
|
|
606
|
+
let item = measured.get(dependency);
|
|
607
|
+
if (!item) {
|
|
608
|
+
item = measuredElsewhere(dependency, baseContext.layoutOnly);
|
|
609
|
+
if (item)
|
|
610
|
+
measured.set(dependency, item);
|
|
611
|
+
}
|
|
612
|
+
return item;
|
|
613
|
+
}
|
|
504
614
|
function getRangeExtents(line, columns, track) {
|
|
505
615
|
const view = baseContext.lines[line];
|
|
506
616
|
const extents = track === undefined ? new Map() : undefined;
|
|
@@ -530,8 +640,10 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
|
|
|
530
640
|
if (host.box.x + host.box.w > right)
|
|
531
641
|
right = host.box.x + host.box.w;
|
|
532
642
|
}
|
|
533
|
-
const
|
|
534
|
-
|
|
643
|
+
const decoration = decorationExtents.get(host);
|
|
644
|
+
const top = decoration?.top ?? 0;
|
|
645
|
+
const hostTop = host.box.y + top - baseContext.getVisualAxis(line, host.track);
|
|
646
|
+
include(host.track, hostTop, hostTop + (host.box.h - top + (decoration?.bottom ?? 0)));
|
|
535
647
|
}
|
|
536
648
|
}
|
|
537
649
|
for (const range of occupancy[line] ?? []) {
|
|
@@ -546,25 +658,30 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
|
|
|
546
658
|
const context = {
|
|
547
659
|
...baseContext,
|
|
548
660
|
getRangeExtents,
|
|
661
|
+
getAttachmentOccupancy(dependency) {
|
|
662
|
+
const geometry = resolveAttachment(dependency)?.geometry;
|
|
663
|
+
return geometry && (geometry.occupancy ?? geometry.regions);
|
|
664
|
+
},
|
|
549
665
|
getAttachmentBox(dependency) {
|
|
550
|
-
const resolved =
|
|
666
|
+
const resolved = resolveAttachment(dependency);
|
|
551
667
|
if (!resolved)
|
|
552
668
|
throw new Error("Layout attachment dependency has not been measured");
|
|
553
|
-
return resolved;
|
|
669
|
+
return resolved.box;
|
|
554
670
|
},
|
|
555
671
|
getContentBounds(content) {
|
|
556
672
|
const boxes = [];
|
|
557
673
|
for (const node of content.nodes) {
|
|
558
674
|
if (isVisualTemporalNode(node) && (node.box.w > 0 || node.box.h > 0))
|
|
559
|
-
boxes.push(node
|
|
675
|
+
boxes.push(getLayoutBounds(node, true));
|
|
560
676
|
}
|
|
561
677
|
for (const attachment of content.attachments) {
|
|
562
678
|
if (!isLayoutAttachment(attachment))
|
|
563
679
|
continue;
|
|
564
680
|
// 端点越出本域的关系交给外层测量,此时还没有几何
|
|
565
|
-
|
|
681
|
+
const resolved = resolveAttachment(attachment);
|
|
682
|
+
if (!resolved && !attachments.includes(attachment))
|
|
566
683
|
continue;
|
|
567
|
-
const box = context.getAttachmentBox(attachment);
|
|
684
|
+
const box = resolved?.box ?? context.getAttachmentBox(attachment);
|
|
568
685
|
if (box.w > 0 || box.h > 0)
|
|
569
686
|
boxes.push(box);
|
|
570
687
|
}
|
|
@@ -577,7 +694,7 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
|
|
|
577
694
|
const box = { x: 0, y: 0, w: 0, h: 0 };
|
|
578
695
|
unionLayoutBoxes(box, geometry.regions);
|
|
579
696
|
const item = { geometry, box };
|
|
580
|
-
measured.set(attachment,
|
|
697
|
+
measured.set(attachment, item);
|
|
581
698
|
// 当前项立即登记,保证后注册的 attachment 能看到并避让它
|
|
582
699
|
for (const region of geometry.occupancy ?? geometry.regions) {
|
|
583
700
|
if (region.line === void 0)
|
|
@@ -588,11 +705,12 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
|
|
|
588
705
|
const bottom = top + region.h;
|
|
589
706
|
const hostExtent = line.hostExtents.get(track);
|
|
590
707
|
const attachmentExtent = line.attachmentExtents.get(track);
|
|
591
|
-
if (top < Math.min(hostExtent?.top ?? Infinity, attachmentExtent?.top ?? Infinity)
|
|
592
|
-
|| bottom > Math.max(hostExtent?.bottom ?? -Infinity, attachmentExtent?.bottom ?? -Infinity)) {
|
|
708
|
+
if (reserve && (top < Math.min(hostExtent?.top ?? Infinity, attachmentExtent?.top ?? Infinity)
|
|
709
|
+
|| bottom > Math.max(hostExtent?.bottom ?? -Infinity, attachmentExtent?.bottom ?? -Infinity))) {
|
|
593
710
|
needsRelayout = true;
|
|
594
711
|
}
|
|
595
|
-
|
|
712
|
+
if (reserve)
|
|
713
|
+
includeTrackExtent(line.attachmentExtents, track, top, bottom);
|
|
596
714
|
(occupancy[region.line] ??= []).push({
|
|
597
715
|
track,
|
|
598
716
|
left: region.x,
|
|
@@ -605,6 +723,14 @@ function measureAttachments(attachments, baseContext, lines, lift, measuredElsew
|
|
|
605
723
|
});
|
|
606
724
|
return { items, needsRelayout };
|
|
607
725
|
}
|
|
726
|
+
/** 固有占用只需轴局部边界,不必创建全局矩形 */
|
|
727
|
+
function includeHostExtent(extents, host) {
|
|
728
|
+
const decoration = decorationExtents.get(host);
|
|
729
|
+
const top = decoration?.top ?? 0;
|
|
730
|
+
const height = host.box.h - top + (decoration?.bottom ?? 0);
|
|
731
|
+
const axisTop = top - host.box.visualAxis;
|
|
732
|
+
includeTrackExtent(extents, host.track, axisTop, axisTop + height);
|
|
733
|
+
}
|
|
608
734
|
/**
|
|
609
735
|
* 沿 Track 树自内向外求出一条谱面行里所有音轨的纵向轴
|
|
610
736
|
*
|
package/dist/layout/model.js
CHANGED
|
@@ -88,7 +88,6 @@ function fillPlaceholders(columns, F = DEFAULT_F) {
|
|
|
88
88
|
if (!sortedCol[rIdx]) {
|
|
89
89
|
// 保证 margin_L = mexLeft, margin_R = maxRight, 实际宽度为0
|
|
90
90
|
const elPlaceholder = layoutElement({
|
|
91
|
-
anchor: 0,
|
|
92
91
|
alpha_L: maxLeft,
|
|
93
92
|
alpha_R: maxRight,
|
|
94
93
|
}, {
|
|
@@ -164,7 +163,6 @@ function normalizeSolverOptions(options) {
|
|
|
164
163
|
function solveHorizontal(mat, rows, X, limit, options) {
|
|
165
164
|
const { damping, maxIter, eps, crossPunish } = options;
|
|
166
165
|
const numCols = mat.length;
|
|
167
|
-
// if (numCols === 0 || rows === 0) return false; layoutHorizontalRegion 已经判断过了
|
|
168
166
|
// 预排列(仅用前 rows 个元素建立无约束时的位置)
|
|
169
167
|
const row_x = new Float64Array(rows);
|
|
170
168
|
for (let c = 0; c < numCols; c++) {
|
|
@@ -187,19 +185,18 @@ function solveHorizontal(mat, rows, X, limit, options) {
|
|
|
187
185
|
x0 = limit / x0;
|
|
188
186
|
for (let c = 0; c < numCols; c++)
|
|
189
187
|
X[c] *= x0;
|
|
190
|
-
// --- 预分配物理引擎所需的全部静态缓存空间,杜绝主循环内的 GC 损耗 ---
|
|
191
188
|
const F_vec = new Float64Array(numCols);
|
|
192
|
-
|
|
193
|
-
|
|
189
|
+
// 各声部共享列坐标,刚度按左墙、相邻列、右墙汇总。
|
|
190
|
+
const stiffness = new Float64Array(numCols + 1);
|
|
194
191
|
function computeForces(cols_X) {
|
|
195
192
|
F_vec.fill(0);
|
|
193
|
+
stiffness.fill(0);
|
|
196
194
|
for (let r = 0; r < rows; r++) {
|
|
197
|
-
const rowOffset = r * (numCols + 1);
|
|
198
195
|
// 左墙
|
|
199
196
|
const el_first = mat[0][r];
|
|
200
197
|
const res_left = calcLeftWall(el_first, cols_X[0], crossPunish);
|
|
201
198
|
F_vec[0] += res_left.force;
|
|
202
|
-
|
|
199
|
+
stiffness[0] += res_left.stiffness;
|
|
203
200
|
// 内部链弹簧力传导
|
|
204
201
|
for (let c = 1; c < numCols; c++) {
|
|
205
202
|
const el_l = mat[c - 1][r];
|
|
@@ -207,74 +204,33 @@ function solveHorizontal(mat, rows, X, limit, options) {
|
|
|
207
204
|
const res = calcPairForceAndStiffness(el_l, el_r, cols_X[c - 1], cols_X[c]);
|
|
208
205
|
F_vec[c - 1] -= res.force;
|
|
209
206
|
F_vec[c] += res.force;
|
|
210
|
-
|
|
207
|
+
stiffness[c] += res.stiffness;
|
|
211
208
|
}
|
|
212
209
|
// 右墙
|
|
213
210
|
const el_last = mat[numCols - 1][r];
|
|
214
211
|
const res_right = calcRightWall(el_last, cols_X[numCols - 1], limit, crossPunish);
|
|
215
212
|
F_vec[numCols - 1] -= res_right.force;
|
|
216
|
-
|
|
213
|
+
stiffness[numCols] += res_right.stiffness;
|
|
217
214
|
}
|
|
218
215
|
return F_vec;
|
|
219
216
|
}
|
|
220
|
-
const
|
|
221
|
-
// Hessian 矩阵切向刚度乘以位移向量 p (精简参数,移除了无用的 cols_X)
|
|
222
|
-
function multiplyH(p) {
|
|
223
|
-
Hp.fill(0);
|
|
224
|
-
for (let r = 0, rowOffset = 0; r < rows; r++, rowOffset += (numCols + 1)) {
|
|
225
|
-
// 左墙刚度
|
|
226
|
-
Hp[0] += stiffnessCache[rowOffset] * p[0];
|
|
227
|
-
// 链弹簧二阶切线刚度
|
|
228
|
-
for (let c = 1; c < numCols; c++) {
|
|
229
|
-
const Kdp = stiffnessCache[rowOffset + c] * (p[c] - p[c - 1]);
|
|
230
|
-
Hp[c - 1] -= Kdp;
|
|
231
|
-
Hp[c] += Kdp;
|
|
232
|
-
}
|
|
233
|
-
// 右墙刚度
|
|
234
|
-
Hp[numCols - 1] += stiffnessCache[rowOffset + numCols] * p[numCols - 1];
|
|
235
|
-
}
|
|
236
|
-
// L-M 正则阻尼,保证矩阵严格正定,避免奇异性
|
|
237
|
-
const lambda = 1e-4;
|
|
238
|
-
for (let i = 0; i < p.length; i++)
|
|
239
|
-
Hp[i] += lambda * p[i];
|
|
240
|
-
return Hp;
|
|
241
|
-
}
|
|
242
|
-
function dot(a, b) {
|
|
243
|
-
let sum = 0;
|
|
244
|
-
for (let i = 0; i < a.length; i++)
|
|
245
|
-
sum += a[i] * b[i];
|
|
246
|
-
return sum;
|
|
247
|
-
}
|
|
248
|
-
// 必须用 Float64Array 不然影响收敛
|
|
217
|
+
const diagonal = new Float64Array(numCols);
|
|
249
218
|
const dx = new Float64Array(numCols);
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
if (rsold < 1e-12)
|
|
259
|
-
return dx;
|
|
260
|
-
for (let iter = 0; iter < maxCGIter; iter++) {
|
|
261
|
-
const Hp = multiplyH(P);
|
|
262
|
-
const p_Hp = dot(P, Hp);
|
|
263
|
-
if (Math.abs(p_Hp) < 1e-12)
|
|
264
|
-
break;
|
|
265
|
-
const alpha = rsold / p_Hp;
|
|
266
|
-
for (let i = 0; i < numCols; i++) {
|
|
267
|
-
dx[i] += alpha * P[i];
|
|
268
|
-
R[i] -= alpha * Hp[i];
|
|
269
|
-
}
|
|
270
|
-
const rsnew = dot(R, R);
|
|
271
|
-
if (Math.sqrt(rsnew) < 1e-6)
|
|
272
|
-
break;
|
|
273
|
-
const beta = rsnew / rsold;
|
|
274
|
-
for (let i = 0; i < numCols; i++) {
|
|
275
|
-
P[i] = R[i] + beta * P[i];
|
|
219
|
+
function solveTridiagonal() {
|
|
220
|
+
// H[i,i] = K[i] + K[i+1] + lambda,H[i-1,i] = -K[i]。
|
|
221
|
+
const lambda = 1e-4;
|
|
222
|
+
for (let i = 0; i < numCols; i++) {
|
|
223
|
+
const factor = i === 0 ? 0 : stiffness[i] / diagonal[i - 1];
|
|
224
|
+
const pivot = stiffness[i] + stiffness[i + 1] + lambda - factor * stiffness[i];
|
|
225
|
+
if (!(pivot > 0) || !Number.isFinite(pivot)) {
|
|
226
|
+
throw new Error("Horizontal spring stiffness must produce a finite positive definite system");
|
|
276
227
|
}
|
|
277
|
-
|
|
228
|
+
diagonal[i] = pivot;
|
|
229
|
+
dx[i] = F_vec[i] + (i === 0 ? 0 : factor * dx[i - 1]);
|
|
230
|
+
}
|
|
231
|
+
dx[numCols - 1] /= diagonal[numCols - 1];
|
|
232
|
+
for (let i = numCols - 2; i >= 0; i--) {
|
|
233
|
+
dx[i] = (dx[i] + stiffness[i + 1] * dx[i + 1]) / diagonal[i];
|
|
278
234
|
}
|
|
279
235
|
return dx;
|
|
280
236
|
}
|
|
@@ -290,7 +246,7 @@ function solveHorizontal(mat, rows, X, limit, options) {
|
|
|
290
246
|
}
|
|
291
247
|
if (f_max < eps)
|
|
292
248
|
break;
|
|
293
|
-
const dx =
|
|
249
|
+
const dx = solveTridiagonal();
|
|
294
250
|
for (let i = 0; i < numCols; i++) {
|
|
295
251
|
X[i] += damping * dx[i];
|
|
296
252
|
}
|
package/dist/layout/page.js
CHANGED
|
@@ -5,10 +5,10 @@ export class PageLayoutError extends Error {
|
|
|
5
5
|
availableHeight;
|
|
6
6
|
constructor(line, requiredHeight, availableHeight) {
|
|
7
7
|
super(`Page cannot fit layout line ${line}: requires ${requiredHeight}px, available ${availableHeight}px`);
|
|
8
|
-
this.name = "PageLayoutError";
|
|
9
8
|
this.line = line;
|
|
10
9
|
this.requiredHeight = requiredHeight;
|
|
11
10
|
this.availableHeight = availableHeight;
|
|
11
|
+
this.name = "PageLayoutError";
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
/** 没有 page 函数时使用的默认 A4 近似页面配置 */
|