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
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
const DEFAULT_F = 1.0; // 多大力让一行的 margin 全变为 0
|
|
2
|
+
const DEFAULT_ALPHA = 6; // 控制无约束时元素的margin
|
|
3
|
+
const DEFAULT_MU = 16;
|
|
4
|
+
const DEFAULT_CROSS_PUNISH = 32;
|
|
5
|
+
const MIN_DURATION = 0.01;
|
|
6
|
+
/**
|
|
7
|
+
* 原地补齐全部弹簧属性
|
|
8
|
+
*
|
|
9
|
+
* 关系修饰在完整配置上只修改自己声明的属性:
|
|
10
|
+
* beam 缩短 alpha 时不会隐式重算 beta,anchor 增大 mu 时也不会影响其他参数。
|
|
11
|
+
*/
|
|
12
|
+
export function completeSpringConfig(config, F = DEFAULT_F) {
|
|
13
|
+
const defaultAlpha = config.alpha_L ?? config.alpha_R ?? DEFAULT_ALPHA;
|
|
14
|
+
config.alpha_L ??= defaultAlpha;
|
|
15
|
+
config.alpha_R ??= defaultAlpha;
|
|
16
|
+
const defaultMu = config.mu_L ?? config.mu_R ?? DEFAULT_MU;
|
|
17
|
+
config.mu_L ??= defaultMu;
|
|
18
|
+
config.mu_R ??= defaultMu;
|
|
19
|
+
config.beta_L ??= F / config.alpha_L;
|
|
20
|
+
config.beta_R ??= F / config.alpha_R;
|
|
21
|
+
}
|
|
22
|
+
// 有副作用:确保直接调用时也能消费尚未补齐的配置
|
|
23
|
+
export function layoutElement(config, box, time, F = DEFAULT_F) {
|
|
24
|
+
completeSpringConfig(config, F);
|
|
25
|
+
// 最小时长限制,与非线性变换(美观)
|
|
26
|
+
const duration = Math.pow(Math.max(time.T, MIN_DURATION), 0.5);
|
|
27
|
+
return {
|
|
28
|
+
config, box, time,
|
|
29
|
+
duration_L: duration,
|
|
30
|
+
duration_R: duration,
|
|
31
|
+
WL: box.anchor,
|
|
32
|
+
WR: box.w - box.anchor,
|
|
33
|
+
margin_L: duration * config.alpha_L,
|
|
34
|
+
margin_R: duration * config.alpha_R,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 补齐列中缺损声部元素的占位工具函数
|
|
39
|
+
* @param columns: 第一维度为列
|
|
40
|
+
*/
|
|
41
|
+
function fillPlaceholders(columns, F = DEFAULT_F) {
|
|
42
|
+
const idOrderMap = new Map();
|
|
43
|
+
let rows = 0;
|
|
44
|
+
for (const col of columns) {
|
|
45
|
+
for (const el of col) {
|
|
46
|
+
// undefined 和 null 都算有效的 id
|
|
47
|
+
if (idOrderMap.has(el.time.track))
|
|
48
|
+
continue;
|
|
49
|
+
idOrderMap.set(el.time.track, rows++);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const mat = [];
|
|
53
|
+
for (const col of columns) {
|
|
54
|
+
if (col.length === 0)
|
|
55
|
+
continue;
|
|
56
|
+
const sortedCol = Array(col.length);
|
|
57
|
+
mat.push(sortedCol);
|
|
58
|
+
let maxLeft = 0;
|
|
59
|
+
let maxRight = 0;
|
|
60
|
+
let same = rows;
|
|
61
|
+
for (const el of col) {
|
|
62
|
+
if (el.WL > maxLeft)
|
|
63
|
+
maxLeft = el.WL;
|
|
64
|
+
if (el.WR > maxRight)
|
|
65
|
+
maxRight = el.WR;
|
|
66
|
+
const rIdx = idOrderMap.get(el.time.track);
|
|
67
|
+
if (sortedCol[rIdx]) {
|
|
68
|
+
// 出现了同一行同一时刻的多个元素,则优先时长更大的
|
|
69
|
+
// 重复的元素放到最后,不参与后续的元素比较
|
|
70
|
+
const elExist = sortedCol[rIdx];
|
|
71
|
+
if (el.time.T > elExist.time.T) {
|
|
72
|
+
sortedCol[rIdx] = el;
|
|
73
|
+
sortedCol[same++] = elExist;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
sortedCol[same++] = el;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else
|
|
80
|
+
sortedCol[rIdx] = el;
|
|
81
|
+
}
|
|
82
|
+
// 填充不足的部分
|
|
83
|
+
const colTime = col[0].time.t;
|
|
84
|
+
for (const rowId of idOrderMap) {
|
|
85
|
+
const rIdx = rowId[1];
|
|
86
|
+
if (!sortedCol[rIdx]) {
|
|
87
|
+
// 保证 margin_L = mexLeft, margin_R = maxRight, 实际宽度为0
|
|
88
|
+
const elPlaceholder = layoutElement({
|
|
89
|
+
anchor: 0,
|
|
90
|
+
alpha_L: maxLeft,
|
|
91
|
+
alpha_R: maxRight,
|
|
92
|
+
}, {
|
|
93
|
+
x: 0, w: 0, anchor: 0
|
|
94
|
+
}, {
|
|
95
|
+
t: colTime, T: 1, track: rowId[0]
|
|
96
|
+
}, F);
|
|
97
|
+
elPlaceholder.fake = true;
|
|
98
|
+
sortedCol[rIdx] = elPlaceholder;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return { mat, rows };
|
|
103
|
+
}
|
|
104
|
+
// 串联弹簧力学解析
|
|
105
|
+
function calcPairForceAndStiffness(el, er, xl, xr) {
|
|
106
|
+
const dis_eff = (xr - xl) - el.WR - er.WL;
|
|
107
|
+
const m0_total = el.margin_R + er.margin_L;
|
|
108
|
+
if (dis_eff >= m0_total)
|
|
109
|
+
return { force: 0, stiffness: 0 };
|
|
110
|
+
// 计算等效弹簧参数
|
|
111
|
+
const k_R1 = el.config.beta_R / el.duration_R;
|
|
112
|
+
const k_L2 = er.config.beta_L / er.duration_L;
|
|
113
|
+
const K_normal = (k_R1 * k_L2) / (k_R1 + k_L2);
|
|
114
|
+
let force, stiffness;
|
|
115
|
+
if (dis_eff < 0) {
|
|
116
|
+
const K_overlap = (el.config.mu_R * er.config.mu_L) / (el.config.mu_R + er.config.mu_L);
|
|
117
|
+
force = K_normal * m0_total - K_overlap * dis_eff; // 重合前的最大力+重合后的线性增长力
|
|
118
|
+
stiffness = K_overlap;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const dl = m0_total - dis_eff;
|
|
122
|
+
force = K_normal * dl;
|
|
123
|
+
stiffness = K_normal;
|
|
124
|
+
}
|
|
125
|
+
return { force, stiffness };
|
|
126
|
+
}
|
|
127
|
+
function calcLeftWall(e, x, crossPunish = DEFAULT_CROSS_PUNISH) {
|
|
128
|
+
const dis = x - e.WL;
|
|
129
|
+
const m0 = e.margin_L;
|
|
130
|
+
if (dis >= m0)
|
|
131
|
+
return { force: 0, stiffness: 0 };
|
|
132
|
+
const k = e.config.beta_L / e.duration_L;
|
|
133
|
+
if (dis >= 0)
|
|
134
|
+
return { force: k * (m0 - dis), stiffness: k };
|
|
135
|
+
const stiffness = e.config.mu_L * crossPunish;
|
|
136
|
+
const force = e.config.alpha_L * e.config.beta_L - stiffness * dis;
|
|
137
|
+
return { force, stiffness };
|
|
138
|
+
}
|
|
139
|
+
function calcRightWall(e, x, limit, crossPunish = DEFAULT_CROSS_PUNISH) {
|
|
140
|
+
const dis = limit - (x + e.WR);
|
|
141
|
+
const m0 = e.margin_R;
|
|
142
|
+
if (dis >= m0)
|
|
143
|
+
return { force: 0, stiffness: 0 };
|
|
144
|
+
const k = e.config.beta_R / e.duration_R;
|
|
145
|
+
if (dis >= 0)
|
|
146
|
+
return { force: k * (m0 - dis), stiffness: k };
|
|
147
|
+
const stiffness = e.config.mu_R * crossPunish;
|
|
148
|
+
const force = e.config.alpha_R * e.config.beta_R - stiffness * dis;
|
|
149
|
+
return { force, stiffness };
|
|
150
|
+
}
|
|
151
|
+
function normalizeSolverOptions(options) {
|
|
152
|
+
return {
|
|
153
|
+
damping: options.damping ?? 0.6,
|
|
154
|
+
maxIter: options.maxIter ?? 100,
|
|
155
|
+
eps: options.eps ?? 1e-2,
|
|
156
|
+
crossPunish: options.crossPunish ?? DEFAULT_CROSS_PUNISH,
|
|
157
|
+
globalC: options.globalC ?? DEFAULT_F,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/** 不处理 fixed 和写回,只求一组普通列的 anchor 坐标;返回自然布局是否有余量 */
|
|
161
|
+
function solveHorizontal(mat, rows, X, limit, options) {
|
|
162
|
+
const { damping, maxIter, eps, crossPunish } = options;
|
|
163
|
+
const numCols = mat.length;
|
|
164
|
+
// if (numCols === 0 || rows === 0) return false; layoutHorizontalRegion 已经判断过了
|
|
165
|
+
// 预排列(仅用前 rows 个元素建立无约束时的位置)
|
|
166
|
+
const row_x = new Float64Array(rows);
|
|
167
|
+
for (let c = 0; c < numCols; c++) {
|
|
168
|
+
let x = 0;
|
|
169
|
+
const col = mat[c];
|
|
170
|
+
for (let r = 0; r < rows; r++) {
|
|
171
|
+
const required_x = row_x[r] + col[r].WL + col[r].margin_L;
|
|
172
|
+
if (required_x > x)
|
|
173
|
+
x = required_x;
|
|
174
|
+
}
|
|
175
|
+
for (let r = 0; r < rows; r++) {
|
|
176
|
+
row_x[r] = x + col[r].WR + col[r].margin_R;
|
|
177
|
+
}
|
|
178
|
+
X[c] = x;
|
|
179
|
+
}
|
|
180
|
+
let x0 = row_x.reduce((a, b) => Math.max(a, b), 0); // 预排列后的总宽度
|
|
181
|
+
if (x0 <= limit)
|
|
182
|
+
return true; // 空间充足,无需迭代
|
|
183
|
+
// 如果预排列就已经超出限制了,则直接等比压缩到限制范围内
|
|
184
|
+
x0 = limit / x0;
|
|
185
|
+
for (let c = 0; c < numCols; c++)
|
|
186
|
+
X[c] *= x0;
|
|
187
|
+
// --- 预分配物理引擎所需的全部静态缓存空间,杜绝主循环内的 GC 损耗 ---
|
|
188
|
+
const F_vec = new Float64Array(numCols);
|
|
189
|
+
const stiffnessCache = new Float32Array(rows * (numCols + 1));
|
|
190
|
+
// 计算全局合力,并就地填充刚度缓存
|
|
191
|
+
function computeForces(cols_X) {
|
|
192
|
+
F_vec.fill(0);
|
|
193
|
+
for (let r = 0; r < rows; r++) {
|
|
194
|
+
const rowOffset = r * (numCols + 1);
|
|
195
|
+
// 左墙
|
|
196
|
+
const el_first = mat[0][r];
|
|
197
|
+
const res_left = calcLeftWall(el_first, cols_X[0], crossPunish);
|
|
198
|
+
F_vec[0] += res_left.force;
|
|
199
|
+
stiffnessCache[rowOffset] = res_left.stiffness; // 缓存左墙刚度
|
|
200
|
+
// 内部链弹簧力传导
|
|
201
|
+
for (let c = 1; c < numCols; c++) {
|
|
202
|
+
const el_l = mat[c - 1][r];
|
|
203
|
+
const el_r = mat[c][r];
|
|
204
|
+
const res = calcPairForceAndStiffness(el_l, el_r, cols_X[c - 1], cols_X[c]);
|
|
205
|
+
F_vec[c - 1] -= res.force;
|
|
206
|
+
F_vec[c] += res.force;
|
|
207
|
+
stiffnessCache[rowOffset + c] = res.stiffness; // 缓存链弹簧刚度
|
|
208
|
+
}
|
|
209
|
+
// 右墙
|
|
210
|
+
const el_last = mat[numCols - 1][r];
|
|
211
|
+
const res_right = calcRightWall(el_last, cols_X[numCols - 1], limit, crossPunish);
|
|
212
|
+
F_vec[numCols - 1] -= res_right.force;
|
|
213
|
+
stiffnessCache[rowOffset + numCols] = res_right.stiffness; // 缓存右墙刚度
|
|
214
|
+
}
|
|
215
|
+
return F_vec;
|
|
216
|
+
}
|
|
217
|
+
const Hp = new Float64Array(numCols);
|
|
218
|
+
// Hessian 矩阵切向刚度乘以位移向量 p (精简参数,移除了无用的 cols_X)
|
|
219
|
+
function multiplyH(p) {
|
|
220
|
+
Hp.fill(0);
|
|
221
|
+
for (let r = 0, rowOffset = 0; r < rows; r++, rowOffset += (numCols + 1)) {
|
|
222
|
+
// 左墙刚度
|
|
223
|
+
Hp[0] += stiffnessCache[rowOffset] * p[0];
|
|
224
|
+
// 链弹簧二阶切线刚度
|
|
225
|
+
for (let c = 1; c < numCols; c++) {
|
|
226
|
+
const Kdp = stiffnessCache[rowOffset + c] * (p[c] - p[c - 1]);
|
|
227
|
+
Hp[c - 1] -= Kdp;
|
|
228
|
+
Hp[c] += Kdp;
|
|
229
|
+
}
|
|
230
|
+
// 右墙刚度
|
|
231
|
+
Hp[numCols - 1] += stiffnessCache[rowOffset + numCols] * p[numCols - 1];
|
|
232
|
+
}
|
|
233
|
+
// L-M 正则阻尼,保证矩阵严格正定,避免奇异性
|
|
234
|
+
const lambda = 1e-4;
|
|
235
|
+
for (let i = 0; i < p.length; i++)
|
|
236
|
+
Hp[i] += lambda * p[i];
|
|
237
|
+
return Hp;
|
|
238
|
+
}
|
|
239
|
+
function dot(a, b) {
|
|
240
|
+
let sum = 0;
|
|
241
|
+
for (let i = 0; i < a.length; i++)
|
|
242
|
+
sum += a[i] * b[i];
|
|
243
|
+
return sum;
|
|
244
|
+
}
|
|
245
|
+
// 必须用 Float64Array 不然影响收敛
|
|
246
|
+
const dx = new Float64Array(numCols);
|
|
247
|
+
const R = new Float64Array(numCols);
|
|
248
|
+
const P = new Float64Array(numCols);
|
|
249
|
+
// 共轭梯度线性解算
|
|
250
|
+
function solveCG(F_vec, maxCGIter = 8) {
|
|
251
|
+
dx.fill(0);
|
|
252
|
+
R.set(F_vec);
|
|
253
|
+
P.set(R);
|
|
254
|
+
let rsold = dot(R, R);
|
|
255
|
+
if (rsold < 1e-12)
|
|
256
|
+
return dx;
|
|
257
|
+
for (let iter = 0; iter < maxCGIter; iter++) {
|
|
258
|
+
const Hp = multiplyH(P);
|
|
259
|
+
const p_Hp = dot(P, Hp);
|
|
260
|
+
if (Math.abs(p_Hp) < 1e-12)
|
|
261
|
+
break;
|
|
262
|
+
const alpha = rsold / p_Hp;
|
|
263
|
+
for (let i = 0; i < numCols; i++) {
|
|
264
|
+
dx[i] += alpha * P[i];
|
|
265
|
+
R[i] -= alpha * Hp[i];
|
|
266
|
+
}
|
|
267
|
+
const rsnew = dot(R, R);
|
|
268
|
+
if (Math.sqrt(rsnew) < 1e-6)
|
|
269
|
+
break;
|
|
270
|
+
const beta = rsnew / rsold;
|
|
271
|
+
for (let i = 0; i < numCols; i++) {
|
|
272
|
+
P[i] = R[i] + beta * P[i];
|
|
273
|
+
}
|
|
274
|
+
rsold = rsnew;
|
|
275
|
+
}
|
|
276
|
+
return dx;
|
|
277
|
+
}
|
|
278
|
+
// 主物理迭代牛顿步
|
|
279
|
+
let turn = 0, f_max = 0;
|
|
280
|
+
for (; turn < maxIter; turn++) {
|
|
281
|
+
const F_vec = computeForces(X);
|
|
282
|
+
f_max = 0;
|
|
283
|
+
for (let i = 0; i < F_vec.length; i++) {
|
|
284
|
+
const absf = Math.abs(F_vec[i]);
|
|
285
|
+
if (absf > f_max)
|
|
286
|
+
f_max = absf;
|
|
287
|
+
}
|
|
288
|
+
if (f_max < eps)
|
|
289
|
+
break;
|
|
290
|
+
const dx = solveCG(F_vec);
|
|
291
|
+
for (let i = 0; i < numCols; i++) {
|
|
292
|
+
X[i] += damping * dx[i];
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* 在一段已经归一化的列上排版
|
|
299
|
+
*
|
|
300
|
+
* @param columns 当前区间的归一化列矩阵,列数必须等于 X.length
|
|
301
|
+
* @param rows fillPlaceholders 得到的 Track 数;每列超出 rows 的元素不参与求解
|
|
302
|
+
* @param X 当前区间的 anchor 坐标;通常是全行 X 的 subarray。是求解结果
|
|
303
|
+
* @param fixed 当前区间的 gap 是否已经固定,长度必须为 X.length - 1;通常是全行 fixed 的 subarray。fixed[i]==1 表示第 i、i+1 列只能整体移动
|
|
304
|
+
* @param limit 当前区间左右墙之间的可用宽度
|
|
305
|
+
* @param options 已补齐默认值的求解参数
|
|
306
|
+
* @param fill 空间有余量时,是否让首尾贴墙并把余量平均分到自由间隙
|
|
307
|
+
*/
|
|
308
|
+
export function layoutHorizontalRegion(columns, rows, X, fixed, limit, options, fill = false) {
|
|
309
|
+
if (X.length !== columns.length || fixed.length !== Math.max(0, X.length - 1)) {
|
|
310
|
+
throw new Error("Horizontal region state does not match its columns");
|
|
311
|
+
}
|
|
312
|
+
if (X.length === 0 || rows === 0)
|
|
313
|
+
return;
|
|
314
|
+
const hasFixed = fixed.some(value => value !== 0);
|
|
315
|
+
let groupOf, offsets;
|
|
316
|
+
let solvedColumns = columns;
|
|
317
|
+
let solvedX = X;
|
|
318
|
+
if (hasFixed) { // 先把连续的固定列合并为虚拟列,求解后再拆开
|
|
319
|
+
groupOf = new Uint32Array(X.length); // 记录每个列属于哪一组
|
|
320
|
+
offsets = new Float64Array(X.length); // 记录每个列相对于组首的偏移量
|
|
321
|
+
solvedColumns = [];
|
|
322
|
+
for (let start = 0; start < X.length;) {
|
|
323
|
+
// 找连续的固定列区间
|
|
324
|
+
let end = start;
|
|
325
|
+
while (end < fixed.length && fixed[end])
|
|
326
|
+
end++;
|
|
327
|
+
const groupIndex = solvedColumns.length;
|
|
328
|
+
for (let i = start; i <= end; i++) {
|
|
329
|
+
groupOf[i] = groupIndex;
|
|
330
|
+
offsets[i] = X[i] - X[start];
|
|
331
|
+
}
|
|
332
|
+
if (start === end)
|
|
333
|
+
solvedColumns.push(columns[start]);
|
|
334
|
+
else {
|
|
335
|
+
const span = X[end] - X[start];
|
|
336
|
+
// 合并为虚拟的一列
|
|
337
|
+
solvedColumns.push(Array.from({ length: rows }, (_, row) => {
|
|
338
|
+
const left = columns[start][row];
|
|
339
|
+
const right = columns[end][row];
|
|
340
|
+
return {
|
|
341
|
+
config: {
|
|
342
|
+
alpha_L: left.config.alpha_L,
|
|
343
|
+
alpha_R: right.config.alpha_R,
|
|
344
|
+
mu_L: left.config.mu_L,
|
|
345
|
+
mu_R: right.config.mu_R,
|
|
346
|
+
beta_L: left.config.beta_L,
|
|
347
|
+
beta_R: right.config.beta_R,
|
|
348
|
+
},
|
|
349
|
+
box: left.box, // 虚拟元素不写回,仅用于满足 LayoutElement,算法不会读取
|
|
350
|
+
time: left.time,
|
|
351
|
+
duration_L: left.duration_L,
|
|
352
|
+
duration_R: right.duration_R,
|
|
353
|
+
WL: left.WL,
|
|
354
|
+
WR: span + right.WR,
|
|
355
|
+
margin_L: left.margin_L,
|
|
356
|
+
margin_R: right.margin_R,
|
|
357
|
+
fake: left.fake && right.fake,
|
|
358
|
+
};
|
|
359
|
+
}));
|
|
360
|
+
}
|
|
361
|
+
start = end + 1;
|
|
362
|
+
}
|
|
363
|
+
solvedX = new Float64Array(solvedColumns.length);
|
|
364
|
+
}
|
|
365
|
+
const hasRoom = solveHorizontal(solvedColumns, rows, solvedX, limit, options);
|
|
366
|
+
fixed.fill(1);
|
|
367
|
+
if (fill && hasRoom) {
|
|
368
|
+
// 让首尾贴墙,并把余量平均分到自由间隙
|
|
369
|
+
let leftWidth = 0;
|
|
370
|
+
let rightWidth = 0;
|
|
371
|
+
for (let row = 0; row < rows; row++) {
|
|
372
|
+
leftWidth = Math.max(leftWidth, solvedColumns[0][row].WL);
|
|
373
|
+
rightWidth = Math.max(rightWidth, solvedColumns[solvedColumns.length - 1][row].WR);
|
|
374
|
+
}
|
|
375
|
+
const shift = leftWidth - solvedX[0];
|
|
376
|
+
const extra = limit - rightWidth - solvedX[solvedX.length - 1] - shift;
|
|
377
|
+
for (let i = 0; i < solvedX.length; i++) {
|
|
378
|
+
solvedX[i] += shift + (extra > 0 && solvedX.length > 1
|
|
379
|
+
? extra * i / (solvedX.length - 1)
|
|
380
|
+
: 0);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (hasFixed) { // 把虚拟列的求解结果写回原始列
|
|
384
|
+
for (let i = 0; i < X.length; i++)
|
|
385
|
+
X[i] = solvedX[groupOf[i]] + offsets[i];
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* 纯扁平化物理排版解算入口
|
|
390
|
+
*
|
|
391
|
+
* @param columns 二维数组,第一维为列,第二维为行,元素包含物理属性和状态属性
|
|
392
|
+
* @param limit 宽度约束
|
|
393
|
+
* @param options 排版物理配置
|
|
394
|
+
* @returns 同一个解算完成的二维矩阵,所有盒子的绝对 box.x (左侧x坐标) 已就地更新
|
|
395
|
+
*/
|
|
396
|
+
export function layoutHorizontal(columns, limit, options = {}, horizontalLayoutHooks = []) {
|
|
397
|
+
const normalizedOptions = normalizeSolverOptions(options);
|
|
398
|
+
const { mat, rows } = fillPlaceholders(columns, normalizedOptions.globalC);
|
|
399
|
+
const X = new Float64Array(mat.length);
|
|
400
|
+
const fixed = new Uint8Array(Math.max(0, mat.length - 1));
|
|
401
|
+
const ordered = horizontalLayoutHooks.length > 1
|
|
402
|
+
? [...horizontalLayoutHooks].sort((a, b) => (a.end - a.start) - (b.end - b.start))
|
|
403
|
+
: horizontalLayoutHooks;
|
|
404
|
+
for (const { start, end, hook } of ordered) {
|
|
405
|
+
if (start < 0 || end < start || end >= mat.length) {
|
|
406
|
+
throw new Error("Horizontal layout registration references invalid columns");
|
|
407
|
+
}
|
|
408
|
+
hook({
|
|
409
|
+
columns: mat, rows,
|
|
410
|
+
start, end,
|
|
411
|
+
X, fixed,
|
|
412
|
+
options: normalizedOptions,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
layoutHorizontalRegion(mat, rows, X, fixed, limit, normalizedOptions);
|
|
416
|
+
// 坐标写回
|
|
417
|
+
for (let c = 0; c < mat.length; c++) {
|
|
418
|
+
for (const el of mat[c])
|
|
419
|
+
el.box.x = X[c] - el.box.anchor;
|
|
420
|
+
}
|
|
421
|
+
return mat;
|
|
422
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Rect, PageConfig } from "./types.js";
|
|
2
|
+
/** 全局坐标中的页面边界与所含谱面行范围 */
|
|
3
|
+
export interface DocumentLayoutPage {
|
|
4
|
+
bounds: Rect;
|
|
5
|
+
lineStart: number;
|
|
6
|
+
lineEnd: number;
|
|
7
|
+
}
|
|
8
|
+
/** 单条谱面行高于页面内容区时传给 engine 的结构化信号 */
|
|
9
|
+
export declare class PageLayoutError extends Error {
|
|
10
|
+
readonly line: number;
|
|
11
|
+
readonly requiredHeight: number;
|
|
12
|
+
readonly availableHeight: number;
|
|
13
|
+
constructor(line: number, requiredHeight: number, availableHeight: number);
|
|
14
|
+
}
|
|
15
|
+
/** 没有 page 函数时使用的默认 A4 近似页面配置 */
|
|
16
|
+
export declare const DEFAULT_PAGE_CONFIG: Readonly<PageConfig>;
|
|
17
|
+
/**
|
|
18
|
+
* 补齐页面配置并保证返回值可直接用于布局
|
|
19
|
+
*/
|
|
20
|
+
export declare function normalizePageConfig(source?: Partial<PageConfig>): PageConfig;
|
|
21
|
+
/**
|
|
22
|
+
* 把已经测好自然高度的谱面行分页,并给出每行的全局顶部坐标
|
|
23
|
+
*/
|
|
24
|
+
export declare function paginateLayoutLines(heights: readonly number[], page: PageConfig): {
|
|
25
|
+
pages: DocumentLayoutPage[];
|
|
26
|
+
lineTops: number[];
|
|
27
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/** 单条谱面行高于页面内容区时传给 engine 的结构化信号 */
|
|
2
|
+
export class PageLayoutError extends Error {
|
|
3
|
+
line;
|
|
4
|
+
requiredHeight;
|
|
5
|
+
availableHeight;
|
|
6
|
+
constructor(line, requiredHeight, availableHeight) {
|
|
7
|
+
super(`Page cannot fit layout line ${line}: requires ${requiredHeight}px, available ${availableHeight}px`);
|
|
8
|
+
this.name = "PageLayoutError";
|
|
9
|
+
this.line = line;
|
|
10
|
+
this.requiredHeight = requiredHeight;
|
|
11
|
+
this.availableHeight = availableHeight;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** 没有 page 函数时使用的默认 A4 近似页面配置 */
|
|
15
|
+
export const DEFAULT_PAGE_CONFIG = {
|
|
16
|
+
width: 794,
|
|
17
|
+
height: Infinity,
|
|
18
|
+
marginTop: 45,
|
|
19
|
+
marginBottom: 48,
|
|
20
|
+
marginLeft: 40,
|
|
21
|
+
marginRight: 40,
|
|
22
|
+
lineGap: 22,
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* 补齐页面配置并保证返回值可直接用于布局
|
|
26
|
+
*/
|
|
27
|
+
export function normalizePageConfig(source = {}) {
|
|
28
|
+
const height = source.height ?? DEFAULT_PAGE_CONFIG.height;
|
|
29
|
+
const page = {
|
|
30
|
+
width: source.width ?? DEFAULT_PAGE_CONFIG.width,
|
|
31
|
+
height: height <= 0 ? Infinity : height,
|
|
32
|
+
marginTop: Math.max(0, source.marginTop ?? DEFAULT_PAGE_CONFIG.marginTop),
|
|
33
|
+
marginBottom: Math.max(0, source.marginBottom ?? DEFAULT_PAGE_CONFIG.marginBottom),
|
|
34
|
+
marginLeft: Math.max(0, source.marginLeft ?? DEFAULT_PAGE_CONFIG.marginLeft),
|
|
35
|
+
marginRight: Math.max(0, source.marginRight ?? DEFAULT_PAGE_CONFIG.marginRight),
|
|
36
|
+
lineGap: Math.max(0, source.lineGap ?? DEFAULT_PAGE_CONFIG.lineGap),
|
|
37
|
+
};
|
|
38
|
+
if (!Number.isFinite(page.width) || page.width <= 0) {
|
|
39
|
+
throw new Error(`Page width must be a positive finite number, got ${page.width}`);
|
|
40
|
+
}
|
|
41
|
+
if (page.height !== Infinity && (!Number.isFinite(page.height) || page.height <= 0)) {
|
|
42
|
+
throw new Error(`Page height must be positive or Infinity, got ${page.height}`);
|
|
43
|
+
}
|
|
44
|
+
const values = [
|
|
45
|
+
page.marginTop,
|
|
46
|
+
page.marginBottom,
|
|
47
|
+
page.marginLeft,
|
|
48
|
+
page.marginRight,
|
|
49
|
+
page.lineGap,
|
|
50
|
+
];
|
|
51
|
+
if (values.some(value => !Number.isFinite(value) || value < 0)) {
|
|
52
|
+
throw new Error("Page margins and line gap must be finite non-negative numbers");
|
|
53
|
+
}
|
|
54
|
+
const contentWidth = page.width - page.marginLeft - page.marginRight;
|
|
55
|
+
if (contentWidth <= 0) {
|
|
56
|
+
throw new Error(`Page horizontal margins leave no content width: ${contentWidth}px`);
|
|
57
|
+
}
|
|
58
|
+
const contentHeight = page.height - page.marginTop - page.marginBottom;
|
|
59
|
+
if (Number.isFinite(page.height) && contentHeight <= 0) {
|
|
60
|
+
throw new Error(`Page vertical margins leave no content height: ${contentHeight}px`);
|
|
61
|
+
}
|
|
62
|
+
return page;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 把已经测好自然高度的谱面行分页,并给出每行的全局顶部坐标
|
|
66
|
+
*/
|
|
67
|
+
export function paginateLayoutLines(heights, page) {
|
|
68
|
+
const availableHeight = page.height - page.marginTop - page.marginBottom;
|
|
69
|
+
const pages = [];
|
|
70
|
+
const lineTops = new Array(heights.length).fill(0);
|
|
71
|
+
let pageTop = 0;
|
|
72
|
+
let start = 0;
|
|
73
|
+
// used 含最小行距,用于判断下一行能否装入;contentHeight 不含行距,用于封页后拉伸
|
|
74
|
+
let used = 0;
|
|
75
|
+
let contentHeight = 0;
|
|
76
|
+
/** 写出 [start, end) 这一页;完整页拉伸行距,末页保留最小行距 */
|
|
77
|
+
const closePage = (end, justify) => {
|
|
78
|
+
const count = end - start;
|
|
79
|
+
const gap = justify && count > 1
|
|
80
|
+
? (availableHeight - contentHeight) / (count - 1)
|
|
81
|
+
: page.lineGap;
|
|
82
|
+
let lineTop = pageTop + page.marginTop;
|
|
83
|
+
for (let i = start; i < end; i++) {
|
|
84
|
+
lineTops[i] = lineTop;
|
|
85
|
+
lineTop += heights[i] + (i + 1 < end ? gap : 0);
|
|
86
|
+
}
|
|
87
|
+
const height = Number.isFinite(page.height)
|
|
88
|
+
? page.height
|
|
89
|
+
// 无限高配置只生成自然高度页面,不把 Infinity 写入布局结果
|
|
90
|
+
: lineTop - pageTop + page.marginBottom;
|
|
91
|
+
pages.push({
|
|
92
|
+
bounds: {
|
|
93
|
+
x: 0,
|
|
94
|
+
y: pageTop,
|
|
95
|
+
w: page.width,
|
|
96
|
+
h: height,
|
|
97
|
+
},
|
|
98
|
+
lineStart: start,
|
|
99
|
+
lineEnd: end,
|
|
100
|
+
});
|
|
101
|
+
pageTop += height;
|
|
102
|
+
start = end;
|
|
103
|
+
used = 0;
|
|
104
|
+
contentHeight = 0;
|
|
105
|
+
};
|
|
106
|
+
for (let i = 0; i < heights.length; i++) {
|
|
107
|
+
const height = heights[i];
|
|
108
|
+
if (Number.isFinite(page.height) && height > availableHeight + 1e-6) {
|
|
109
|
+
throw new PageLayoutError(i, height, availableHeight);
|
|
110
|
+
}
|
|
111
|
+
// 当前行放不下时先封口已有内容;每页因此至少包含一行
|
|
112
|
+
if (Number.isFinite(page.height) && i > start && used + page.lineGap + height > availableHeight + 1e-6) {
|
|
113
|
+
closePage(i, true);
|
|
114
|
+
}
|
|
115
|
+
used += (i > start ? page.lineGap : 0) + height;
|
|
116
|
+
contentHeight += height;
|
|
117
|
+
}
|
|
118
|
+
if (heights.length > start)
|
|
119
|
+
closePage(heights.length, false);
|
|
120
|
+
return { pages, lineTops };
|
|
121
|
+
}
|