jpfun 0.1.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/LICENSE +201 -0
- package/README.md +134 -0
- package/dist/diagnostic.d.ts +27 -0
- package/dist/diagnostic.js +141 -0
- package/dist/functions/ASTtypes.d.ts +115 -0
- package/dist/functions/ASTtypes.js +187 -0
- package/dist/functions/bar/index.d.ts +2 -0
- package/dist/functions/bar/index.js +180 -0
- package/dist/functions/beam/auto.d.ts +4 -0
- package/dist/functions/beam/auto.js +202 -0
- package/dist/functions/beam/index.d.ts +2 -0
- package/dist/functions/beam/index.js +54 -0
- package/dist/functions/beam/layout.d.ts +9 -0
- package/dist/functions/beam/layout.js +233 -0
- package/dist/functions/box/index.d.ts +2 -0
- package/dist/functions/box/index.js +202 -0
- package/dist/functions/br/index.d.ts +2 -0
- package/dist/functions/br/index.js +83 -0
- package/dist/functions/dash/index.d.ts +2 -0
- package/dist/functions/dash/index.js +61 -0
- package/dist/functions/default.d.ts +2 -0
- package/dist/functions/default.js +32 -0
- package/dist/functions/div/index.d.ts +50 -0
- package/dist/functions/div/index.js +190 -0
- package/dist/functions/dot/index.d.ts +2 -0
- package/dist/functions/dot/index.js +160 -0
- package/dist/functions/grace/index.d.ts +84 -0
- package/dist/functions/grace/index.js +464 -0
- package/dist/functions/key/index.d.ts +2 -0
- package/dist/functions/key/index.js +40 -0
- package/dist/functions/note/accidentals.d.ts +10 -0
- package/dist/functions/note/accidentals.js +60 -0
- package/dist/functions/note/index.d.ts +2 -0
- package/dist/functions/note/index.js +284 -0
- package/dist/functions/note/noteNameFSM.d.ts +8 -0
- package/dist/functions/note/noteNameFSM.js +192 -0
- package/dist/functions/over/index.d.ts +2 -0
- package/dist/functions/over/index.js +307 -0
- package/dist/functions/page/index.d.ts +20 -0
- package/dist/functions/page/index.js +63 -0
- package/dist/functions/set/index.d.ts +2 -0
- package/dist/functions/set/index.js +55 -0
- package/dist/functions/stack/index.d.ts +2 -0
- package/dist/functions/stack/index.js +164 -0
- package/dist/functions/tempo/index.d.ts +2 -0
- package/dist/functions/tempo/index.js +37 -0
- package/dist/functions/text/index.d.ts +2 -0
- package/dist/functions/text/index.js +65 -0
- package/dist/functions/tie/index.d.ts +2 -0
- package/dist/functions/tie/index.js +301 -0
- package/dist/functions/voice/index.d.ts +3 -0
- package/dist/functions/voice/index.js +714 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/layout/default.d.ts +12 -0
- package/dist/layout/default.js +27 -0
- package/dist/layout/engine.d.ts +47 -0
- package/dist/layout/engine.js +410 -0
- package/dist/layout/model.d.ts +73 -0
- package/dist/layout/model.js +422 -0
- package/dist/layout/page.d.ts +27 -0
- package/dist/layout/page.js +121 -0
- package/dist/layout/types.d.ts +197 -0
- package/dist/layout/types.js +1 -0
- package/dist/lowering/loweringContext.d.ts +126 -0
- package/dist/lowering/loweringContext.js +625 -0
- package/dist/lowering/track.d.ts +61 -0
- package/dist/lowering/track.js +35 -0
- package/dist/lowering/types.d.ts +176 -0
- package/dist/lowering/types.js +82 -0
- package/dist/parser/grammarType.d.ts +81 -0
- package/dist/parser/grammarType.js +1 -0
- package/dist/parser/parse-utils/brace-utils.d.ts +1 -0
- package/dist/parser/parse-utils/brace-utils.js +29 -0
- package/dist/parser/parse-utils/call-utils.d.ts +24 -0
- package/dist/parser/parse-utils/call-utils.js +178 -0
- package/dist/parser/parse-utils/label-utils.d.ts +4 -0
- package/dist/parser/parse-utils/label-utils.js +17 -0
- package/dist/parser/parse-utils/length-utils.d.ts +3 -0
- package/dist/parser/parse-utils/length-utils.js +20 -0
- package/dist/parser/parse-utils/note-utils.d.ts +10 -0
- package/dist/parser/parse-utils/note-utils.js +111 -0
- package/dist/parser/parserContext.d.ts +93 -0
- package/dist/parser/parserContext.js +542 -0
- package/dist/parser/preprocess.d.ts +6 -0
- package/dist/parser/preprocess.js +193 -0
- package/dist/parser/types.d.ts +31 -0
- package/dist/parser/types.js +2 -0
- package/dist/pipeline.d.ts +36 -0
- package/dist/pipeline.js +51 -0
- package/dist/render/canvas.d.ts +19 -0
- package/dist/render/canvas.js +121 -0
- package/dist/render/index.d.ts +5 -0
- package/dist/render/index.js +5 -0
- package/dist/render/recording.d.ts +46 -0
- package/dist/render/recording.js +76 -0
- package/dist/render/svg.d.ts +18 -0
- package/dist/render/svg.js +126 -0
- package/dist/render/text.d.ts +6 -0
- package/dist/render/text.js +17 -0
- package/dist/render/types.d.ts +86 -0
- package/dist/render/types.js +1 -0
- package/grammar.md +123 -0
- package/package.json +58 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from "./pipeline.js";
|
|
2
|
+
export * from "./functions/ASTtypes.js";
|
|
3
|
+
export * from "./functions/default.js";
|
|
4
|
+
export * from "./diagnostic.js";
|
|
5
|
+
export * from "./parser/types.js";
|
|
6
|
+
export * from "./parser/grammarType.js";
|
|
7
|
+
export * from "./parser/parserContext.js";
|
|
8
|
+
export * from "./parser/preprocess.js";
|
|
9
|
+
export * from "./lowering/types.js";
|
|
10
|
+
export * from "./lowering/loweringContext.js";
|
|
11
|
+
export * from "./lowering/track.js";
|
|
12
|
+
export * from "./layout/types.js";
|
|
13
|
+
export * from "./layout/model.js";
|
|
14
|
+
export * from "./layout/default.js";
|
|
15
|
+
export * from "./layout/page.js";
|
|
16
|
+
export * from "./layout/engine.js";
|
|
17
|
+
export * from "./render/index.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from "./pipeline.js";
|
|
2
|
+
export * from "./functions/ASTtypes.js";
|
|
3
|
+
export * from "./functions/default.js";
|
|
4
|
+
export * from "./diagnostic.js";
|
|
5
|
+
export * from "./parser/types.js";
|
|
6
|
+
export * from "./parser/grammarType.js";
|
|
7
|
+
export * from "./parser/parserContext.js";
|
|
8
|
+
export * from "./parser/preprocess.js";
|
|
9
|
+
export * from "./lowering/types.js";
|
|
10
|
+
export * from "./lowering/loweringContext.js";
|
|
11
|
+
export * from "./lowering/track.js";
|
|
12
|
+
export * from "./layout/types.js";
|
|
13
|
+
export * from "./layout/model.js";
|
|
14
|
+
export * from "./layout/default.js";
|
|
15
|
+
export * from "./layout/page.js";
|
|
16
|
+
export * from "./layout/engine.js";
|
|
17
|
+
export * from "./render/index.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type ASTFunctionClass } from "../functions/ASTtypes.js";
|
|
2
|
+
import type { TextMeasurer } from "../render/types.js";
|
|
3
|
+
import type { LayoutPrepareContext } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* 从函数类声明创建排版上下文
|
|
6
|
+
*
|
|
7
|
+
* 与 LoweringContext.registerFunctions 使用相同的注册思想
|
|
8
|
+
* 新增装饰函数只修改自己的类,不修改 layout 引擎
|
|
9
|
+
*/
|
|
10
|
+
export declare function createLayoutPrepareContext(functionClasses: Iterable<ASTFunctionClass>, options?: {
|
|
11
|
+
textMeasurer?: TextMeasurer;
|
|
12
|
+
}): LayoutPrepareContext;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { functionAddonKey, } from "../functions/ASTtypes.js";
|
|
2
|
+
import { defaultTextMeasurer } from "../render/text.js";
|
|
3
|
+
/**
|
|
4
|
+
* 从函数类声明创建排版上下文
|
|
5
|
+
*
|
|
6
|
+
* 与 LoweringContext.registerFunctions 使用相同的注册思想
|
|
7
|
+
* 新增装饰函数只修改自己的类,不修改 layout 引擎
|
|
8
|
+
*/
|
|
9
|
+
export function createLayoutPrepareContext(functionClasses, options = {}) {
|
|
10
|
+
const handlers = new Map();
|
|
11
|
+
const uniqueClasses = new Set(functionClasses);
|
|
12
|
+
for (const functionClass of uniqueClasses) {
|
|
13
|
+
const staticClass = functionClass;
|
|
14
|
+
const handler = staticClass.layoutDecorationHandler;
|
|
15
|
+
if (!handler)
|
|
16
|
+
continue;
|
|
17
|
+
const names = staticClass.def?.name;
|
|
18
|
+
if (!names)
|
|
19
|
+
continue;
|
|
20
|
+
const primaryName = Array.isArray(names) ? names[0] : names;
|
|
21
|
+
handlers.set(functionAddonKey(primaryName), handler);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
textMeasurer: options.textMeasurer ?? defaultTextMeasurer,
|
|
25
|
+
decorationHandlers: handlers,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Diagnostic } from "../diagnostic.js";
|
|
2
|
+
import { type LoweringResult, type VisualTemporalNode } from "../lowering/types.js";
|
|
3
|
+
import type { Painter } from "../render/types.js";
|
|
4
|
+
import { type SolverOptions } from "./model.js";
|
|
5
|
+
import { type DocumentLayoutPage } from "./page.js";
|
|
6
|
+
import type { LayoutAttachment, LayoutPrepareContext, Rect } from "./types.js";
|
|
7
|
+
export interface DocumentLayoutOptions extends SolverOptions {
|
|
8
|
+
rowGap?: number;
|
|
9
|
+
}
|
|
10
|
+
export { PageLayoutError } from "./page.js";
|
|
11
|
+
export type { DocumentLayoutPage } from "./page.js";
|
|
12
|
+
export interface DocumentLayoutResult {
|
|
13
|
+
diagnostics: Diagnostic[];
|
|
14
|
+
objects: VisualTemporalNode[];
|
|
15
|
+
attachments: LayoutAttachment[];
|
|
16
|
+
bounds: Rect;
|
|
17
|
+
lineCount: number;
|
|
18
|
+
pages: DocumentLayoutPage[];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 把多个已经定位的盒子合并到 target
|
|
22
|
+
*/
|
|
23
|
+
export declare function unionLayoutBoxes(target: Rect, boxes: Iterable<Rect>): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* 把一个 lowering 后的可见事件准备成横向布局可以直接消费的主体
|
|
26
|
+
*
|
|
27
|
+
* 调用前,node 只有已经固化的时间语义、addon 和 LayoutBox 引用;
|
|
28
|
+
* box 的尺寸、命名端口及装饰几何都尚未生成。
|
|
29
|
+
*
|
|
30
|
+
* 执行后,box 的 x/y 仍只是局部初值;固有宽高、弹簧参数、端口和装饰均已对应本轮 context 冻结,可以进入横向求解。
|
|
31
|
+
* ports 和 decorations 在本次 pass 中从空容器开始建立。
|
|
32
|
+
*
|
|
33
|
+
* 具有内部几何的复合节点(例如 up 的和弦成员)可以对自己的子对象调用同一个函数,
|
|
34
|
+
* 从而保证子对象与顶层对象经过完全一致的准备流程。
|
|
35
|
+
*/
|
|
36
|
+
export declare function prepareLayoutHost(node: VisualTemporalNode, context: LayoutPrepareContext): void;
|
|
37
|
+
/**
|
|
38
|
+
* 完成 lowering 结果的全部几何计算
|
|
39
|
+
*/
|
|
40
|
+
export declare function layoutDocument(result: LoweringResult, context: LayoutPrepareContext, options?: DocumentLayoutOptions): DocumentLayoutResult;
|
|
41
|
+
/**
|
|
42
|
+
* 按固定层级绘制已经完成布局的结果
|
|
43
|
+
*
|
|
44
|
+
* background 先于 temporal 对象绘制
|
|
45
|
+
* foreground 用于 tie、beam 等覆盖在线符号之上的对象
|
|
46
|
+
*/
|
|
47
|
+
export declare function paintLayout(result: DocumentLayoutResult, painter: Painter): void;
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import { Diagnostic } from "../diagnostic.js";
|
|
2
|
+
import { isVisualTemporalNode, } from "../lowering/types.js";
|
|
3
|
+
import { completeSpringConfig, layoutElement, layoutHorizontal, } from "./model.js";
|
|
4
|
+
import { normalizePageConfig, paginateLayoutLines, PageLayoutError, } from "./page.js";
|
|
5
|
+
// 兼容旧导入路径;布局入口会把正常页面溢出转换为 ErrorDiagnostic
|
|
6
|
+
export { PageLayoutError } from "./page.js";
|
|
7
|
+
/**
|
|
8
|
+
* 把多个已经定位的盒子合并到 target
|
|
9
|
+
*/
|
|
10
|
+
export function unionLayoutBoxes(target, boxes) {
|
|
11
|
+
let left = Infinity;
|
|
12
|
+
let top = Infinity;
|
|
13
|
+
let right = -Infinity;
|
|
14
|
+
let bottom = -Infinity;
|
|
15
|
+
for (const box of boxes) {
|
|
16
|
+
left = Math.min(left, box.x);
|
|
17
|
+
top = Math.min(top, box.y);
|
|
18
|
+
right = Math.max(right, box.x + box.w);
|
|
19
|
+
bottom = Math.max(bottom, box.y + box.h);
|
|
20
|
+
}
|
|
21
|
+
if (!Number.isFinite(left)) {
|
|
22
|
+
target.x = target.y = target.w = target.h = 0;
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
target.x = left;
|
|
26
|
+
target.y = top;
|
|
27
|
+
target.w = right - left;
|
|
28
|
+
target.h = bottom - top;
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 把一个 lowering 后的可见事件准备成横向布局可以直接消费的主体
|
|
33
|
+
*
|
|
34
|
+
* 调用前,node 只有已经固化的时间语义、addon 和 LayoutBox 引用;
|
|
35
|
+
* box 的尺寸、命名端口及装饰几何都尚未生成。
|
|
36
|
+
*
|
|
37
|
+
* 执行后,box 的 x/y 仍只是局部初值;固有宽高、弹簧参数、端口和装饰均已对应本轮 context 冻结,可以进入横向求解。
|
|
38
|
+
* ports 和 decorations 在本次 pass 中从空容器开始建立。
|
|
39
|
+
*
|
|
40
|
+
* 具有内部几何的复合节点(例如 up 的和弦成员)可以对自己的子对象调用同一个函数,
|
|
41
|
+
* 从而保证子对象与顶层对象经过完全一致的准备流程。
|
|
42
|
+
*/
|
|
43
|
+
export function prepareLayoutHost(node, context) {
|
|
44
|
+
node.springConfig ??= {};
|
|
45
|
+
node.decorations = [];
|
|
46
|
+
node.ports = {};
|
|
47
|
+
node.box.x = node.box.y = 0;
|
|
48
|
+
node.prepareLayout(context); // 计算大小等 layout 需要的参数
|
|
49
|
+
// 把 lowering 固化在 addon 中的语义交给已注册 handler,生成本轮函数装饰
|
|
50
|
+
for (const [key, value] of Object.entries(node.addon ?? {})) {
|
|
51
|
+
const handler = context.decorationHandlers.get(key);
|
|
52
|
+
if (!handler)
|
|
53
|
+
continue;
|
|
54
|
+
const decoration = handler(node, value, context);
|
|
55
|
+
if (decoration)
|
|
56
|
+
node.decorations.push(decoration);
|
|
57
|
+
}
|
|
58
|
+
// 处理 decoration
|
|
59
|
+
arrangeBelowDecorations(node);
|
|
60
|
+
// 让节点在最终盒尺寸确定后发布依赖 box 的端口
|
|
61
|
+
node.finalizeLayout?.(context);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 完成 lowering 结果的全部几何计算
|
|
65
|
+
*/
|
|
66
|
+
export function layoutDocument(result, context, options = {}) {
|
|
67
|
+
const page = normalizePageConfig(result.page);
|
|
68
|
+
const contentWidth = page.width - page.marginLeft - page.marginRight;
|
|
69
|
+
const originX = page.marginLeft;
|
|
70
|
+
// 1. 按 layoutLine 切行,并生成固有尺寸与装饰尺寸
|
|
71
|
+
const lines = splitLayoutLines(result);
|
|
72
|
+
const objects = [];
|
|
73
|
+
for (const line of lines) {
|
|
74
|
+
for (const column of line.columns) {
|
|
75
|
+
for (const node of column) {
|
|
76
|
+
objects.push(node);
|
|
77
|
+
prepareLayoutHost(node, context);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// 2. 横向弹簧布局,得到 box.x
|
|
82
|
+
const views = buildLineViews(lines, options.globalC);
|
|
83
|
+
for (const object of objects)
|
|
84
|
+
object.prepareHorizontal?.(views[object.layoutLine]);
|
|
85
|
+
for (const attachment of result.attachments)
|
|
86
|
+
attachment.prepareHorizontal?.(views);
|
|
87
|
+
for (const line of lines) {
|
|
88
|
+
const elements = line.columns.map(column => column.map(node => layoutElement(node.springConfig, node.box, node, options.globalC)));
|
|
89
|
+
layoutHorizontal(elements, contentWidth, options, line.horizontalLayoutHooks);
|
|
90
|
+
for (const column of line.columns) {
|
|
91
|
+
for (const node of column)
|
|
92
|
+
node.box.x += originX;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// host 申报纵向占用
|
|
96
|
+
// 主体占用只依赖固有尺寸,整个纵向布局中保持不变
|
|
97
|
+
for (const node of objects) {
|
|
98
|
+
const line = lines[node.layoutLine];
|
|
99
|
+
const top = -node.box.visualAxis;
|
|
100
|
+
includeTrackExtent(line.hostExtents, node.track, top, top + node.box.h);
|
|
101
|
+
}
|
|
102
|
+
// 根据配置得到行距
|
|
103
|
+
const rowGaps = measureRowGaps(lines.length, objects, options.rowGap);
|
|
104
|
+
/** 按当前主体与 attachment 占用求轴、分页并放置所有主体 */
|
|
105
|
+
const placeVertically = () => {
|
|
106
|
+
const heights = [];
|
|
107
|
+
const axes = lines.map((line, i) => {
|
|
108
|
+
const solved = solveVerticalAxes(line, result.rootTrack, rowGaps[i]);
|
|
109
|
+
heights.push(solved.height);
|
|
110
|
+
return solved.axes;
|
|
111
|
+
});
|
|
112
|
+
let pages;
|
|
113
|
+
let lineTops;
|
|
114
|
+
try {
|
|
115
|
+
({ pages, lineTops } = paginateLayoutLines(heights, page));
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
if (!(error instanceof PageLayoutError))
|
|
119
|
+
throw error;
|
|
120
|
+
// 用溢出的元素的 span 构成 Error
|
|
121
|
+
const line = lines[error.line];
|
|
122
|
+
let start = Infinity;
|
|
123
|
+
let end = -Infinity;
|
|
124
|
+
for (const column of line?.columns ?? []) {
|
|
125
|
+
for (const node of column) {
|
|
126
|
+
start = Math.min(start, node.ast.sourceSpan.start);
|
|
127
|
+
end = Math.max(end, node.ast.sourceSpan.end);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (Number.isFinite(start)) {
|
|
131
|
+
throw Diagnostic.error.PageOverflow(error.requiredHeight, error.availableHeight, { start, end });
|
|
132
|
+
}
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
const visualAxisOf = (line, track) => (lineTops[line] ?? 0) + (axes[line]?.get(track) ?? 0);
|
|
136
|
+
for (const node of objects) {
|
|
137
|
+
node.box.y = visualAxisOf(node.layoutLine, node.track) - node.box.visualAxis;
|
|
138
|
+
node.onPlaced?.();
|
|
139
|
+
}
|
|
140
|
+
const attachmentContext = {
|
|
141
|
+
...context,
|
|
142
|
+
width: contentWidth,
|
|
143
|
+
originX,
|
|
144
|
+
getVisualAxis: visualAxisOf,
|
|
145
|
+
getHostExtent: (line, track) => lines[line]?.hostExtents.get(track),
|
|
146
|
+
};
|
|
147
|
+
return { pages, attachmentContext };
|
|
148
|
+
};
|
|
149
|
+
// 3. 首次纵向放置后测量 attachment;只有有效轨道占用扩张时才重新求解
|
|
150
|
+
let placement = placeVertically();
|
|
151
|
+
let needsRelayout = false;
|
|
152
|
+
for (const attachment of result.attachments) {
|
|
153
|
+
if (registerAttachmentRegions(attachment, attachment.layout?.(placement.attachmentContext), lines, placement.attachmentContext.getVisualAxis))
|
|
154
|
+
needsRelayout = true;
|
|
155
|
+
}
|
|
156
|
+
if (needsRelayout) {
|
|
157
|
+
placement = placeVertically();
|
|
158
|
+
// 最终区域只更新绘制外接盒,不再累计没有后续消费者的轨道占用
|
|
159
|
+
for (const attachment of result.attachments) {
|
|
160
|
+
unionLayoutBoxes(attachment.box, attachment.layout?.(placement.attachmentContext) ?? []);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const pages = placement.pages;
|
|
164
|
+
const bounds = {
|
|
165
|
+
x: 0, y: 0,
|
|
166
|
+
w: 0, h: 0,
|
|
167
|
+
};
|
|
168
|
+
// 最终画布只使用排版盒,不追踪盒外悬挂图形
|
|
169
|
+
function* layoutBoxes() {
|
|
170
|
+
for (const pageResult of pages)
|
|
171
|
+
yield pageResult.bounds;
|
|
172
|
+
for (const node of objects)
|
|
173
|
+
yield node.box;
|
|
174
|
+
for (const attachment of result.attachments)
|
|
175
|
+
yield attachment.box;
|
|
176
|
+
}
|
|
177
|
+
unionLayoutBoxes(bounds, layoutBoxes());
|
|
178
|
+
return {
|
|
179
|
+
diagnostics: result.diagnostics,
|
|
180
|
+
objects,
|
|
181
|
+
attachments: result.attachments,
|
|
182
|
+
bounds,
|
|
183
|
+
lineCount: lines.length,
|
|
184
|
+
pages,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* 按 below.order 排列主体下方装饰,调用 place 并把其占高计入 box.h
|
|
189
|
+
*/
|
|
190
|
+
function arrangeBelowDecorations(node) {
|
|
191
|
+
// 现代 ECMAScript 的稳定排序会保留相同 order 的注册顺序
|
|
192
|
+
const below = node.decorations
|
|
193
|
+
.map(decoration => decoration.below)
|
|
194
|
+
.filter(item => item !== void 0)
|
|
195
|
+
.sort((left, right) => left.order - right.order);
|
|
196
|
+
if (below.length === 0)
|
|
197
|
+
return;
|
|
198
|
+
// 依次分配每个装饰的局部上边界,并累加最终盒高
|
|
199
|
+
let y = node.box.h;
|
|
200
|
+
for (const item of below) {
|
|
201
|
+
y += item.gap ?? 0;
|
|
202
|
+
item.place?.(y);
|
|
203
|
+
y += Math.max(0, item.height ?? 0);
|
|
204
|
+
}
|
|
205
|
+
node.box.h = y;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* 按 lowering 已固化的 layoutLine 把可见事件拆成谱面行
|
|
209
|
+
*
|
|
210
|
+
* 控制事件不会进入 columns;行号跳跃时保留中间空行,使数组下标始终等于 node.layoutLine
|
|
211
|
+
*/
|
|
212
|
+
function splitLayoutLines(result) {
|
|
213
|
+
const createLine = () => ({
|
|
214
|
+
columns: [],
|
|
215
|
+
horizontalLayoutHooks: [],
|
|
216
|
+
hostExtents: new Map(),
|
|
217
|
+
attachmentExtents: new Map(),
|
|
218
|
+
});
|
|
219
|
+
const lines = [];
|
|
220
|
+
let currentLine = createLine();
|
|
221
|
+
for (const column of result.columns) {
|
|
222
|
+
const visibleColumn = []; // 从列中提取可见元素
|
|
223
|
+
// 空行
|
|
224
|
+
while (lines.length < column[0].layoutLine) {
|
|
225
|
+
lines.push(currentLine);
|
|
226
|
+
currentLine = createLine();
|
|
227
|
+
}
|
|
228
|
+
for (const node of column) {
|
|
229
|
+
if (isVisualTemporalNode(node))
|
|
230
|
+
visibleColumn.push(node);
|
|
231
|
+
}
|
|
232
|
+
if (visibleColumn.length)
|
|
233
|
+
currentLine.columns.push(visibleColumn);
|
|
234
|
+
}
|
|
235
|
+
// 至少要有一行,哪怕它没有任何可见对象
|
|
236
|
+
if (currentLine.columns.length > 0 || lines.length === 0)
|
|
237
|
+
lines.push(currentLine);
|
|
238
|
+
return lines;
|
|
239
|
+
}
|
|
240
|
+
/** 把新的上下界原地并入已有纵向占用范围 */
|
|
241
|
+
function includeExtent(extent, top, bottom) {
|
|
242
|
+
extent.top = Math.min(extent.top, top);
|
|
243
|
+
extent.bottom = Math.max(extent.bottom, bottom);
|
|
244
|
+
}
|
|
245
|
+
function includeTrackExtent(extents, track, top, bottom) {
|
|
246
|
+
const extent = extents.get(track);
|
|
247
|
+
if (extent)
|
|
248
|
+
includeExtent(extent, top, bottom);
|
|
249
|
+
else
|
|
250
|
+
extents.set(track, { top, bottom });
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* 固化每行的横向拓扑:补齐弹簧配置,并生成交给具体函数的只读视图
|
|
254
|
+
*/
|
|
255
|
+
function buildLineViews(lines, force) {
|
|
256
|
+
return lines.map((line, index) => {
|
|
257
|
+
const columnIndex = new Map();
|
|
258
|
+
const trackRuns = new Map();
|
|
259
|
+
for (let i = 0; i < line.columns.length; i++) {
|
|
260
|
+
for (const node of line.columns[i]) {
|
|
261
|
+
columnIndex.set(node, i);
|
|
262
|
+
completeSpringConfig(node.springConfig, force);
|
|
263
|
+
const run = trackRuns.get(node.track);
|
|
264
|
+
if (run)
|
|
265
|
+
run.push(node);
|
|
266
|
+
else
|
|
267
|
+
trackRuns.set(node.track, [node]);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
index,
|
|
272
|
+
trackRuns,
|
|
273
|
+
columnOf: host => columnIndex.get(host) ?? -1,
|
|
274
|
+
registerHorizontalLayoutHook(from, to, hook) {
|
|
275
|
+
const start = columnIndex.get(from);
|
|
276
|
+
const end = columnIndex.get(to);
|
|
277
|
+
if (start === void 0 || end === void 0)
|
|
278
|
+
return;
|
|
279
|
+
line.horizontalLayoutHooks.push({
|
|
280
|
+
start: Math.min(start, end),
|
|
281
|
+
end: Math.max(start, end),
|
|
282
|
+
hook,
|
|
283
|
+
});
|
|
284
|
+
},
|
|
285
|
+
};
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
/** 行距取该行最大字号的 0.75 倍;没有可见对象的行回退到全文档最大字号 */
|
|
289
|
+
function measureRowGaps(lineCount, objects, override) {
|
|
290
|
+
if (override !== void 0)
|
|
291
|
+
return new Array(lineCount).fill(override);
|
|
292
|
+
const gaps = new Array(lineCount).fill(0);
|
|
293
|
+
let documentGap = 0;
|
|
294
|
+
for (const node of objects) {
|
|
295
|
+
const gap = node.ast.size * 0.75;
|
|
296
|
+
if (gap > gaps[node.layoutLine])
|
|
297
|
+
gaps[node.layoutLine] = gap;
|
|
298
|
+
if (gap > documentGap)
|
|
299
|
+
documentGap = gap;
|
|
300
|
+
}
|
|
301
|
+
for (let i = 0; i < lineCount; i++)
|
|
302
|
+
gaps[i] ||= documentGap;
|
|
303
|
+
return gaps;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* 把一个 attachment 首次报出的区域折算成外接盒与轨道占用
|
|
307
|
+
*
|
|
308
|
+
* 只声明了矩形的区域仅影响画布边界;同时声明 line 与 track 才会撑开谱面行。
|
|
309
|
+
* 返回值表示区域是否扩张了主体与先前区域的合并占用,因而需要最终重排。
|
|
310
|
+
*/
|
|
311
|
+
function registerAttachmentRegions(attachment, regions, lines, visualAxisOf) {
|
|
312
|
+
unionLayoutBoxes(attachment.box, regions ?? []);
|
|
313
|
+
let expandedTrackOccupancy = false;
|
|
314
|
+
for (const region of regions ?? []) {
|
|
315
|
+
if (region.line === void 0 || !region.track)
|
|
316
|
+
continue;
|
|
317
|
+
const line = lines[region.line];
|
|
318
|
+
if (!line)
|
|
319
|
+
throw new Error(`Layout attachment referenced invalid line ${region.line}`);
|
|
320
|
+
const axis = visualAxisOf(region.line, region.track);
|
|
321
|
+
const top = region.y - axis;
|
|
322
|
+
const bottom = region.y + region.h - axis;
|
|
323
|
+
const hostExtent = line.hostExtents.get(region.track);
|
|
324
|
+
const attachmentExtent = line.attachmentExtents.get(region.track);
|
|
325
|
+
const previousTop = Math.min(hostExtent?.top ?? Infinity, attachmentExtent?.top ?? Infinity);
|
|
326
|
+
const previousBottom = Math.max(hostExtent?.bottom ?? -Infinity, attachmentExtent?.bottom ?? -Infinity);
|
|
327
|
+
if (top < previousTop || bottom > previousBottom)
|
|
328
|
+
expandedTrackOccupancy = true;
|
|
329
|
+
includeTrackExtent(line.attachmentExtents, region.track, top, bottom);
|
|
330
|
+
}
|
|
331
|
+
return expandedTrackOccupancy;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* 按固定层级绘制已经完成布局的结果
|
|
335
|
+
*
|
|
336
|
+
* background 先于 temporal 对象绘制
|
|
337
|
+
* foreground 用于 tie、beam 等覆盖在线符号之上的对象
|
|
338
|
+
*/
|
|
339
|
+
export function paintLayout(result, painter) {
|
|
340
|
+
for (const attachment of result.attachments) {
|
|
341
|
+
if (attachment.layer === "background")
|
|
342
|
+
attachment.paint(painter);
|
|
343
|
+
}
|
|
344
|
+
for (const node of result.objects) {
|
|
345
|
+
node.paint(painter);
|
|
346
|
+
for (const decoration of node.decorations)
|
|
347
|
+
decoration.paint(painter);
|
|
348
|
+
}
|
|
349
|
+
for (const attachment of result.attachments) {
|
|
350
|
+
if (attachment.layer === "foreground")
|
|
351
|
+
attachment.paint(painter);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* 沿 Track 树自内向外求出一条谱面行里所有音轨的纵向轴
|
|
356
|
+
*
|
|
357
|
+
* 引擎只做三件通用的事:递归求出每个成员的子树高度、调用该分组自己声明的 arrange、
|
|
358
|
+
* 把返回的偏移并回宿主的占用范围。上方叠放还是局部居中完全由 arrange 决定,
|
|
359
|
+
* 因此这里不需要认识 stack、voices 或任何将来新增的排版函数。
|
|
360
|
+
*/
|
|
361
|
+
function solveVerticalAxes(line, rootTrack, gap) {
|
|
362
|
+
// 成员基线相对宿主基线的偏移;只属于当前这一行
|
|
363
|
+
const offsets = new Map();
|
|
364
|
+
/** 返回该音轨连同其全部分支的占用;null 表示本行完全没有内容 */
|
|
365
|
+
const solveTrack = (track) => {
|
|
366
|
+
const hostExtent = line.hostExtents.get(track);
|
|
367
|
+
const attachmentExtent = line.attachmentExtents.get(track);
|
|
368
|
+
// 下面会原地并入分支占用,所以必须复制:hostExtents 在整个纵向布局中不变
|
|
369
|
+
let extent = hostExtent ? { top: hostExtent.top, bottom: hostExtent.bottom } : null;
|
|
370
|
+
if (attachmentExtent) {
|
|
371
|
+
if (extent)
|
|
372
|
+
includeExtent(extent, attachmentExtent.top, attachmentExtent.bottom);
|
|
373
|
+
else
|
|
374
|
+
extent = { top: attachmentExtent.top, bottom: attachmentExtent.bottom };
|
|
375
|
+
}
|
|
376
|
+
for (const group of track.groups) {
|
|
377
|
+
const memberExtents = group.members.map(solveTrack);
|
|
378
|
+
// 整组在本行没有任何内容时不占位,避免共用音轨的分组在空行上浪费高度
|
|
379
|
+
if (memberExtents.every(member => member === null))
|
|
380
|
+
continue;
|
|
381
|
+
const placements = group.arrange(extent ?? { top: 0, bottom: 0 }, memberExtents, gap);
|
|
382
|
+
extent ??= { top: 0, bottom: 0 };
|
|
383
|
+
for (let i = 0; i < group.members.length; i++) {
|
|
384
|
+
const placement = placements[i];
|
|
385
|
+
if (!placement)
|
|
386
|
+
continue;
|
|
387
|
+
offsets.set(group.members[i], placement.offset);
|
|
388
|
+
includeExtent(extent, placement.offset + placement.extent.top, placement.offset + placement.extent.bottom);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return extent;
|
|
392
|
+
};
|
|
393
|
+
const totalExtent = solveTrack(rootTrack);
|
|
394
|
+
const axes = new Map();
|
|
395
|
+
if (!totalExtent)
|
|
396
|
+
return { axes, height: 0 };
|
|
397
|
+
// 行顶归一化到 0,再把偏移沿树传播成本行的相对视觉轴
|
|
398
|
+
const placeTrack = (track, axis) => {
|
|
399
|
+
axes.set(track, axis);
|
|
400
|
+
for (const group of track.groups) {
|
|
401
|
+
for (const member of group.members) {
|
|
402
|
+
const offset = offsets.get(member);
|
|
403
|
+
if (offset !== void 0)
|
|
404
|
+
placeTrack(member, axis + offset);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
placeTrack(rootTrack, -totalExtent.top);
|
|
409
|
+
return { axes, height: totalExtent.bottom - totalExtent.top };
|
|
410
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 基于弹簧的有时长物体排版模型 原理参考 docs/layout.md
|
|
3
|
+
* 基本使用: 借助 layoutElement() 构建 LayoutElement[][],调用 layout() 函数进行排版
|
|
4
|
+
*/
|
|
5
|
+
import type { HorizontalSpringConfig, LayoutBox, TimeLineEvent } from "./types.js";
|
|
6
|
+
type _LayoutBox = Pick<LayoutBox, 'w' | 'x' | 'anchor'>;
|
|
7
|
+
export interface LayoutElement {
|
|
8
|
+
config: Required<HorizontalSpringConfig>;
|
|
9
|
+
box: _LayoutBox;
|
|
10
|
+
time: TimeLineEvent;
|
|
11
|
+
duration_L: number;
|
|
12
|
+
duration_R: number;
|
|
13
|
+
WL: number;
|
|
14
|
+
WR: number;
|
|
15
|
+
margin_L: number;
|
|
16
|
+
margin_R: number;
|
|
17
|
+
fake?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 原地补齐全部弹簧属性
|
|
21
|
+
*
|
|
22
|
+
* 关系修饰在完整配置上只修改自己声明的属性:
|
|
23
|
+
* beam 缩短 alpha 时不会隐式重算 beta,anchor 增大 mu 时也不会影响其他参数。
|
|
24
|
+
*/
|
|
25
|
+
export declare function completeSpringConfig(config: HorizontalSpringConfig, F?: number): asserts config is Required<HorizontalSpringConfig>;
|
|
26
|
+
export declare function layoutElement(config: HorizontalSpringConfig, box: _LayoutBox, time: TimeLineEvent, F?: number): LayoutElement;
|
|
27
|
+
/**
|
|
28
|
+
* 求解器运行参数
|
|
29
|
+
*/
|
|
30
|
+
export interface SolverOptions {
|
|
31
|
+
damping?: number;
|
|
32
|
+
maxIter?: number;
|
|
33
|
+
eps?: number;
|
|
34
|
+
crossPunish?: number;
|
|
35
|
+
globalC?: number;
|
|
36
|
+
}
|
|
37
|
+
export interface HorizontalLayoutHookContext {
|
|
38
|
+
readonly columns: LayoutElement[][];
|
|
39
|
+
readonly rows: number;
|
|
40
|
+
readonly start: number;
|
|
41
|
+
readonly end: number;
|
|
42
|
+
readonly X: Float64Array;
|
|
43
|
+
readonly fixed: Uint8Array;
|
|
44
|
+
readonly options: Required<SolverOptions>;
|
|
45
|
+
}
|
|
46
|
+
export type HorizontalLayoutHook = (context: HorizontalLayoutHookContext) => void;
|
|
47
|
+
export interface HorizontalLayoutHookEntry {
|
|
48
|
+
readonly start: number;
|
|
49
|
+
readonly end: number;
|
|
50
|
+
readonly hook: HorizontalLayoutHook;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 在一段已经归一化的列上排版
|
|
54
|
+
*
|
|
55
|
+
* @param columns 当前区间的归一化列矩阵,列数必须等于 X.length
|
|
56
|
+
* @param rows fillPlaceholders 得到的 Track 数;每列超出 rows 的元素不参与求解
|
|
57
|
+
* @param X 当前区间的 anchor 坐标;通常是全行 X 的 subarray。是求解结果
|
|
58
|
+
* @param fixed 当前区间的 gap 是否已经固定,长度必须为 X.length - 1;通常是全行 fixed 的 subarray。fixed[i]==1 表示第 i、i+1 列只能整体移动
|
|
59
|
+
* @param limit 当前区间左右墙之间的可用宽度
|
|
60
|
+
* @param options 已补齐默认值的求解参数
|
|
61
|
+
* @param fill 空间有余量时,是否让首尾贴墙并把余量平均分到自由间隙
|
|
62
|
+
*/
|
|
63
|
+
export declare function layoutHorizontalRegion(columns: LayoutElement[][], rows: number, X: Float64Array, fixed: Uint8Array, limit: number, options: Required<SolverOptions>, fill?: boolean): void;
|
|
64
|
+
/**
|
|
65
|
+
* 纯扁平化物理排版解算入口
|
|
66
|
+
*
|
|
67
|
+
* @param columns 二维数组,第一维为列,第二维为行,元素包含物理属性和状态属性
|
|
68
|
+
* @param limit 宽度约束
|
|
69
|
+
* @param options 排版物理配置
|
|
70
|
+
* @returns 同一个解算完成的二维矩阵,所有盒子的绝对 box.x (左侧x坐标) 已就地更新
|
|
71
|
+
*/
|
|
72
|
+
export declare function layoutHorizontal(columns: LayoutElement[][], limit: number, options?: SolverOptions, horizontalLayoutHooks?: readonly HorizontalLayoutHookEntry[]): LayoutElement[][];
|
|
73
|
+
export {};
|