jpfun 0.4.1 → 0.5.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 +53 -113
- package/dist/converter/midi/convert.js +14 -7
- package/dist/converter/musicxml/features.d.ts +57 -16
- package/dist/converter/musicxml/features.js +204 -42
- package/dist/converter/musicxml/index.d.ts +7 -2
- package/dist/converter/musicxml/index.js +256 -268
- package/dist/converter/musicxml/model.d.ts +36 -7
- package/dist/converter/musicxml/resolve.d.ts +10 -0
- package/dist/converter/musicxml/resolve.js +152 -0
- package/dist/core.d.ts +1 -0
- package/dist/core.js +1 -0
- package/dist/fraction.js +7 -1
- package/dist/functions/ASTtypes.d.ts +2 -3
- package/dist/functions/ASTtypes.js +5 -9
- package/dist/functions/adjust/index.js +4 -48
- package/dist/functions/arpeggio/index.js +21 -13
- package/dist/functions/box/index.js +81 -82
- package/dist/functions/dash/index.js +20 -11
- package/dist/functions/div/index.d.ts +1 -1
- package/dist/functions/div/index.js +7 -7
- package/dist/functions/dot/index.js +4 -5
- package/dist/functions/dyn/index.js +28 -32
- package/dist/functions/grace/index.d.ts +19 -6
- package/dist/functions/grace/index.js +123 -115
- package/dist/functions/key/index.js +1 -1
- package/dist/functions/meter/index.js +14 -1
- package/dist/functions/note/index.d.ts +6 -1
- package/dist/functions/note/index.js +37 -14
- package/dist/functions/note/source-transform.d.ts +21 -0
- package/dist/functions/note/source-transform.js +164 -0
- package/dist/functions/page/index.d.ts +5 -0
- package/dist/functions/page/index.js +8 -5
- package/dist/functions/set/index.js +5 -1
- package/dist/functions/symbol/index.d.ts +2 -0
- package/dist/functions/symbol/index.js +1 -1
- package/dist/functions/symbol/ornament.d.ts +3 -3
- package/dist/functions/symbol/ornament.js +21 -35
- package/dist/functions/symbol/symbols/accent.js +5 -5
- package/dist/functions/symbol/symbols/fermata.js +5 -4
- package/dist/functions/tie/index.js +22 -32
- package/dist/functions/tuplet/index.js +19 -23
- package/dist/functions/up/index.js +9 -9
- package/dist/functions/voice/index.js +4 -3
- package/dist/functions/volta/index.js +7 -12
- package/dist/jpfun.core.min.js +40 -37
- package/dist/jpfun.from-midi.min.js +5 -5
- package/dist/jpfun.from-musicxml.min.js +8 -8
- package/dist/jpfun.min.js +49 -46
- package/dist/layout/engine.d.ts +11 -1
- package/dist/layout/engine.js +299 -96
- package/dist/layout/page.js +2 -0
- package/dist/layout/range.d.ts +26 -0
- package/dist/layout/range.js +75 -0
- package/dist/layout/types.d.ts +33 -4
- package/dist/lowering/types.d.ts +5 -0
- package/dist/parser/parse-utils/length-utils.js +2 -0
- package/dist/parser/parse-utils/note-utils.d.ts +2 -0
- package/dist/parser/parse-utils/note-utils.js +6 -2
- package/dist/parser/parserContext.js +1 -1
- package/dist/playback/compile.d.ts +2 -9
- package/dist/playback/compile.js +297 -186
- package/dist/playback/event.d.ts +8 -16
- package/dist/playback/event.js +13 -3
- package/dist/playback/types.d.ts +76 -18
- package/dist/playback/types.js +1 -0
- package/dist/render/index.d.ts +1 -1
- package/dist/render/index.js +1 -1
- package/dist/render/recording.d.ts +1 -0
- package/dist/render/recording.js +21 -0
- package/dist/render/translate.d.ts +14 -0
- package/dist/render/translate.js +48 -0
- package/grammar.md +13 -6
- package/package.json +4 -4
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 { LayoutPrepareContext, PlacedAttachment, Rect } from "./types.js";
|
|
6
|
+
import type { HorizontalLineView, LayoutPrepareContext, PlacedAttachment, Rect } from "./types.js";
|
|
7
7
|
export interface DocumentLayoutOptions extends SolverOptions {
|
|
8
8
|
rowGap?: number;
|
|
9
9
|
}
|
|
@@ -39,3 +39,13 @@ export declare function prepareLayoutHost(node: VisualTemporalNode, context: Lay
|
|
|
39
39
|
* 完成 lowering 结果的全部几何计算
|
|
40
40
|
*/
|
|
41
41
|
export declare function layoutDocument(result: LoweringResult, context: LayoutPrepareContext, options?: DocumentLayoutOptions): DocumentLayoutResult;
|
|
42
|
+
/**
|
|
43
|
+
* 为已完成尺寸准备的成员建立横向视图并注册约束;调用 layout 时才创建输入和执行 hook
|
|
44
|
+
* compact 只清零本轮自然间隙,保留时值和弹簧配置;局部列顺序由调用方提供
|
|
45
|
+
*/
|
|
46
|
+
export declare function prepareHorizontalLine(columns: readonly (readonly VisualTemporalNode[])[], index: number, options?: SolverOptions, compact?: boolean): {
|
|
47
|
+
view: HorizontalLineView;
|
|
48
|
+
layout(limit: number, fillMinRatio?: number): import("./model.js").LayoutElement[][];
|
|
49
|
+
};
|
|
50
|
+
/** 局部序列按给定顺序完成尺寸、约束与零间隙自然横排,归零占位左沿并返回完整宽度 */
|
|
51
|
+
export declare function layoutLocalSequence(nodes: readonly VisualTemporalNode[], context: LayoutPrepareContext): number;
|
package/dist/layout/engine.js
CHANGED
|
@@ -3,8 +3,12 @@ import { isVisualTemporalNode, } from "../functions/temporal.js";
|
|
|
3
3
|
import { completeSpringConfig, layoutElement, layoutHorizontal, } from "./model.js";
|
|
4
4
|
import { normalizePageConfig, paginateLayoutLines, PageLayoutError, } from "./page.js";
|
|
5
5
|
import { isLayoutAttachment } from "./types.js";
|
|
6
|
+
import { liftInto, rangeColumns, resolveRange } from "./range.js";
|
|
7
|
+
import { TranslatingPainter } from "../render/translate.js";
|
|
6
8
|
// 兼容旧导入路径;布局入口会把正常页面溢出转换为 ErrorDiagnostic
|
|
7
9
|
export { PageLayoutError } from "./page.js";
|
|
10
|
+
/** 轨道间距相对行内最大字号的比例;正文行与子域共用 */
|
|
11
|
+
const ROW_GAP_RATIO = 0.75;
|
|
8
12
|
/**
|
|
9
13
|
* 把多个已经定位的盒子合并到 target
|
|
10
14
|
*/
|
|
@@ -70,6 +74,40 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
70
74
|
const page = normalizePageConfig(result.page);
|
|
71
75
|
const contentWidth = page.width - page.marginLeft - page.marginRight;
|
|
72
76
|
const originX = page.marginLeft;
|
|
77
|
+
const lift = liftInto(result.rootTrack);
|
|
78
|
+
// 子域在主体准备时就排版完毕;它认领的附件保留块内几何,随块平移
|
|
79
|
+
const order = new Map(layoutAttachments.map((attachment, index) => [attachment, index]));
|
|
80
|
+
const relations = groupRelations(layoutAttachments);
|
|
81
|
+
const subdomainItems = new Map();
|
|
82
|
+
const subdomainBox = (attachment) => {
|
|
83
|
+
const item = subdomainItems.get(attachment);
|
|
84
|
+
return item && moveSubdomainItem(item).box;
|
|
85
|
+
};
|
|
86
|
+
context = {
|
|
87
|
+
...context,
|
|
88
|
+
layoutSubdomain(owner, columns, root, attachments) {
|
|
89
|
+
// 横排时内层子域先排版并认领关系,这里只取剩下的
|
|
90
|
+
const line = layoutLocalLine(columns, context);
|
|
91
|
+
const members = new Set(columns.flat());
|
|
92
|
+
const within = (node) => {
|
|
93
|
+
for (let host = node; host && host !== owner; host = host.foldedInto) {
|
|
94
|
+
if (members.has(host))
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
};
|
|
99
|
+
// 内容附件跟随声明位置,关系附件跟随端点
|
|
100
|
+
const own = attachments.filter(isLayoutAttachment).filter(attachment => !attachment.endPoints?.length);
|
|
101
|
+
for (const relation of relations.get(owner) ?? []) {
|
|
102
|
+
if (!subdomainItems.has(relation) && relation.endPoints.every(within))
|
|
103
|
+
own.push(relation);
|
|
104
|
+
}
|
|
105
|
+
own.sort((left, right) => order.get(left) - order.get(right));
|
|
106
|
+
const block = layoutSubdomainBlock(columns, line, root, own, context, subdomainBox);
|
|
107
|
+
own.forEach((attachment, index) => subdomainItems.set(attachment, block.items[index]));
|
|
108
|
+
return { width: line.width, height: block.height, positions: block.positions };
|
|
109
|
+
},
|
|
110
|
+
};
|
|
73
111
|
// 1. 按 layoutLine 切行,并生成固有尺寸与装饰尺寸
|
|
74
112
|
const lines = splitLayoutLines(result);
|
|
75
113
|
const objects = [];
|
|
@@ -81,17 +119,15 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
81
119
|
}
|
|
82
120
|
}
|
|
83
121
|
}
|
|
122
|
+
const documentAttachments = layoutAttachments.filter(attachment => !subdomainItems.has(attachment));
|
|
84
123
|
// 2. 横向弹簧布局,得到 box.x
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
for (const attachment of layoutAttachments)
|
|
124
|
+
const horizontal = lines.map((line, index) => prepareHorizontalLine(line.columns, index, options));
|
|
125
|
+
const views = horizontal.map(line => line.view);
|
|
126
|
+
for (const attachment of documentAttachments)
|
|
89
127
|
attachment.prepareHorizontal?.(views, context);
|
|
90
|
-
for (
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
layoutHorizontal(elements, contentWidth, options, line.horizontalLayoutHooks, 0.5);
|
|
94
|
-
for (const column of line.columns) {
|
|
128
|
+
for (const line of horizontal) {
|
|
129
|
+
line.layout(contentWidth, page.fillRatio);
|
|
130
|
+
for (const column of line.view.columns) {
|
|
95
131
|
for (const node of column)
|
|
96
132
|
node.box.x += originX;
|
|
97
133
|
}
|
|
@@ -147,21 +183,25 @@ export function layoutDocument(result, context, options = {}) {
|
|
|
147
183
|
originX,
|
|
148
184
|
pages: pages.map(item => item.bounds),
|
|
149
185
|
lines: views,
|
|
150
|
-
|
|
151
|
-
|
|
186
|
+
// 子域的私有轨在正文里归入复合体所在的轨
|
|
187
|
+
getVisualAxis: (line, track) => visualAxisOf(line, lift(track)),
|
|
188
|
+
getHostExtent: (line, track) => lines[line]?.hostExtents.get(lift(track)),
|
|
189
|
+
getRangeColumns: (line, range, track) => rangeColumns(views[line], range, track, lift),
|
|
152
190
|
};
|
|
153
191
|
return { pages, attachmentContext };
|
|
154
192
|
};
|
|
155
193
|
// 3. 首次纵向放置后测量 attachment;只有有效轨道占用扩张时才重新求解
|
|
156
194
|
let placement = placeVertically();
|
|
157
|
-
let measured = measureAttachments(
|
|
195
|
+
let measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainBox);
|
|
158
196
|
if (measured.needsRelayout) {
|
|
159
197
|
placement = placeVertically();
|
|
160
|
-
measured = measureAttachments(
|
|
198
|
+
measured = measureAttachments(documentAttachments, placement.attachmentContext, lines, lift, subdomainBox);
|
|
161
199
|
}
|
|
162
200
|
const pages = placement.pages;
|
|
163
|
-
|
|
164
|
-
|
|
201
|
+
let next = 0;
|
|
202
|
+
const attachments = layoutAttachments.map(attachment => {
|
|
203
|
+
const item = subdomainItems.get(attachment);
|
|
204
|
+
const { geometry, box } = item ? moveSubdomainItem(item) : measured.items[next++];
|
|
165
205
|
return {
|
|
166
206
|
box,
|
|
167
207
|
regions: geometry.regions,
|
|
@@ -221,10 +261,8 @@ function arrangeBelowDecorations(node) {
|
|
|
221
261
|
function splitLayoutLines(result) {
|
|
222
262
|
const createLine = () => ({
|
|
223
263
|
columns: [],
|
|
224
|
-
horizontalLayoutHooks: [],
|
|
225
264
|
hostExtents: new Map(),
|
|
226
265
|
attachmentExtents: new Map(),
|
|
227
|
-
attachmentRanges: [],
|
|
228
266
|
});
|
|
229
267
|
const lines = [];
|
|
230
268
|
let currentLine = createLine();
|
|
@@ -260,50 +298,186 @@ function includeTrackExtent(extents, track, top, bottom) {
|
|
|
260
298
|
extents.set(track, { top, bottom });
|
|
261
299
|
}
|
|
262
300
|
/**
|
|
263
|
-
*
|
|
301
|
+
* 为已完成尺寸准备的成员建立横向视图并注册约束;调用 layout 时才创建输入和执行 hook
|
|
302
|
+
* compact 只清零本轮自然间隙,保留时值和弹簧配置;局部列顺序由调用方提供
|
|
264
303
|
*/
|
|
265
|
-
function
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
}
|
|
304
|
+
export function prepareHorizontalLine(columns, index, options = {}, compact = false) {
|
|
305
|
+
const hooks = [];
|
|
306
|
+
const columnIndex = new Map();
|
|
307
|
+
const trackRuns = new Map();
|
|
308
|
+
for (let column = 0; column < columns.length; column++) {
|
|
309
|
+
for (const node of columns[column]) {
|
|
310
|
+
columnIndex.set(node, column);
|
|
311
|
+
completeSpringConfig(node.springConfig, options.globalC);
|
|
312
|
+
const run = trackRuns.get(node.track);
|
|
313
|
+
if (run)
|
|
314
|
+
run.push(node);
|
|
315
|
+
else
|
|
316
|
+
trackRuns.set(node.track, [node]);
|
|
279
317
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
}
|
|
318
|
+
}
|
|
319
|
+
const view = {
|
|
320
|
+
index,
|
|
321
|
+
columns,
|
|
322
|
+
trackRuns,
|
|
323
|
+
columnOf: host => columnIndex.get(host) ?? -1,
|
|
324
|
+
registerHorizontalLayoutHook(from, to, hook) {
|
|
325
|
+
const start = columnIndex.get(from);
|
|
326
|
+
const end = columnIndex.get(to);
|
|
327
|
+
if (start === void 0 || end === void 0)
|
|
328
|
+
return;
|
|
329
|
+
hooks.push({ start: Math.min(start, end), end: Math.max(start, end), hook });
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
for (const column of columns) {
|
|
333
|
+
for (const node of column)
|
|
334
|
+
node.prepareHorizontal?.(view);
|
|
335
|
+
}
|
|
336
|
+
return {
|
|
337
|
+
view,
|
|
338
|
+
layout(limit, fillMinRatio) {
|
|
339
|
+
const elements = columns.map(column => column.map(node => {
|
|
340
|
+
const element = layoutElement(node.springConfig, node.box, node, options.globalC);
|
|
341
|
+
if (compact)
|
|
342
|
+
element.margin_L = element.margin_R = 0;
|
|
343
|
+
return element;
|
|
344
|
+
}));
|
|
345
|
+
layoutHorizontal(elements, limit, options, hooks, fillMinRatio);
|
|
346
|
+
return elements;
|
|
347
|
+
},
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
/** 局部序列按给定顺序完成尺寸、约束与零间隙自然横排,归零占位左沿并返回完整宽度 */
|
|
351
|
+
export function layoutLocalSequence(nodes, context) {
|
|
352
|
+
return nodes.length === 0 ? 0 : layoutLocalLine(nodes.map(node => [node]), context).width;
|
|
353
|
+
}
|
|
354
|
+
/** 同 layoutLocalSequence,但同列成员按各自 Track 分行;columns 不能为空 */
|
|
355
|
+
function layoutLocalLine(columns, context) {
|
|
356
|
+
for (const column of columns) {
|
|
357
|
+
for (const node of column)
|
|
358
|
+
prepareLayoutHost(node, context);
|
|
359
|
+
}
|
|
360
|
+
const line = prepareHorizontalLine(columns, columns[0][0].layoutLine, {}, true);
|
|
361
|
+
let left = Infinity;
|
|
362
|
+
let right = -Infinity;
|
|
363
|
+
for (const column of line.layout(Infinity)) {
|
|
364
|
+
for (const element of column) {
|
|
365
|
+
const anchor = element.box.x + element.box.anchor;
|
|
366
|
+
left = Math.min(left, anchor - element.WL);
|
|
367
|
+
right = Math.max(right, anchor + element.WR);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
for (const column of columns) {
|
|
371
|
+
for (const node of column)
|
|
372
|
+
node.box.x -= left;
|
|
373
|
+
}
|
|
374
|
+
return { width: right - left, view: line.view };
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* 把已横排的子域按正文规则纵向排版,坐标以块的左上角为原点
|
|
378
|
+
*
|
|
379
|
+
* 与正文行的流程相同:沿 root 的 Track 树求轴放置,按登记顺序测量附件,
|
|
380
|
+
* 附件扩张占用时重排一次。positions 是放置时写入的块内位置,不含成员 onPlaced 追加的平移
|
|
381
|
+
*/
|
|
382
|
+
function layoutSubdomainBlock(columns, { width, view }, root, attachments, context, measuredElsewhere) {
|
|
383
|
+
const members = columns.flat();
|
|
384
|
+
const extents = { hostExtents: new Map(), attachmentExtents: new Map() };
|
|
385
|
+
let gap = 0;
|
|
386
|
+
for (const member of members) {
|
|
387
|
+
const top = -member.box.visualAxis;
|
|
388
|
+
includeTrackExtent(extents.hostExtents, member.track, top, top + member.box.h);
|
|
389
|
+
gap = Math.max(gap, member.ast.size * ROW_GAP_RATIO);
|
|
390
|
+
}
|
|
391
|
+
const positions = members.map(member => ({ x: member.box.x, y: 0 }));
|
|
392
|
+
const place = () => {
|
|
393
|
+
const solved = solveVerticalAxes(extents, root, gap);
|
|
394
|
+
members.forEach((member, index) => {
|
|
395
|
+
member.box.y = positions[index].y = solved.axes.get(member.track) - member.box.visualAxis;
|
|
396
|
+
member.onPlaced?.();
|
|
397
|
+
});
|
|
398
|
+
return solved;
|
|
399
|
+
};
|
|
400
|
+
let solved = place();
|
|
401
|
+
if (attachments.length === 0)
|
|
402
|
+
return { height: solved.height, positions, items: [] };
|
|
403
|
+
const lift = liftInto(root);
|
|
404
|
+
// 附件按行号查询,子域只占自己所在的那一行
|
|
405
|
+
const lines = [];
|
|
406
|
+
const views = [];
|
|
407
|
+
lines[view.index] = extents;
|
|
408
|
+
views[view.index] = view;
|
|
409
|
+
const measure = () => measureAttachments(attachments, {
|
|
410
|
+
...context,
|
|
411
|
+
width,
|
|
412
|
+
originX: 0,
|
|
413
|
+
pages: [],
|
|
414
|
+
lines: views,
|
|
415
|
+
getVisualAxis: (_line, track) => solved.axes.get(lift(track)) ?? 0,
|
|
416
|
+
getHostExtent: (line, track) => lines[line]?.hostExtents.get(lift(track)),
|
|
417
|
+
getRangeColumns: (line, range, track) => rangeColumns(views[line], range, track, lift),
|
|
418
|
+
}, lines, lift, measuredElsewhere);
|
|
419
|
+
let measured = measure();
|
|
420
|
+
if (measured.needsRelayout) {
|
|
421
|
+
solved = place();
|
|
422
|
+
measured = measure();
|
|
423
|
+
}
|
|
424
|
+
const reference = members[0];
|
|
425
|
+
const items = measured.items.map(item => ({ ...item, reference, x: reference.box.x, y: reference.box.y }));
|
|
426
|
+
return { height: solved.height, positions, items };
|
|
298
427
|
}
|
|
299
|
-
/**
|
|
428
|
+
/**
|
|
429
|
+
* 关系附件按同时包含全部端点的复合体分组,由内到外逐层登记
|
|
430
|
+
*
|
|
431
|
+
* 端点都在正文的附件不进分组;子域从这里认领端点全部落在自己成员上的关系,不论它写在哪里
|
|
432
|
+
*/
|
|
433
|
+
function groupRelations(attachments) {
|
|
434
|
+
const groups = new Map();
|
|
435
|
+
for (const attachment of attachments) {
|
|
436
|
+
const [first, ...rest] = attachment.endPoints ?? [];
|
|
437
|
+
for (let owner = first?.foldedInto; owner; owner = owner.foldedInto) {
|
|
438
|
+
const candidate = owner;
|
|
439
|
+
if (!rest.every(point => encloses(candidate, point)))
|
|
440
|
+
continue;
|
|
441
|
+
const group = groups.get(owner);
|
|
442
|
+
if (group)
|
|
443
|
+
group.push(attachment);
|
|
444
|
+
else
|
|
445
|
+
groups.set(owner, [attachment]);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return groups;
|
|
449
|
+
}
|
|
450
|
+
function encloses(owner, node) {
|
|
451
|
+
for (let host = node.foldedInto; host; host = host.foldedInto) {
|
|
452
|
+
if (host === owner)
|
|
453
|
+
return true;
|
|
454
|
+
}
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
457
|
+
/** 子域是刚性块:按参照成员的位移平移块内测得的几何 */
|
|
458
|
+
function moveSubdomainItem(item) {
|
|
459
|
+
const dx = item.reference.box.x - item.x;
|
|
460
|
+
const dy = item.reference.box.y - item.y;
|
|
461
|
+
if (dx === 0 && dy === 0)
|
|
462
|
+
return item;
|
|
463
|
+
const move = (rect) => ({ ...rect, x: rect.x + dx, y: rect.y + dy });
|
|
464
|
+
const { geometry } = item;
|
|
465
|
+
return {
|
|
466
|
+
box: move(item.box),
|
|
467
|
+
geometry: {
|
|
468
|
+
regions: geometry.regions.map(move),
|
|
469
|
+
paint: painter => geometry.paint(new TranslatingPainter(painter, dx, dy)),
|
|
470
|
+
},
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
/** 行距取该行最大字号的 ROW_GAP_RATIO 倍;没有可见对象的行回退到全文档最大字号 */
|
|
300
474
|
function measureRowGaps(lineCount, objects, override) {
|
|
301
475
|
if (override !== void 0)
|
|
302
476
|
return new Array(lineCount).fill(override);
|
|
303
477
|
const gaps = new Array(lineCount).fill(0);
|
|
304
478
|
let documentGap = 0;
|
|
305
479
|
for (const node of objects) {
|
|
306
|
-
const gap = node.ast.size *
|
|
480
|
+
const gap = node.ast.size * ROW_GAP_RATIO;
|
|
307
481
|
if (gap > gaps[node.layoutLine])
|
|
308
482
|
gaps[node.layoutLine] = gap;
|
|
309
483
|
if (gap > documentGap)
|
|
@@ -316,58 +490,86 @@ function measureRowGaps(lineCount, objects, override) {
|
|
|
316
490
|
/**
|
|
317
491
|
* 按 lowering 注册顺序原子生成本轮几何,并同步登记轨道占用
|
|
318
492
|
*
|
|
493
|
+
* 正文与子域共用:lines 与 baseContext.lines 都按行号索引,lift 把域外的私有轨归入本域。
|
|
319
494
|
* 分组的 attachment 总在组内对象之后注册,所以读取依赖不需要额外排序;
|
|
320
495
|
* 占用逐条累加,后注册者因此能通过 getRangeExtents 避让先注册者。
|
|
496
|
+
* 其他域测得的附件由 measuredElsewhere 给出当前位置。
|
|
321
497
|
* 返回的 needsRelayout 表示本轮占用超出了主体与先前区域的合并范围。
|
|
322
498
|
*/
|
|
323
|
-
function measureAttachments(attachments, baseContext, lines) {
|
|
499
|
+
function measureAttachments(attachments, baseContext, lines, lift, measuredElsewhere) {
|
|
324
500
|
const measured = new Map();
|
|
501
|
+
const own = new Set(attachments);
|
|
502
|
+
const occupancy = [];
|
|
325
503
|
let needsRelayout = false;
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
504
|
+
function getRangeExtents(line, columns, track) {
|
|
505
|
+
const view = baseContext.lines[line];
|
|
506
|
+
const extents = track === undefined ? new Map() : undefined;
|
|
507
|
+
let extent;
|
|
508
|
+
if (!view)
|
|
509
|
+
return extents;
|
|
510
|
+
const include = (owner, top, bottom) => {
|
|
511
|
+
if (extents)
|
|
512
|
+
includeTrackExtent(extents, owner, top, bottom);
|
|
513
|
+
else if (extent)
|
|
514
|
+
includeExtent(extent, top, bottom);
|
|
515
|
+
else
|
|
516
|
+
extent = { top, bottom };
|
|
517
|
+
};
|
|
518
|
+
// 主体和附件共用范围筛选,仅累加结果的容器随查询模式变化。
|
|
519
|
+
const { start, end, wholeLine } = resolveRange(view, columns, track, lift);
|
|
520
|
+
const target = track && lift(track);
|
|
521
|
+
let left = wholeLine ? baseContext.originX : Infinity;
|
|
522
|
+
let right = wholeLine ? baseContext.originX + baseContext.width : -Infinity;
|
|
523
|
+
for (let column = start; column <= end; column++) {
|
|
524
|
+
for (const host of view.columns[column]) {
|
|
525
|
+
if (target && host.track !== target)
|
|
526
|
+
continue;
|
|
527
|
+
if (!wholeLine) {
|
|
528
|
+
if (host.box.x < left)
|
|
529
|
+
left = host.box.x;
|
|
530
|
+
if (host.box.x + host.box.w > right)
|
|
531
|
+
right = host.box.x + host.box.w;
|
|
532
|
+
}
|
|
533
|
+
const hostTop = host.box.y - baseContext.getVisualAxis(line, host.track);
|
|
534
|
+
include(host.track, hostTop, hostTop + host.box.h);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
for (const range of occupancy[line] ?? []) {
|
|
538
|
+
if (target && range.track !== target)
|
|
539
|
+
continue;
|
|
540
|
+
if (range.right <= left || range.left >= right)
|
|
541
|
+
continue;
|
|
542
|
+
include(range.track, range.top, range.bottom);
|
|
543
|
+
}
|
|
544
|
+
return extents ?? extent;
|
|
545
|
+
}
|
|
329
546
|
const context = {
|
|
330
547
|
...baseContext,
|
|
548
|
+
getRangeExtents,
|
|
331
549
|
getAttachmentBox(dependency) {
|
|
332
|
-
const resolved = measured.get(dependency);
|
|
550
|
+
const resolved = measured.get(dependency) ?? measuredElsewhere(dependency);
|
|
333
551
|
if (!resolved)
|
|
334
552
|
throw new Error("Layout attachment dependency has not been measured");
|
|
335
|
-
return resolved
|
|
553
|
+
return resolved;
|
|
336
554
|
},
|
|
337
|
-
|
|
338
|
-
const
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
// 主体按列精确选取;先完成的 attachment 再按这些列的最终横向范围相交选取
|
|
343
|
-
const wholeLine = columns === undefined;
|
|
344
|
-
if (columns && columns[0] > columns[1])
|
|
345
|
-
return extents;
|
|
346
|
-
const start = wholeLine ? 0 : Math.max(0, columns[0]);
|
|
347
|
-
const end = wholeLine ? target.columns.length - 1 : Math.min(columns[1], target.columns.length - 1);
|
|
348
|
-
if (!wholeLine && start > end)
|
|
349
|
-
return extents;
|
|
350
|
-
let left = wholeLine ? baseContext.originX : Infinity;
|
|
351
|
-
let right = wholeLine ? baseContext.originX + baseContext.width : -Infinity;
|
|
352
|
-
for (let column = start; column <= end; column++) {
|
|
353
|
-
for (const host of target.columns[column]) {
|
|
354
|
-
if (!wholeLine) {
|
|
355
|
-
if (host.box.x < left)
|
|
356
|
-
left = host.box.x;
|
|
357
|
-
if (host.box.x + host.box.w > right)
|
|
358
|
-
right = host.box.x + host.box.w;
|
|
359
|
-
}
|
|
360
|
-
const hostTop = host.box.y - baseContext.getVisualAxis(line, host.track);
|
|
361
|
-
const hostBottom = hostTop + host.box.h;
|
|
362
|
-
includeTrackExtent(extents, host.track, hostTop, hostBottom);
|
|
363
|
-
}
|
|
555
|
+
getContentBounds(content) {
|
|
556
|
+
const boxes = [];
|
|
557
|
+
for (const node of content.nodes) {
|
|
558
|
+
if (isVisualTemporalNode(node) && (node.box.w > 0 || node.box.h > 0))
|
|
559
|
+
boxes.push(node.box);
|
|
364
560
|
}
|
|
365
|
-
for (const
|
|
366
|
-
if (
|
|
561
|
+
for (const attachment of content.attachments) {
|
|
562
|
+
if (!isLayoutAttachment(attachment))
|
|
367
563
|
continue;
|
|
368
|
-
|
|
564
|
+
// 端点越出本域的关系交给外层测量,此时还没有几何
|
|
565
|
+
if (!own.has(attachment) && !measuredElsewhere(attachment))
|
|
566
|
+
continue;
|
|
567
|
+
const box = context.getAttachmentBox(attachment);
|
|
568
|
+
if (box.w > 0 || box.h > 0)
|
|
569
|
+
boxes.push(box);
|
|
369
570
|
}
|
|
370
|
-
|
|
571
|
+
const bounds = { x: 0, y: 0, w: 0, h: 0 };
|
|
572
|
+
return unionLayoutBoxes(bounds, boxes) ? bounds : undefined;
|
|
371
573
|
},
|
|
372
574
|
};
|
|
373
575
|
const items = attachments.map(attachment => {
|
|
@@ -375,23 +577,24 @@ function measureAttachments(attachments, baseContext, lines) {
|
|
|
375
577
|
const box = { x: 0, y: 0, w: 0, h: 0 };
|
|
376
578
|
unionLayoutBoxes(box, geometry.regions);
|
|
377
579
|
const item = { geometry, box };
|
|
378
|
-
measured.set(attachment,
|
|
580
|
+
measured.set(attachment, box);
|
|
379
581
|
// 当前项立即登记,保证后注册的 attachment 能看到并避让它
|
|
380
582
|
for (const region of geometry.occupancy ?? geometry.regions) {
|
|
381
583
|
if (region.line === void 0)
|
|
382
584
|
continue;
|
|
383
585
|
const line = lines[region.line];
|
|
384
|
-
const
|
|
586
|
+
const track = lift(region.track);
|
|
587
|
+
const top = region.y - baseContext.getVisualAxis(region.line, track);
|
|
385
588
|
const bottom = top + region.h;
|
|
386
|
-
const hostExtent = line.hostExtents.get(
|
|
387
|
-
const attachmentExtent = line.attachmentExtents.get(
|
|
589
|
+
const hostExtent = line.hostExtents.get(track);
|
|
590
|
+
const attachmentExtent = line.attachmentExtents.get(track);
|
|
388
591
|
if (top < Math.min(hostExtent?.top ?? Infinity, attachmentExtent?.top ?? Infinity)
|
|
389
592
|
|| bottom > Math.max(hostExtent?.bottom ?? -Infinity, attachmentExtent?.bottom ?? -Infinity)) {
|
|
390
593
|
needsRelayout = true;
|
|
391
594
|
}
|
|
392
|
-
includeTrackExtent(line.attachmentExtents,
|
|
393
|
-
line.
|
|
394
|
-
track
|
|
595
|
+
includeTrackExtent(line.attachmentExtents, track, top, bottom);
|
|
596
|
+
(occupancy[region.line] ??= []).push({
|
|
597
|
+
track,
|
|
395
598
|
left: region.x,
|
|
396
599
|
right: region.x + region.w,
|
|
397
600
|
top,
|
package/dist/layout/page.js
CHANGED
|
@@ -20,6 +20,7 @@ export const DEFAULT_PAGE_CONFIG = {
|
|
|
20
20
|
marginLeft: 40,
|
|
21
21
|
marginRight: 40,
|
|
22
22
|
lineGap: 22,
|
|
23
|
+
fillRatio: 0.5,
|
|
23
24
|
};
|
|
24
25
|
/**
|
|
25
26
|
* 补齐页面配置并保证返回值可直接用于布局
|
|
@@ -34,6 +35,7 @@ export function normalizePageConfig(source = {}) {
|
|
|
34
35
|
marginLeft: Math.max(0, source.marginLeft ?? DEFAULT_PAGE_CONFIG.marginLeft),
|
|
35
36
|
marginRight: Math.max(0, source.marginRight ?? DEFAULT_PAGE_CONFIG.marginRight),
|
|
36
37
|
lineGap: Math.max(0, source.lineGap ?? DEFAULT_PAGE_CONFIG.lineGap),
|
|
38
|
+
fillRatio: source.fillRatio ?? DEFAULT_PAGE_CONFIG.fillRatio,
|
|
37
39
|
};
|
|
38
40
|
if (!Number.isFinite(page.width) || page.width <= 0) {
|
|
39
41
|
throw new Error(`Page width must be a positive finite number, got ${page.width}`);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Track } from "../lowering/track.js";
|
|
2
|
+
import type { HorizontalLineView, LayoutHost, LayoutRange } from "./types.js";
|
|
3
|
+
export type QueryLine = Omit<HorizontalLineView, "registerHorizontalLayoutHook">;
|
|
4
|
+
/** 范围在本域视图中覆盖的闭区间列,start > end 表示空 */
|
|
5
|
+
interface ResolvedRange {
|
|
6
|
+
start: number;
|
|
7
|
+
end: number;
|
|
8
|
+
/** 省略范围即整行,附件占用不再按所选主体的横向范围筛选 */
|
|
9
|
+
wholeLine: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 把轨归入以 root 为根的 Track 树
|
|
13
|
+
*
|
|
14
|
+
* 复合体的私有根轨从它所在的轨分出却不挂进分组,所以沿 Track.parent 上溯总会回到外层的树
|
|
15
|
+
*/
|
|
16
|
+
export declare function liftInto(root: Track): (track: Track) => Track;
|
|
17
|
+
/**
|
|
18
|
+
* 在正文行或子域的视图里解析一次范围查询
|
|
19
|
+
*
|
|
20
|
+
* 数字端点是本视图的列下标;对象端点可以逆序或跨行,按本行截取。
|
|
21
|
+
* 指定 track 时两个端点都必须位于该轨,私有轨先经 lift 归入本域
|
|
22
|
+
*/
|
|
23
|
+
export declare function resolveRange(view: QueryLine, range: LayoutRange | undefined, track: Track | undefined, lift: (track: Track) => Track): ResolvedRange;
|
|
24
|
+
/** 范围内的列;指定 track 时只保留该轨主体,空列照样保留 */
|
|
25
|
+
export declare function rangeColumns(view: QueryLine | undefined, range: LayoutRange | undefined, track: Track | undefined, lift: (track: Track) => Track): readonly (readonly LayoutHost[])[];
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { isVisualTemporalNode, TemporalNodeBase } from "../functions/temporal.js";
|
|
2
|
+
const EMPTY = { start: 0, end: -1, wholeLine: false };
|
|
3
|
+
/**
|
|
4
|
+
* 把轨归入以 root 为根的 Track 树
|
|
5
|
+
*
|
|
6
|
+
* 复合体的私有根轨从它所在的轨分出却不挂进分组,所以沿 Track.parent 上溯总会回到外层的树
|
|
7
|
+
*/
|
|
8
|
+
export function liftInto(root) {
|
|
9
|
+
const tracks = new Set();
|
|
10
|
+
const collect = (track) => {
|
|
11
|
+
tracks.add(track);
|
|
12
|
+
for (const group of track.groups) {
|
|
13
|
+
for (const member of group.members)
|
|
14
|
+
collect(member);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
collect(root);
|
|
18
|
+
return track => {
|
|
19
|
+
for (let lane = track; lane; lane = lane.parent) {
|
|
20
|
+
if (tracks.has(lane))
|
|
21
|
+
return lane;
|
|
22
|
+
}
|
|
23
|
+
return track;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** 本域里代表 host 的列:折叠成员沿 foldedInto 上溯到本域的主体 */
|
|
27
|
+
function columnOf(view, host) {
|
|
28
|
+
for (let node = host; node;) {
|
|
29
|
+
const column = view.columnOf(node);
|
|
30
|
+
if (column >= 0)
|
|
31
|
+
return column;
|
|
32
|
+
const folded = node instanceof TemporalNodeBase ? node.foldedInto : undefined;
|
|
33
|
+
node = folded && isVisualTemporalNode(folded) ? folded : undefined;
|
|
34
|
+
}
|
|
35
|
+
return -1;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 在正文行或子域的视图里解析一次范围查询
|
|
39
|
+
*
|
|
40
|
+
* 数字端点是本视图的列下标;对象端点可以逆序或跨行,按本行截取。
|
|
41
|
+
* 指定 track 时两个端点都必须位于该轨,私有轨先经 lift 归入本域
|
|
42
|
+
*/
|
|
43
|
+
export function resolveRange(view, range, track, lift) {
|
|
44
|
+
if (!range)
|
|
45
|
+
return { start: 0, end: view.columns.length - 1, wholeLine: true };
|
|
46
|
+
if (typeof range[0] === "number") {
|
|
47
|
+
return { start: Math.max(0, range[0]), end: Math.min(view.columns.length - 1, range[1]), wholeLine: false };
|
|
48
|
+
}
|
|
49
|
+
let [from, to] = range;
|
|
50
|
+
if (from.layoutLine > to.layoutLine)
|
|
51
|
+
[from, to] = [to, from];
|
|
52
|
+
if (view.index < from.layoutLine || view.index > to.layoutLine)
|
|
53
|
+
return EMPTY;
|
|
54
|
+
const target = track && lift(track);
|
|
55
|
+
if (target && (lift(from.track) !== target || lift(to.track) !== target)) {
|
|
56
|
+
throw new Error("Range endpoints must belong to the selected track");
|
|
57
|
+
}
|
|
58
|
+
const start = from.layoutLine < view.index ? 0 : columnOf(view, from);
|
|
59
|
+
const end = to.layoutLine > view.index ? view.columns.length - 1 : columnOf(view, to);
|
|
60
|
+
if (start < 0 || end < 0)
|
|
61
|
+
return EMPTY;
|
|
62
|
+
return { start: Math.min(start, end), end: Math.max(start, end), wholeLine: false };
|
|
63
|
+
}
|
|
64
|
+
/** 范围内的列;指定 track 时只保留该轨主体,空列照样保留 */
|
|
65
|
+
export function rangeColumns(view, range, track, lift) {
|
|
66
|
+
if (!view)
|
|
67
|
+
return [];
|
|
68
|
+
const { start, end } = resolveRange(view, range, track, lift);
|
|
69
|
+
const columns = view.columns.slice(start, end + 1);
|
|
70
|
+
if (!track)
|
|
71
|
+
return columns;
|
|
72
|
+
const target = lift(track);
|
|
73
|
+
const onTarget = (host) => host.track === target;
|
|
74
|
+
return columns.every(column => column.every(onTarget)) ? columns : columns.map(column => column.filter(onTarget));
|
|
75
|
+
}
|