jpfun 1.0.0 → 1.2.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/README.md +1 -1
- package/dist/diagnostic.d.ts +1 -0
- package/dist/diagnostic.js +1 -0
- package/dist/functions/ASTtypes.d.ts +2 -3
- package/dist/functions/ASTtypes.js +2 -3
- package/dist/functions/adjust/index.js +15 -67
- package/dist/functions/bar/index.js +11 -7
- package/dist/functions/beam/auto.js +15 -11
- package/dist/functions/beam/layout.js +22 -48
- package/dist/functions/br/index.d.ts +9 -3
- package/dist/functions/br/index.js +20 -16
- package/dist/functions/dash/index.js +13 -11
- package/dist/functions/default.js +2 -1
- package/dist/functions/div/index.d.ts +2 -2
- package/dist/functions/div/index.js +5 -9
- package/dist/functions/dot/index.js +5 -9
- package/dist/functions/dyn/index.js +125 -53
- package/dist/functions/gliss/index.js +81 -7
- package/dist/functions/grace/index.d.ts +3 -10
- package/dist/functions/grace/index.js +25 -57
- package/dist/functions/head/index.js +6 -7
- package/dist/functions/key/index.js +1 -0
- package/dist/functions/meter/index.js +7 -5
- package/dist/functions/note/index.d.ts +5 -2
- package/dist/functions/note/index.js +162 -19
- package/dist/functions/note/source-transform.d.ts +8 -2
- package/dist/functions/note/source-transform.js +25 -11
- package/dist/functions/ottava/index.js +8 -7
- package/dist/functions/page/index.d.ts +1 -36
- package/dist/functions/page/index.js +11 -15
- package/dist/functions/pagebreak/index.js +2 -1
- package/dist/functions/rest/index.js +5 -5
- package/dist/functions/set/index.js +2 -2
- package/dist/functions/stack/index.js +7 -7
- package/dist/functions/symbol/symbols/dynamics.d.ts +3 -0
- package/dist/functions/symbol/symbols/dynamics.js +10 -1
- package/dist/functions/tempo/index.js +8 -3
- package/dist/functions/temporal.d.ts +21 -6
- package/dist/functions/temporal.js +1 -1
- package/dist/functions/text/index.js +3 -5
- package/dist/functions/up/index.js +37 -34
- package/dist/functions/voice/connections.js +2 -2
- package/dist/functions/voice/lyrics.d.ts +2 -0
- package/dist/functions/voice/lyrics.js +4 -4
- package/dist/functions/voice/voice.d.ts +2 -1
- package/dist/functions/voice/voice.js +9 -15
- package/dist/functions/voice/voices.d.ts +6 -4
- package/dist/functions/voice/voices.js +10 -10
- package/dist/index.d.ts +6 -34
- package/dist/index.js +6 -34
- package/dist/jpfun.min.js +49 -46
- package/dist/layout/engine.js +237 -232
- package/dist/layout/model.d.ts +1 -1
- package/dist/layout/page.d.ts +2 -2
- package/dist/layout/page.js +8 -22
- package/dist/layout/types.d.ts +4 -5
- package/dist/lowering/accidentalMemory.d.ts +33 -0
- package/dist/lowering/accidentalMemory.js +84 -0
- package/dist/lowering/loweringContext.d.ts +12 -7
- package/dist/lowering/loweringContext.js +54 -51
- package/dist/lowering/types.d.ts +2 -3
- package/dist/parser/grammarType.d.ts +10 -4
- package/dist/parser/parse-utils/brace-utils.js +4 -3
- package/dist/parser/parse-utils/call-utils.js +7 -7
- package/dist/parser/parse-utils/string-utils.d.ts +1 -1
- package/dist/parser/parse-utils/string-utils.js +1 -1
- package/dist/parser/parserContext.d.ts +30 -34
- package/dist/parser/parserContext.js +77 -181
- package/dist/parser/preprocess.js +10 -23
- package/dist/parser/syntax.d.ts +9 -0
- package/dist/parser/syntax.js +109 -0
- package/dist/parser/types.d.ts +2 -1
- package/dist/pipeline.js +3 -2
- package/dist/playback/compile.js +26 -18
- package/dist/playback/event.d.ts +1 -2
- package/dist/playback/event.js +2 -3
- package/dist/playback/types.d.ts +0 -1
- package/dist/render/index.d.ts +1 -0
- package/dist/render/index.js +1 -0
- package/grammar.md +8 -6
- package/package.json +2 -2
- package/dist/functions/voice/index.d.ts +0 -2
- package/dist/functions/voice/index.js +0 -2
package/dist/layout/engine.js
CHANGED
|
@@ -92,15 +92,9 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
92
92
|
// 子域在主体准备时就排版完毕;它认领的附件保留块内几何,随块平移
|
|
93
93
|
const order = new Map(layoutAttachments.map((attachment, index) => [attachment, index]));
|
|
94
94
|
const relations = groupRelations(layoutAttachments);
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
if (!nodes.some(node => node.placementOffset?.x || node.placementOffset?.y))
|
|
99
|
-
continue;
|
|
100
|
-
hosts = [...new Set([...result.astToTemporal.values()].flat().filter(isVisualTemporalNode))];
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
95
|
+
const displaced = collectDisplaced(result);
|
|
96
|
+
// 块内(含内层块)有平移的复合体:外层可能依赖它的附件,也要分开测量布局与绘制
|
|
97
|
+
const displacedBlocks = [];
|
|
104
98
|
const subdomainItems = new Map();
|
|
105
99
|
const subdomainGeometry = (attachment, layoutOnly = false) => {
|
|
106
100
|
const item = subdomainItems.get(attachment);
|
|
@@ -108,6 +102,7 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
108
102
|
};
|
|
109
103
|
context = {
|
|
110
104
|
...context,
|
|
105
|
+
solverOptions: options,
|
|
111
106
|
layoutSubdomain(owner, columns, root, attachments) {
|
|
112
107
|
// 横排时内层子域先排版并认领关系,这里只取剩下的
|
|
113
108
|
const line = layoutLocalLine(columns, context);
|
|
@@ -126,7 +121,11 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
126
121
|
own.push(relation);
|
|
127
122
|
}
|
|
128
123
|
own.sort((left, right) => order.get(left) - order.get(right));
|
|
129
|
-
const
|
|
124
|
+
const nodes = displaced.filter(within);
|
|
125
|
+
const layoutOnly = nodes.length > 0 || own.some(isDisplaced) || displacedBlocks.some(within);
|
|
126
|
+
if (layoutOnly)
|
|
127
|
+
displacedBlocks.push(owner);
|
|
128
|
+
const block = layoutSubdomainBlock(columns, line, root, own, context, subdomainGeometry, { nodes, layoutOnly });
|
|
130
129
|
own.forEach((attachment, index) => subdomainItems.set(attachment, block.items[index]));
|
|
131
130
|
return { width: line.width, height: block.height, positions: block.positions };
|
|
132
131
|
},
|
|
@@ -175,26 +174,29 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
175
174
|
}
|
|
176
175
|
// 根据配置得到行距
|
|
177
176
|
const rowGaps = measureRowGaps(lines.length, objects, options.rowGap);
|
|
178
|
-
|
|
177
|
+
const domain = { lines, views, lift };
|
|
178
|
+
let pages = [];
|
|
179
|
+
/** 按当前主体与 attachment 占用求轴、分页并放置所有主体,返回本轮的附件上下文 */
|
|
179
180
|
const placeVertically = () => {
|
|
180
181
|
const heights = [];
|
|
181
|
-
const
|
|
182
|
+
const gaps = [];
|
|
182
183
|
const axes = lines.map((line, i) => {
|
|
183
184
|
const solved = solveVerticalAxes(line, result.rootTrack, rowGaps[i]);
|
|
184
185
|
heights.push(solved.height);
|
|
186
|
+
// 行顶高出主旋律上沿(基线 - 半字高)的外扩先借用行距,再保留 minGap
|
|
185
187
|
let top = Infinity;
|
|
186
188
|
for (const column of line.columns) {
|
|
187
189
|
for (const node of column) {
|
|
188
190
|
top = Math.min(top, (solved.axes.get(node.track) ?? 0) - node.ast.size / 2);
|
|
189
191
|
}
|
|
190
192
|
}
|
|
191
|
-
|
|
193
|
+
const { gap = 0, minGap = 0 } = line.breakBefore ?? {};
|
|
194
|
+
gaps.push(Math.max(gap - (top < Infinity ? Math.max(0, top) : 0), Math.min(gap, minGap)));
|
|
192
195
|
return solved.axes;
|
|
193
196
|
});
|
|
194
|
-
let pages;
|
|
195
197
|
let lineTops;
|
|
196
198
|
try {
|
|
197
|
-
({ pages, lineTops } = paginateLayoutLines(heights, page, pageBreaks,
|
|
199
|
+
({ pages, lineTops } = paginateLayoutLines(heights, page, pageBreaks, gaps));
|
|
198
200
|
}
|
|
199
201
|
catch (error) {
|
|
200
202
|
if (!(error instanceof PageLayoutError))
|
|
@@ -219,31 +221,15 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
219
221
|
node.box.y = visualAxisOf(node.layoutLine, node.track) - node.box.visualAxis;
|
|
220
222
|
node.onPlaced?.();
|
|
221
223
|
}
|
|
222
|
-
|
|
223
|
-
...context,
|
|
224
|
-
width: contentWidth,
|
|
225
|
-
originX,
|
|
226
|
-
pages: pages.map(item => item.bounds),
|
|
227
|
-
lines: views,
|
|
228
|
-
// 子域的私有轨在正文里归入复合体所在的轨
|
|
229
|
-
getVisualAxis: (line, track) => visualAxisOf(line, lift(track)),
|
|
230
|
-
getHostExtent: (line, track) => lines[line]?.hostExtents.get(lift(track)),
|
|
231
|
-
getRangeColumns: (line, range, track) => rangeColumns(views[line], range, track, lift),
|
|
232
|
-
};
|
|
233
|
-
return { pages, attachmentContext };
|
|
224
|
+
return attachmentContext(context, domain, { width: contentWidth, originX, pages: pages.map(item => item.bounds) }, visualAxisOf);
|
|
234
225
|
};
|
|
235
|
-
// 3.
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
placement = placeVertically();
|
|
240
|
-
measured = measureAttachmentPass(documentAttachments, placement.attachmentContext, lines, lift, subdomainGeometry, false);
|
|
241
|
-
}
|
|
242
|
-
const pages = placement.pages;
|
|
226
|
+
// 3. 放置并测量正文 attachment,扩张有效轨道占用时重排一次,最后施加后置平移
|
|
227
|
+
// 正文的作用域是全文:子域认领的附件有平移时,正文依赖它也只能读布局快照
|
|
228
|
+
const displacement = { nodes: displaced, layoutOnly: displaced.length > 0 || layoutAttachments.some(isDisplaced) };
|
|
229
|
+
const { items } = placeAndMeasure(domain, placeVertically, documentAttachments, subdomainGeometry, displacement);
|
|
243
230
|
let next = 0;
|
|
244
231
|
const attachments = layoutAttachments.map(attachment => {
|
|
245
|
-
const
|
|
246
|
-
const { geometry } = item ? moveSubdomainItem(item) : measured.items[next++];
|
|
232
|
+
const { geometry } = subdomainGeometry(attachment) ?? items[next++];
|
|
247
233
|
return {
|
|
248
234
|
kind: attachment.kind,
|
|
249
235
|
regions: geometry.regions,
|
|
@@ -338,7 +324,7 @@ function splitLayoutLines(result) {
|
|
|
338
324
|
for (const column of result.columns) {
|
|
339
325
|
const visibleColumn = []; // 从列中提取可见元素
|
|
340
326
|
// 空行
|
|
341
|
-
while (lines.length < column
|
|
327
|
+
while (lines.length < column.layoutLine) {
|
|
342
328
|
lines.push(currentLine);
|
|
343
329
|
currentLine = createLine();
|
|
344
330
|
}
|
|
@@ -347,6 +333,9 @@ function splitLayoutLines(result) {
|
|
|
347
333
|
if (pageBreak !== undefined) {
|
|
348
334
|
pageBreaks[lines.length] = (pageBreaks[lines.length] ?? true) && pageBreak;
|
|
349
335
|
}
|
|
336
|
+
const brk = node.addon?.breakBefore, prev = currentLine.breakBefore;
|
|
337
|
+
if (brk)
|
|
338
|
+
currentLine.breakBefore = prev ? { gap: Math.max(prev.gap, brk.gap), minGap: Math.max(prev.minGap, brk.minGap) } : brk;
|
|
350
339
|
if (isVisualTemporalNode(node))
|
|
351
340
|
visibleColumn.push(node);
|
|
352
341
|
}
|
|
@@ -471,6 +460,67 @@ function alignHorizontalBars(views, regions, contentWidth, pageBreaks) {
|
|
|
471
460
|
});
|
|
472
461
|
const divides = (a, b) => a % b === 0 || b % a === 0;
|
|
473
462
|
const gcd = (a, b) => b ? gcd(b, a % b) : a;
|
|
463
|
+
// 在各行小节数最小公倍数 L 的细网格上求公共台阶(整段时 L 即最大小节数,回退子窗口时也保证整除),并通过水位填充(涨水填谷)补足剩余空白
|
|
464
|
+
const buildGrid = (lo, hi) => {
|
|
465
|
+
const group = states.slice(lo, hi + 1);
|
|
466
|
+
const L = group.reduce((acc, n) => acc / gcd(acc, n.minW.length) * n.minW.length, 1);
|
|
467
|
+
const grid = new Array(L).fill(0);
|
|
468
|
+
// 先填细网格行,再把粗网格行(如 2 小节行)超出对应细格之和的差额平摊进去
|
|
469
|
+
for (const n of [...group].sort((a, b) => b.minW.length - a.minW.length)) {
|
|
470
|
+
const step = L / n.minW.length;
|
|
471
|
+
for (let m = 0; m < n.minW.length; m++) {
|
|
472
|
+
const w = n.minW[m];
|
|
473
|
+
let sum = 0;
|
|
474
|
+
for (let j = 0; j < step; j++)
|
|
475
|
+
sum += grid[m * step + j];
|
|
476
|
+
if (w > sum)
|
|
477
|
+
for (let j = 0; j < step; j++)
|
|
478
|
+
grid[m * step + j] += (w - sum) / step;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
// 公共台阶总和不超过行宽时,把水位拉到 avg,只填平低于水位的窄格,不拉大已高于水位的宽格
|
|
482
|
+
// open 记录段内所有行在该细格均有自由间隙,供第 3 步复用
|
|
483
|
+
const open = grid.map((_, g) => Number(group.every(n => n.gaps[Math.floor(g * n.minW.length / L)] > 0)));
|
|
484
|
+
const free = open.slice();
|
|
485
|
+
let rem = contentWidth;
|
|
486
|
+
for (let g = 0; g < L; g++)
|
|
487
|
+
if (!free[g])
|
|
488
|
+
rem -= grid[g];
|
|
489
|
+
for (let step = 0; step < L; step++) {
|
|
490
|
+
const freeSum = free.reduce((a, b) => a + b, 0);
|
|
491
|
+
if (!freeSum || rem <= 0)
|
|
492
|
+
break;
|
|
493
|
+
const avg = rem / freeSum;
|
|
494
|
+
if (!grid.some((w, g) => free[g] && w > avg && (rem -= w, free[g] = 0, true))) {
|
|
495
|
+
for (let g = 0; g < L; g++)
|
|
496
|
+
if (free[g])
|
|
497
|
+
grid[g] = avg;
|
|
498
|
+
break;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return { L, grid, open };
|
|
502
|
+
};
|
|
503
|
+
// 公共台阶只取决于窗口,按窗口缓存,避免段内每行重建网格导致 O(行数²);每行只把每 r 个细格合回自己的 1 个小节目标宽度
|
|
504
|
+
const grids = new Map();
|
|
505
|
+
const targetOf = (s, lo, hi) => {
|
|
506
|
+
const key = lo * states.length + hi;
|
|
507
|
+
let cell = grids.get(key);
|
|
508
|
+
if (!cell)
|
|
509
|
+
grids.set(key, cell = buildGrid(lo, hi));
|
|
510
|
+
const { L, grid, open } = cell, r = L / s.minW.length;
|
|
511
|
+
// 本行被 @rest 等完全锁死的小节保持原宽
|
|
512
|
+
const target = s.minW.map((w, m) => {
|
|
513
|
+
if (!s.gaps[m])
|
|
514
|
+
return w;
|
|
515
|
+
let sum = 0;
|
|
516
|
+
for (let j = 0; j < r; j++)
|
|
517
|
+
sum += grid[m * r + j];
|
|
518
|
+
return sum;
|
|
519
|
+
});
|
|
520
|
+
return { r, open, target };
|
|
521
|
+
};
|
|
522
|
+
// 同小节数且被上一行段覆盖的行,两侧扩展与相同行窗口的扫描都与上一行一致,从上一行结果续扫以免每行重扫整段
|
|
523
|
+
let prev;
|
|
474
524
|
for (let l = 0; l < states.length; l++) {
|
|
475
525
|
const s = states[l];
|
|
476
526
|
if (!s || s.extra <= 0 || !s.totalGaps)
|
|
@@ -478,75 +528,36 @@ function alignHorizontalBars(views, regions, contentWidth, pageBreaks) {
|
|
|
478
528
|
const M = s.minW.length;
|
|
479
529
|
// 2. 向两侧扩展收集连续行段:新行小节数须与本行及段内最大小节数都成整数倍,从而段内各行都整除最大小节数
|
|
480
530
|
// (如 2、6 小节行成段后不再并入 4 小节行);遇到 extra = 0 的挤压行或强制换页即作为刚性边界停下
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
531
|
+
const last = prev?.l === l - 1 && prev.s.minW.length === M && prev.end >= l ? prev : undefined;
|
|
532
|
+
let start = last?.start ?? l, end = last?.end ?? l, maxM = M;
|
|
533
|
+
if (!last) {
|
|
534
|
+
const joins = (n) => !!n && divides(n.minW.length, M) && divides(n.minW.length, maxM);
|
|
535
|
+
while (states[start].extra > 0 && pageBreaks[start] === undefined && joins(states[start - 1]))
|
|
536
|
+
maxM = Math.max(maxM, states[--start].minW.length);
|
|
537
|
+
while (states[end].extra > 0 && pageBreaks[end + 1] === undefined && joins(states[end + 1]))
|
|
538
|
+
maxM = Math.max(maxM, states[++end].minW.length);
|
|
539
|
+
}
|
|
540
|
+
prev = { l, s, start, end };
|
|
487
541
|
if (start === end)
|
|
488
542
|
continue;
|
|
489
|
-
|
|
490
|
-
const env = (lo, hi) => {
|
|
491
|
-
const group = states.slice(lo, hi + 1);
|
|
492
|
-
const L = group.reduce((acc, n) => acc / gcd(acc, n.minW.length) * n.minW.length, 1), r = L / M;
|
|
493
|
-
const grid = new Array(L).fill(0);
|
|
494
|
-
// 先填细网格行,再把粗网格行(如 2 小节行)超出对应细格之和的差额平摊进去
|
|
495
|
-
for (const n of [...group].sort((a, b) => b.minW.length - a.minW.length)) {
|
|
496
|
-
const step = L / n.minW.length;
|
|
497
|
-
for (let m = 0; m < n.minW.length; m++) {
|
|
498
|
-
const w = n.minW[m];
|
|
499
|
-
let sum = 0;
|
|
500
|
-
for (let j = 0; j < step; j++)
|
|
501
|
-
sum += grid[m * step + j];
|
|
502
|
-
if (w > sum)
|
|
503
|
-
for (let j = 0; j < step; j++)
|
|
504
|
-
grid[m * step + j] += (w - sum) / step;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
// 公共台阶总和不超过行宽时,把水位拉到 avg,只填平低于水位的窄格,不拉大已高于水位的宽格
|
|
508
|
-
const free = grid.map((_, g) => Number(group.every(n => n.gaps[Math.floor(g * n.minW.length / L)] > 0)));
|
|
509
|
-
let rem = contentWidth;
|
|
510
|
-
for (let g = 0; g < L; g++)
|
|
511
|
-
if (!free[g])
|
|
512
|
-
rem -= grid[g];
|
|
513
|
-
for (let step = 0; step < L; step++) {
|
|
514
|
-
const freeSum = free.reduce((a, b) => a + b, 0);
|
|
515
|
-
if (!freeSum || rem <= 0)
|
|
516
|
-
break;
|
|
517
|
-
const avg = rem / freeSum;
|
|
518
|
-
if (!grid.some((w, g) => free[g] && w > avg && (rem -= w, free[g] = 0, true))) {
|
|
519
|
-
for (let g = 0; g < L; g++)
|
|
520
|
-
if (free[g])
|
|
521
|
-
grid[g] = avg;
|
|
522
|
-
break;
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
// 将每 r 个细格合回本行的 1 个小节目标宽度(本行被 @rest 等完全锁死的小节保持原宽)
|
|
526
|
-
const target = s.minW.map((w, m) => {
|
|
527
|
-
if (!s.gaps[m])
|
|
528
|
-
return w;
|
|
529
|
-
let sum = 0;
|
|
530
|
-
for (let j = 0; j < r; j++)
|
|
531
|
-
sum += grid[m * r + j];
|
|
532
|
-
return sum;
|
|
533
|
-
});
|
|
534
|
-
return { group, target };
|
|
535
|
-
};
|
|
536
|
-
let { group, target } = env(start, end);
|
|
543
|
+
let { r, open, target } = targetOf(s, start, end);
|
|
537
544
|
// 若整段公共台阶总和超出行宽,退回只与上下紧邻行对齐;与本行台阶相同的相邻行视为一体共用窗口,避免相同行逐行错开
|
|
538
545
|
if (target.reduce((a, b) => a + b, 0) > contentWidth + 1e-6) {
|
|
539
546
|
const same = (n) => n?.minW.length === M
|
|
540
547
|
&& n.minW.every((w, m) => Math.abs(w - s.minW[m]) <= 1e-6 && !n.gaps[m] === !s.gaps[m]);
|
|
541
|
-
|
|
548
|
+
// 与上一行逐值相同时 same 判定不变,上一行窗口的两端必然停在扫描终点,从那里续扫即可
|
|
549
|
+
let [lo, hi] = last?.win && last.win[1] >= l
|
|
550
|
+
&& last.s.minW.every((w, m) => w === s.minW[m] && last.s.gaps[m] === s.gaps[m]) ? last.win : [l, l];
|
|
542
551
|
while (lo > start && same(states[lo - 1]))
|
|
543
552
|
lo--;
|
|
544
553
|
while (hi < end && same(states[hi + 1]))
|
|
545
554
|
hi++;
|
|
546
|
-
|
|
555
|
+
prev.win = [lo, hi];
|
|
556
|
+
({ r, open, target } = targetOf(s, Math.max(start, lo - 1), Math.min(end, hi + 1)));
|
|
547
557
|
}
|
|
548
558
|
// 3. 退水触底:若目标总宽仍超出行宽(空白不够追平邻行),同步回缩并在碰到本行底线 minW 时锁死
|
|
549
|
-
|
|
559
|
+
// 本行第 m 小节对应细格 m·r,段内各行在该细格的自由标记即 open[m·r]
|
|
560
|
+
const free = s.gaps.map((g, m) => g > 0 ? open[m * r] : 0);
|
|
550
561
|
if (!free.some(Boolean))
|
|
551
562
|
for (let m = 0; m < M; m++)
|
|
552
563
|
free[m] = Number(s.gaps[m] > 0);
|
|
@@ -595,7 +606,7 @@ function layoutLocalLine(columns, context) {
|
|
|
595
606
|
for (const node of column)
|
|
596
607
|
prepareLayoutHost(node, context);
|
|
597
608
|
}
|
|
598
|
-
const line = prepareHorizontalLine(columns, columns[0][0].layoutLine,
|
|
609
|
+
const line = prepareHorizontalLine(columns, columns[0][0].layoutLine, context.solverOptions, true);
|
|
599
610
|
let left = Infinity;
|
|
600
611
|
let right = -Infinity;
|
|
601
612
|
for (const column of line.layout(Infinity)) {
|
|
@@ -614,10 +625,10 @@ function layoutLocalLine(columns, context) {
|
|
|
614
625
|
/**
|
|
615
626
|
* 把已横排的子域按正文规则纵向排版,坐标以块的左上角为原点
|
|
616
627
|
*
|
|
617
|
-
*
|
|
618
|
-
*
|
|
628
|
+
* 与正文行共用 placeAndMeasure:沿 root 的 Track 树求轴放置,按登记顺序测量附件,
|
|
629
|
+
* 附件扩张占用时重排一次,最后施加块内的后置平移。positions 是放置时写入的块内布局位置,不含后置平移
|
|
619
630
|
*/
|
|
620
|
-
function layoutSubdomainBlock(columns, { width, view }, root, attachments, context,
|
|
631
|
+
function layoutSubdomainBlock(columns, { width, view }, root, attachments, context, lookup, displacement) {
|
|
621
632
|
const members = columns.flat();
|
|
622
633
|
const extents = { hostExtents: new Map(), attachmentExtents: new Map() };
|
|
623
634
|
let gap = 0;
|
|
@@ -625,63 +636,92 @@ function layoutSubdomainBlock(columns, { width, view }, root, attachments, conte
|
|
|
625
636
|
includeHostExtent(extents.hostExtents, member);
|
|
626
637
|
gap = Math.max(gap, member.ast.size * ROW_GAP_RATIO);
|
|
627
638
|
}
|
|
639
|
+
const lines = [];
|
|
640
|
+
const views = [];
|
|
641
|
+
lines[view.index] = extents;
|
|
642
|
+
views[view.index] = view;
|
|
643
|
+
const domain = { lines, views, lift: liftInto(root) };
|
|
628
644
|
const positions = members.map(member => ({ x: member.box.x, y: 0 }));
|
|
629
|
-
|
|
630
|
-
|
|
645
|
+
let solved;
|
|
646
|
+
const blockContext = attachmentContext(context, domain, { width, originX: 0, pages: [] }, (_line, track) => solved.axes.get(track) ?? 0);
|
|
647
|
+
const { items, layoutItems } = placeAndMeasure(domain, () => {
|
|
648
|
+
solved = solveVerticalAxes(extents, root, gap);
|
|
631
649
|
members.forEach((member, index) => {
|
|
632
650
|
member.box.y = positions[index].y = solved.axes.get(member.track) - member.box.visualAxis;
|
|
633
651
|
member.onPlaced?.();
|
|
634
652
|
});
|
|
635
|
-
return
|
|
653
|
+
return blockContext;
|
|
654
|
+
}, attachments, lookup, displacement, true);
|
|
655
|
+
// 以首个成员为参照:布局快照就是它的块内位置,绘制快照取平移后的位置
|
|
656
|
+
const reference = members[0];
|
|
657
|
+
const painted = { x: reference.box.x, y: reference.box.y };
|
|
658
|
+
return {
|
|
659
|
+
height: solved.height,
|
|
660
|
+
positions,
|
|
661
|
+
items: items.map(({ geometry }, index) => {
|
|
662
|
+
const paint = { geometry, position: painted };
|
|
663
|
+
const layout = layoutItems ? { geometry: layoutItems[index].geometry, position: positions[0] } : paint;
|
|
664
|
+
return { reference, paint, layout };
|
|
665
|
+
}),
|
|
636
666
|
};
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
// 附件按行号查询,子域只占自己所在的那一行
|
|
642
|
-
const lines = [];
|
|
643
|
-
const views = [];
|
|
644
|
-
lines[view.index] = extents;
|
|
645
|
-
views[view.index] = view;
|
|
646
|
-
const measure = () => measureAttachments(attachments, {
|
|
667
|
+
}
|
|
668
|
+
/** 正文与子域的附件上下文只差页面坐标系与视觉轴来源 */
|
|
669
|
+
function attachmentContext(context, { lines, views, lift }, frame, axisOf) {
|
|
670
|
+
return {
|
|
647
671
|
...context,
|
|
648
|
-
|
|
649
|
-
originX: 0,
|
|
650
|
-
pages: [],
|
|
672
|
+
...frame,
|
|
651
673
|
lines: views,
|
|
652
|
-
getVisualAxis: (
|
|
674
|
+
getVisualAxis: (line, track) => axisOf(line, lift(track)),
|
|
653
675
|
getHostExtent: (line, track) => lines[line]?.hostExtents.get(lift(track)),
|
|
654
676
|
getRangeColumns: (line, range, track) => rangeColumns(views[line], range, track, lift),
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
const
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* 正文与子域共用的纵向流程:放置 → 测占用 → [重排一次] → 后置平移 → 测绘制几何
|
|
681
|
+
*
|
|
682
|
+
* place 写入主体坐标并返回本轮附件上下文。有后置平移时首轮只测布局位置的占用,平移后再生成绘制几何;
|
|
683
|
+
* 既无平移也未重排时首轮几何就是最终结果。keepLayout 时另留一份最终布局位置下的占用几何,供外层读取。
|
|
684
|
+
*/
|
|
685
|
+
function placeAndMeasure(domain, place, attachments, lookup, { nodes, layoutOnly }, keepLayout = false) {
|
|
686
|
+
const measure = (context, layoutOnly) => measureAttachmentPass(attachments, context, domain.lift, lookup, layoutOnly);
|
|
687
|
+
let context = place();
|
|
688
|
+
let measured = measure(context, layoutOnly);
|
|
689
|
+
const relayout = reserveOccupancy(domain.lines, measured.occupancy);
|
|
690
|
+
if (relayout)
|
|
691
|
+
context = place();
|
|
692
|
+
const layoutItems = keepLayout && layoutOnly ? (relayout ? measure(context, true) : measured).items : undefined;
|
|
693
|
+
displace(nodes);
|
|
694
|
+
if (relayout || layoutOnly)
|
|
695
|
+
measured = measure(context, false);
|
|
696
|
+
return { items: measured.items, layoutItems };
|
|
671
697
|
}
|
|
672
698
|
/**
|
|
673
|
-
*
|
|
699
|
+
* 首轮占用并入各行的附件占用,供重排时求轴;任一块越出主体占用就需要重排
|
|
674
700
|
*
|
|
675
|
-
*
|
|
701
|
+
* 与逐块比较「主体 ∪ 此前各块」等价:第一个越界块之前的块都落在主体占用之内
|
|
702
|
+
*/
|
|
703
|
+
function reserveOccupancy(lines, occupancy) {
|
|
704
|
+
let needsRelayout = false;
|
|
705
|
+
occupancy.forEach((ranges, line) => {
|
|
706
|
+
const { hostExtents, attachmentExtents } = lines[line];
|
|
707
|
+
for (const { track, top, bottom } of ranges) {
|
|
708
|
+
const host = hostExtents.get(track);
|
|
709
|
+
if (!host || top < host.top || bottom > host.bottom)
|
|
710
|
+
needsRelayout = true;
|
|
711
|
+
includeTrackExtent(attachmentExtents, track, top, bottom);
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
return needsRelayout;
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* 关系附件按首端点所在的各层复合体建立候选索引,由内到外逐层登记
|
|
718
|
+
*
|
|
719
|
+
* 端点都在正文的附件不进索引;子域从这里取候选,再认领端点全部落在自己成员上的关系,不论它写在哪里
|
|
676
720
|
*/
|
|
677
721
|
function groupRelations(attachments) {
|
|
678
722
|
const groups = new Map();
|
|
679
723
|
for (const attachment of attachments) {
|
|
680
|
-
|
|
681
|
-
for (let owner = first?.foldedInto; owner; owner = owner.foldedInto) {
|
|
682
|
-
const candidate = owner;
|
|
683
|
-
if (!rest.every(point => encloses(candidate, point)))
|
|
684
|
-
continue;
|
|
724
|
+
for (let owner = attachment.endPoints?.[0]?.foldedInto; owner; owner = owner.foldedInto) {
|
|
685
725
|
const group = groups.get(owner);
|
|
686
726
|
if (group)
|
|
687
727
|
group.push(attachment);
|
|
@@ -691,30 +731,48 @@ function groupRelations(attachments) {
|
|
|
691
731
|
}
|
|
692
732
|
return groups;
|
|
693
733
|
}
|
|
694
|
-
function encloses(owner, node) {
|
|
695
|
-
for (let host = node.foldedInto; host; host = host.foldedInto) {
|
|
696
|
-
if (host === owner)
|
|
697
|
-
return true;
|
|
698
|
-
}
|
|
699
|
-
return false;
|
|
700
|
-
}
|
|
701
734
|
/** 子域是刚性块:按参照成员的位移平移块内测得的几何 */
|
|
702
|
-
function moveSubdomainItem(
|
|
703
|
-
const
|
|
704
|
-
const
|
|
705
|
-
const
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
735
|
+
function moveSubdomainItem({ reference, paint, layout }, layoutOnly = false) {
|
|
736
|
+
const { geometry, position } = layoutOnly ? layout : paint;
|
|
737
|
+
const dx = reference.box.x - position.x;
|
|
738
|
+
const dy = reference.box.y - position.y;
|
|
739
|
+
return { geometry: dx === 0 && dy === 0 ? geometry : translateGeometry(geometry, dx, dy) };
|
|
740
|
+
}
|
|
741
|
+
function translateGeometry(geometry, dx, dy) {
|
|
709
742
|
const move = (rect) => ({ ...rect, x: rect.x + dx, y: rect.y + dy });
|
|
710
743
|
return {
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
},
|
|
744
|
+
regions: geometry.regions.map(move),
|
|
745
|
+
occupancy: geometry.occupancy?.map(move),
|
|
746
|
+
// attachment 的绘制大多用闭包保存几何,只能代理 painter
|
|
747
|
+
paint: painter => geometry.paint(new TranslatingPainter(painter, dx, dy)),
|
|
716
748
|
};
|
|
717
749
|
}
|
|
750
|
+
function isDisplaced(target) {
|
|
751
|
+
return !!(target.placementOffset?.x || target.placementOffset?.y);
|
|
752
|
+
}
|
|
753
|
+
/** 按 foldedInto 深度排序,外层先平移:复合体的 onPlaced 带动成员,成员再叠加自己的平移 */
|
|
754
|
+
function collectDisplaced(result) {
|
|
755
|
+
const depths = new Map();
|
|
756
|
+
for (const nodes of result.astToTemporal.values()) {
|
|
757
|
+
for (const node of nodes) {
|
|
758
|
+
if (!isVisualTemporalNode(node) || !isDisplaced(node))
|
|
759
|
+
continue;
|
|
760
|
+
let depth = 0;
|
|
761
|
+
for (let host = node.foldedInto; host; host = host.foldedInto)
|
|
762
|
+
depth++;
|
|
763
|
+
depths.set(node, depth);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
return [...depths.keys()].sort((left, right) => depths.get(left) - depths.get(right));
|
|
767
|
+
}
|
|
768
|
+
/** 占用测量完成后施加后置平移;之后不得再放置,否则复合体会把成员同步回布局位置 */
|
|
769
|
+
function displace(nodes) {
|
|
770
|
+
for (const node of nodes) {
|
|
771
|
+
node.box.x += node.placementOffset.x;
|
|
772
|
+
node.box.y += node.placementOffset.y;
|
|
773
|
+
node.onPlaced?.();
|
|
774
|
+
}
|
|
775
|
+
}
|
|
718
776
|
/** 行距取该行最大字号的 ROW_GAP_RATIO 倍;没有可见对象的行回退到全文档最大字号 */
|
|
719
777
|
function measureRowGaps(lineCount, objects, override) {
|
|
720
778
|
if (override !== void 0)
|
|
@@ -732,67 +790,19 @@ function measureRowGaps(lineCount, objects, override) {
|
|
|
732
790
|
gaps[i] ||= documentGap;
|
|
733
791
|
return gaps;
|
|
734
792
|
}
|
|
735
|
-
/**
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
y += host.placementOffset?.y ?? 0;
|
|
742
|
-
}
|
|
743
|
-
return { x, y };
|
|
744
|
-
}
|
|
745
|
-
/** 后置平移只改变绘制几何,占用始终在未平移的布局位置测量 */
|
|
746
|
-
function measureAttachments(attachments, baseContext, lines, lift, measuredElsewhere, hosts, owner) {
|
|
747
|
-
if (attachments.length === 0)
|
|
748
|
-
return { items: [], needsRelayout: false };
|
|
749
|
-
const shifts = [];
|
|
750
|
-
for (const node of hosts) {
|
|
751
|
-
const { x, y } = placementOffset(node, owner);
|
|
752
|
-
if (x !== 0 || y !== 0)
|
|
753
|
-
shifts.push({ node, x, y, boxX: node.box.x, boxY: node.box.y });
|
|
754
|
-
}
|
|
755
|
-
const displaced = shifts.length > 0 || attachments.some(attachment => attachment.placementOffset?.x || attachment.placementOffset?.y);
|
|
756
|
-
let layout;
|
|
757
|
-
if (displaced) {
|
|
758
|
-
for (const { node, x, y } of shifts) {
|
|
759
|
-
node.box.x -= x;
|
|
760
|
-
node.box.y -= y;
|
|
761
|
-
}
|
|
762
|
-
try {
|
|
763
|
-
layout = measureAttachmentPass(attachments, { ...baseContext, layoutOnly: true }, lines, lift, measuredElsewhere, true);
|
|
764
|
-
}
|
|
765
|
-
finally {
|
|
766
|
-
for (const { node, boxX, boxY } of shifts) {
|
|
767
|
-
node.box.x = boxX;
|
|
768
|
-
node.box.y = boxY;
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
else
|
|
773
|
-
layout = measureAttachmentPass(attachments, baseContext, lines, lift, measuredElsewhere, true);
|
|
774
|
-
if (!displaced)
|
|
775
|
-
return layout;
|
|
776
|
-
// 试测结果若被重排丢弃,就不再为它生成绘制几何
|
|
777
|
-
let painted;
|
|
778
|
-
return {
|
|
779
|
-
needsRelayout: layout.needsRelayout,
|
|
780
|
-
layoutItems: layout.items,
|
|
781
|
-
get items() {
|
|
782
|
-
return painted ??= measureAttachmentPass(attachments, baseContext, lines, lift, measuredElsewhere, false).items;
|
|
783
|
-
},
|
|
784
|
-
};
|
|
785
|
-
}
|
|
786
|
-
/** 按声明顺序测量几何,后注册的附件可以查询此前的区域和占用 */
|
|
787
|
-
function measureAttachmentPass(attachments, baseContext, lines, lift, measuredElsewhere, reserve) {
|
|
793
|
+
/**
|
|
794
|
+
* 按声明顺序测量几何,后注册的附件可以查询此前的区域和占用;只读主体坐标,不改写行占用
|
|
795
|
+
*
|
|
796
|
+
* layoutOnly 用于有后置平移时的占用测量:主体尚未平移,附件自身的平移也不施加,依赖的子域附件取布局快照
|
|
797
|
+
*/
|
|
798
|
+
function measureAttachmentPass(attachments, baseContext, lift, lookup, layoutOnly) {
|
|
788
799
|
const measured = new Map();
|
|
789
800
|
const occupancy = [];
|
|
790
|
-
let needsRelayout = false;
|
|
791
801
|
// 同一测量轮坐标不变,子域几何只需平移一次
|
|
792
802
|
function resolveAttachment(dependency) {
|
|
793
803
|
let item = measured.get(dependency);
|
|
794
804
|
if (!item) {
|
|
795
|
-
item =
|
|
805
|
+
item = lookup(dependency, layoutOnly);
|
|
796
806
|
if (item)
|
|
797
807
|
measured.set(dependency, item);
|
|
798
808
|
}
|
|
@@ -831,10 +841,9 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
|
|
|
831
841
|
if (host.box.x + host.box.w > right)
|
|
832
842
|
right = host.box.x + host.box.w;
|
|
833
843
|
}
|
|
834
|
-
const
|
|
835
|
-
const top = decoration?.top ?? 0;
|
|
844
|
+
const { top, height } = hostSpan(host);
|
|
836
845
|
const hostTop = host.box.y + top - baseContext.getVisualAxis(line, host.track);
|
|
837
|
-
include(host.track, hostTop, hostTop +
|
|
846
|
+
include(host.track, hostTop, hostTop + height);
|
|
838
847
|
}
|
|
839
848
|
}
|
|
840
849
|
for (const range of occupancy[line] ?? []) {
|
|
@@ -889,42 +898,38 @@ function measureAttachmentPass(attachments, baseContext, lines, lift, measuredEl
|
|
|
889
898
|
},
|
|
890
899
|
};
|
|
891
900
|
const items = attachments.map(attachment => {
|
|
892
|
-
|
|
901
|
+
let geometry = attachment.createGeometry(context);
|
|
902
|
+
if (!layoutOnly && isDisplaced(attachment)) {
|
|
903
|
+
geometry = translateGeometry(geometry, attachment.placementOffset.x, attachment.placementOffset.y);
|
|
904
|
+
}
|
|
893
905
|
const item = { geometry };
|
|
894
906
|
measured.set(attachment, item);
|
|
895
907
|
// 当前项立即登记,保证后注册的 attachment 能看到并避让它
|
|
896
908
|
for (const region of geometry.occupancy ?? geometry.regions) {
|
|
897
909
|
if (region.line === void 0)
|
|
898
910
|
continue;
|
|
899
|
-
const line = lines[region.line];
|
|
900
911
|
const track = lift(region.track);
|
|
901
912
|
const top = region.y - baseContext.getVisualAxis(region.line, track);
|
|
902
|
-
const bottom = top + region.h;
|
|
903
|
-
const hostExtent = line.hostExtents.get(track);
|
|
904
|
-
const attachmentExtent = line.attachmentExtents.get(track);
|
|
905
|
-
if (reserve && (top < Math.min(hostExtent?.top ?? Infinity, attachmentExtent?.top ?? Infinity)
|
|
906
|
-
|| bottom > Math.max(hostExtent?.bottom ?? -Infinity, attachmentExtent?.bottom ?? -Infinity))) {
|
|
907
|
-
needsRelayout = true;
|
|
908
|
-
}
|
|
909
|
-
if (reserve)
|
|
910
|
-
includeTrackExtent(line.attachmentExtents, track, top, bottom);
|
|
911
913
|
(occupancy[region.line] ??= []).push({
|
|
912
914
|
track,
|
|
913
915
|
left: region.x,
|
|
914
916
|
right: region.x + region.w,
|
|
915
917
|
top,
|
|
916
|
-
bottom,
|
|
918
|
+
bottom: top + region.h,
|
|
917
919
|
});
|
|
918
920
|
}
|
|
919
921
|
return item;
|
|
920
922
|
});
|
|
921
|
-
return { items,
|
|
923
|
+
return { items, occupancy };
|
|
922
924
|
}
|
|
923
|
-
/**
|
|
924
|
-
function
|
|
925
|
+
/** 主体完整占用相对盒顶的上沿与高度;调用方各自换算到轴局部或行坐标,不必创建全局矩形 */
|
|
926
|
+
function hostSpan(host) {
|
|
925
927
|
const decoration = decorationExtents.get(host);
|
|
926
928
|
const top = decoration?.top ?? 0;
|
|
927
|
-
|
|
929
|
+
return { top, height: host.box.h - top + (decoration?.bottom ?? 0) };
|
|
930
|
+
}
|
|
931
|
+
function includeHostExtent(extents, host) {
|
|
932
|
+
const { top, height } = hostSpan(host);
|
|
928
933
|
const axisTop = top - host.box.visualAxis;
|
|
929
934
|
includeTrackExtent(extents, host.track, axisTop, axisTop + height);
|
|
930
935
|
}
|