@yft-design/engine 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/homography.d.ts +55 -0
- package/dist/core/homography.js +123 -0
- package/dist/core/warpMesh.d.ts +75 -0
- package/dist/core/warpMesh.js +469 -0
- package/dist/export/svgExport.d.ts +11 -5
- package/dist/export/svgExport.js +17 -4
- package/dist/index.d.ts +4 -0
- package/dist/index.js +7 -0
- package/dist/model/smartObjectBake.d.ts +31 -0
- package/dist/model/smartObjectBake.js +295 -0
- package/dist/render/SceneRenderer.js +10 -0
- package/dist/render/TextRenderer.d.ts +12 -0
- package/dist/render/TextRenderer.js +55 -2
- package/dist/render/psdWarpText.d.ts +28 -0
- package/dist/render/psdWarpText.js +212 -0
- package/dist/render/warpText.d.ts +13 -0
- package/dist/render/warpText.js +8 -6
- package/package.json +1 -1
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PSD 文字变形(Warp Text)网格数学——批次 365 自 Patchy(github.com/SethRobinson/Patchy)
|
|
3
|
+
* core/warp_mesh.cpp 逐式移植:15 种变形样式的控制网格构造 + 水平/垂直失真施加 +
|
|
4
|
+
* Bernstein 曲面前向求值 + 前向格点构建 + 闭式逆双线性。
|
|
5
|
+
*
|
|
6
|
+
* Patchy 用 COM 自动化抓 Photoshop 2026 实际生成的网格,58 组 capture 拟合到 1e-4
|
|
7
|
+
* (最大残差 2.4e-6 px)——「样式 + 参数 → 控制点网格」的构造法 Adobe 从未公开,
|
|
8
|
+
* 本模块是该逆向的唯一权威数学源,公式改动须以 Patchy 上游为据。
|
|
9
|
+
*
|
|
10
|
+
* 语义锚点(warp.md「Warp Text」节):
|
|
11
|
+
* - 变形作用于文本 bounds 布局盒而非墨迹盒;PS 每次重排自行重推盒,引擎消费方
|
|
12
|
+
* 同样以节点布局盒实时求值(不落盘缓存盒)。
|
|
13
|
+
* - 失真施加顺序:先按行(v,绕该行两边缘点中点缩放)、后按列(u,同规则),
|
|
14
|
+
* so_persp capture 以 ~1e-7 px 钉死该顺序与中点锚定。
|
|
15
|
+
* - warpRotate=Vrtc = 水平构造在 (h,w) 上做、x/y 互换回装、布局转置(twist 除外
|
|
16
|
+
* ——PS 对 Hrzn/Vrtc 烘出同一网格,方向不变量)。
|
|
17
|
+
* 纯函数无 DOM,jsdom 可测(smartObjectHomography.spec 同范式)。
|
|
18
|
+
*/
|
|
19
|
+
const ZERO_BEND = 1e-9;
|
|
20
|
+
const CELL_EPSILON = 1e-9;
|
|
21
|
+
/** Bernstein 基(order 2/3/4 = 一次/二次/三次),返回长度 4 的权重(尾部补零) */
|
|
22
|
+
const bernsteinWeights = (order, t) => {
|
|
23
|
+
const s = 1 - t;
|
|
24
|
+
if (order === 2)
|
|
25
|
+
return [s, t, 0, 0];
|
|
26
|
+
if (order === 3)
|
|
27
|
+
return [s * s, 2 * s * t, t * t, 0];
|
|
28
|
+
return [s * s * s, 3 * s * s * t, 3 * s * t * t, t * t * t];
|
|
29
|
+
};
|
|
30
|
+
/** 盒网格(uOrder×vOrder 控制点铺在 left/top/right/bottom 上;order 钳 [2,4]) */
|
|
31
|
+
export const identityWarpMesh = (left, top, right, bottom, uOrder, vOrder) => {
|
|
32
|
+
const mesh = {
|
|
33
|
+
uOrder: Math.min(4, Math.max(2, uOrder)),
|
|
34
|
+
vOrder: Math.min(4, Math.max(2, vOrder)),
|
|
35
|
+
xs: [],
|
|
36
|
+
ys: []
|
|
37
|
+
};
|
|
38
|
+
for (let row = 0; row < mesh.vOrder; row++) {
|
|
39
|
+
const fv = mesh.vOrder === 1 ? 0 : row / (mesh.vOrder - 1);
|
|
40
|
+
for (let column = 0; column < mesh.uOrder; column++) {
|
|
41
|
+
const fu = mesh.uOrder === 1 ? 0 : column / (mesh.uOrder - 1);
|
|
42
|
+
mesh.xs.push(left + (right - left) * fu);
|
|
43
|
+
mesh.ys.push(top + (bottom - top) * fv);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return mesh;
|
|
47
|
+
};
|
|
48
|
+
/** 张量积 Bernstein 曲面在 (u, v) 的前向求值(u/v 可越 [0,1]——Bernstein 外推覆盖出盒墨迹) */
|
|
49
|
+
export const evaluateWarpMesh = (mesh, u, v) => {
|
|
50
|
+
const wu = bernsteinWeights(mesh.uOrder, u);
|
|
51
|
+
const wv = bernsteinWeights(mesh.vOrder, v);
|
|
52
|
+
let x = 0;
|
|
53
|
+
let y = 0;
|
|
54
|
+
for (let row = 0; row < mesh.vOrder; row++) {
|
|
55
|
+
for (let column = 0; column < mesh.uOrder; column++) {
|
|
56
|
+
const weight = wu[column] * wv[row];
|
|
57
|
+
x += weight * mesh.xs[row * mesh.uOrder + column];
|
|
58
|
+
y += weight * mesh.ys[row * mesh.uOrder + column];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return { x, y };
|
|
62
|
+
};
|
|
63
|
+
/** 经典三次贝塞尔圆弧近似的一行:水平弦 (ax,y)..(bx,y)、圆心角 2θ、bulgeUp 时向 -y 鼓出
|
|
64
|
+
* (内容空间 +y 向下)。手柄长 (4/3)·tan(θ/2)·R,端点切线自弦转 θ(E9 capture 逐点吻合)。 */
|
|
65
|
+
const appendArcRow = (mesh, ax, bx, y, theta, bulgeUp) => {
|
|
66
|
+
const sinTheta = Math.sin(theta);
|
|
67
|
+
const cosTheta = Math.cos(theta);
|
|
68
|
+
const radius = (bx - ax) / (2 * sinTheta);
|
|
69
|
+
const handle = (4 / 3) * Math.tan(theta / 2) * radius;
|
|
70
|
+
const dy = bulgeUp ? -handle * sinTheta : handle * sinTheta;
|
|
71
|
+
mesh.xs.push(ax, ax + handle * cosTheta, bx - handle * cosTheta, bx);
|
|
72
|
+
mesh.ys.push(y, y + dy, y + dy, y);
|
|
73
|
+
};
|
|
74
|
+
const appendIdentityRow = (mesh, width, y) => {
|
|
75
|
+
mesh.xs.push(0, width / 3, (2 * width) / 3, width);
|
|
76
|
+
mesh.ys.push(y, y, y, y);
|
|
77
|
+
};
|
|
78
|
+
/** 4×n 网格纵向镜像(y → height − y,行序倒转):PS 负 bend 的 arc 就是正 bend 的上下翻转 */
|
|
79
|
+
const mirrorMeshVertically = (mesh, height) => {
|
|
80
|
+
const mirrored = { uOrder: mesh.uOrder, vOrder: mesh.vOrder, xs: [], ys: [] };
|
|
81
|
+
for (let row = 0; row < mesh.vOrder; row++) {
|
|
82
|
+
const sourceRow = mesh.vOrder - 1 - row;
|
|
83
|
+
for (let column = 0; column < mesh.uOrder; column++) {
|
|
84
|
+
const to = row * mesh.uOrder + column;
|
|
85
|
+
const from = sourceRow * mesh.uOrder + column;
|
|
86
|
+
mirrored.xs[to] = mesh.xs[from];
|
|
87
|
+
mirrored.ys[to] = height - mesh.ys[from];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return mirrored;
|
|
91
|
+
};
|
|
92
|
+
/** warpRotate=Vrtc 的换轴装回:水平构造在 (h,w) 上做、x/y 互换、布局转置(e9 vrtc capture) */
|
|
93
|
+
const swapMeshAxes = (mesh) => {
|
|
94
|
+
const swapped = { uOrder: mesh.vOrder, vOrder: mesh.uOrder, xs: [], ys: [] };
|
|
95
|
+
for (let row = 0; row < mesh.vOrder; row++) {
|
|
96
|
+
for (let column = 0; column < mesh.uOrder; column++) {
|
|
97
|
+
const from = row * mesh.uOrder + column;
|
|
98
|
+
const to = column * mesh.vOrder + row;
|
|
99
|
+
swapped.xs[to] = mesh.ys[from];
|
|
100
|
+
swapped.ys[to] = mesh.xs[from];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return swapped;
|
|
104
|
+
};
|
|
105
|
+
/** 15 样式的水平构造(warp.md §样式网格构造 逐式对应);style 词表 = psd-lib warpStyle 解码值 */
|
|
106
|
+
const generateHorizontalStyleMesh = (style, value, width, height) => {
|
|
107
|
+
const bend = Math.min(100, Math.max(-100, value));
|
|
108
|
+
const theta = (Math.abs(bend) * Math.PI) / 200;
|
|
109
|
+
// flag/wave/rise 的位移尺度基于高度而非宽度(E9b capture)
|
|
110
|
+
const displacement = (2 * height * bend) / 100;
|
|
111
|
+
let mesh = { uOrder: 4, vOrder: style === 'wave' ? 3 : 2, xs: [], ys: [] };
|
|
112
|
+
if (style === 'arcLower' || style === 'arcUpper') {
|
|
113
|
+
if (Math.abs(bend) < ZERO_BEND)
|
|
114
|
+
return identityWarpMesh(0, 0, width, height, 4, 2);
|
|
115
|
+
// 一边保持原位;另一边正 bend 向盒外拱、负 bend 拱进盒内(e9c capture)
|
|
116
|
+
appendIdentityRow(mesh, width, 0);
|
|
117
|
+
appendArcRow(mesh, 0, width, height, theta, bend < 0);
|
|
118
|
+
return style === 'arcUpper' ? mirrorMeshVertically(mesh, height) : mesh;
|
|
119
|
+
}
|
|
120
|
+
if (style === 'fish') {
|
|
121
|
+
// flag 的 S 曲线在上缘、下缘反向 S——首尾被掐(e9c:内部列上 -/+d、下 +/-d)
|
|
122
|
+
appendIdentityRow(mesh, width, 0);
|
|
123
|
+
appendIdentityRow(mesh, width, height);
|
|
124
|
+
mesh.ys[1] -= displacement;
|
|
125
|
+
mesh.ys[2] += displacement;
|
|
126
|
+
mesh.ys[5] += displacement;
|
|
127
|
+
mesh.ys[6] -= displacement;
|
|
128
|
+
return mesh;
|
|
129
|
+
}
|
|
130
|
+
if (style === 'fisheye') {
|
|
131
|
+
// 4×4 仅四个内部点动:各沿到最近角点的连线移 bend/50(+50 时恰好坐在角点上)
|
|
132
|
+
mesh = identityWarpMesh(0, 0, width, height, 4, 4);
|
|
133
|
+
const t = bend / 50;
|
|
134
|
+
for (let row = 1; row <= 2; row++) {
|
|
135
|
+
for (let column = 1; column <= 2; column++) {
|
|
136
|
+
const index = row * 4 + column;
|
|
137
|
+
const cornerX = column === 1 ? 0 : width;
|
|
138
|
+
const cornerY = row === 1 ? 0 : height;
|
|
139
|
+
mesh.xs[index] += t * (cornerX - mesh.xs[index]);
|
|
140
|
+
mesh.ys[index] += t * (cornerY - mesh.ys[index]);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return mesh;
|
|
144
|
+
}
|
|
145
|
+
if (style === 'inflate' || style === 'squeeze') {
|
|
146
|
+
// 二次 3×3 面片:角点与中心钉死,边中点每 50% bend 滑轴长的 1/4。inflate 四边
|
|
147
|
+
// 全外推;squeeze 上下外推、左右内捏(e9c capture)
|
|
148
|
+
mesh = identityWarpMesh(0, 0, width, height, 3, 3);
|
|
149
|
+
const dx = (width * bend) / 200;
|
|
150
|
+
const dy = (height * bend) / 200;
|
|
151
|
+
mesh.ys[1] -= dy;
|
|
152
|
+
mesh.ys[7] += dy;
|
|
153
|
+
if (style === 'inflate') {
|
|
154
|
+
mesh.xs[3] -= dx;
|
|
155
|
+
mesh.xs[5] += dx;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
mesh.xs[3] += dx;
|
|
159
|
+
mesh.xs[5] -= dx;
|
|
160
|
+
}
|
|
161
|
+
return mesh;
|
|
162
|
+
}
|
|
163
|
+
if (style === 'twist') {
|
|
164
|
+
// 4×4 内环切向环流:各内部点朝下一环位移动(正 bend 顺时针、负逆时针),
|
|
165
|
+
// 每 100% bend 移满轴长(e9c capture)
|
|
166
|
+
mesh = identityWarpMesh(0, 0, width, height, 4, 4);
|
|
167
|
+
const moveX = (width * Math.abs(bend)) / 100;
|
|
168
|
+
const moveY = (height * Math.abs(bend)) / 100;
|
|
169
|
+
if (bend > 0) {
|
|
170
|
+
mesh.xs[5] += moveX;
|
|
171
|
+
mesh.ys[6] += moveY;
|
|
172
|
+
mesh.xs[10] -= moveX;
|
|
173
|
+
mesh.ys[9] -= moveY;
|
|
174
|
+
}
|
|
175
|
+
else if (bend < 0) {
|
|
176
|
+
mesh.ys[5] += moveY;
|
|
177
|
+
mesh.xs[6] -= moveX;
|
|
178
|
+
mesh.ys[10] -= moveY;
|
|
179
|
+
mesh.xs[9] += moveX;
|
|
180
|
+
}
|
|
181
|
+
return mesh;
|
|
182
|
+
}
|
|
183
|
+
if (style === 'shellLower' || style === 'shellUpper') {
|
|
184
|
+
if (Math.abs(bend) < ZERO_BEND)
|
|
185
|
+
return identityWarpMesh(0, 0, width, height, 4, 4);
|
|
186
|
+
// 两行保持 identity;第三行端点绕锚角旋转 θ(正 bend 锚顶角、负 bend 锚底角)、
|
|
187
|
+
// 内部保持 identity;壳缘自身正 bend 绕远角(半径 height)外扬成弧、负 bend 沿
|
|
188
|
+
// 原弦向盒内收弧(e9c/e9d capture)
|
|
189
|
+
const sinTheta = Math.sin(theta);
|
|
190
|
+
const cosTheta = Math.cos(theta);
|
|
191
|
+
mesh = { uOrder: 4, vOrder: 4, xs: [], ys: [] };
|
|
192
|
+
appendIdentityRow(mesh, width, 0);
|
|
193
|
+
appendIdentityRow(mesh, width, height / 3);
|
|
194
|
+
if (bend > 0) {
|
|
195
|
+
const radius = (2 * height) / 3;
|
|
196
|
+
mesh.xs.push(-radius * sinTheta, width / 3, (2 * width) / 3, width + radius * sinTheta);
|
|
197
|
+
mesh.ys.push(radius * cosTheta, (2 * height) / 3, (2 * height) / 3, radius * cosTheta);
|
|
198
|
+
appendArcRow(mesh, -height * sinTheta, width + height * sinTheta, height * cosTheta, theta, false);
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
const radius = height / 3;
|
|
202
|
+
mesh.xs.push(-radius * sinTheta, width / 3, (2 * width) / 3, width + radius * sinTheta);
|
|
203
|
+
mesh.ys.push(height - radius * cosTheta, (2 * height) / 3, (2 * height) / 3, height - radius * cosTheta);
|
|
204
|
+
appendArcRow(mesh, 0, width, height, theta, true);
|
|
205
|
+
}
|
|
206
|
+
return style === 'shellUpper' ? mirrorMeshVertically(mesh, height) : mesh;
|
|
207
|
+
}
|
|
208
|
+
if (style === 'arc') {
|
|
209
|
+
if (Math.abs(bend) < ZERO_BEND)
|
|
210
|
+
return identityWarpMesh(0, 0, width, height, 4, 2);
|
|
211
|
+
const sinTheta = Math.sin(theta);
|
|
212
|
+
// 顶角绕底角外摆(半径 = height);两缘同圆心角画弧——带厚恒为 height
|
|
213
|
+
appendArcRow(mesh, -height * sinTheta, width + height * sinTheta, height * (1 - Math.cos(theta)), theta, true);
|
|
214
|
+
appendArcRow(mesh, 0, width, height, theta, true);
|
|
215
|
+
return bend < 0 ? mirrorMeshVertically(mesh, height) : mesh;
|
|
216
|
+
}
|
|
217
|
+
if (style === 'arch' || style === 'bulge') {
|
|
218
|
+
if (Math.abs(bend) < ZERO_BEND)
|
|
219
|
+
return identityWarpMesh(0, 0, width, height, 4, 2);
|
|
220
|
+
const topUp = bend > 0;
|
|
221
|
+
appendArcRow(mesh, 0, width, 0, theta, topUp);
|
|
222
|
+
// arch:两缘同向拱(列刚体平移);bulge:下缘反向拱(带被撑鼓)
|
|
223
|
+
appendArcRow(mesh, 0, width, height, theta, style === 'bulge' ? !topUp : topUp);
|
|
224
|
+
return mesh;
|
|
225
|
+
}
|
|
226
|
+
if (style === 'flag') {
|
|
227
|
+
appendIdentityRow(mesh, width, 0);
|
|
228
|
+
appendIdentityRow(mesh, width, height);
|
|
229
|
+
mesh.ys[1] -= displacement;
|
|
230
|
+
mesh.ys[2] += displacement;
|
|
231
|
+
mesh.ys[5] -= displacement;
|
|
232
|
+
mesh.ys[6] += displacement;
|
|
233
|
+
return mesh;
|
|
234
|
+
}
|
|
235
|
+
if (style === 'wave') {
|
|
236
|
+
appendIdentityRow(mesh, width, 0);
|
|
237
|
+
appendIdentityRow(mesh, width, height / 2);
|
|
238
|
+
appendIdentityRow(mesh, width, height);
|
|
239
|
+
// 两缘行保持水平;仅中间行摆动(且与 flag 的 S 反向)
|
|
240
|
+
mesh.ys[5] += displacement;
|
|
241
|
+
mesh.ys[6] -= displacement;
|
|
242
|
+
return mesh;
|
|
243
|
+
}
|
|
244
|
+
if (style === 'rise') {
|
|
245
|
+
// 刚体列坡道:左半控制点纵向整体平移 d
|
|
246
|
+
appendIdentityRow(mesh, width, 0);
|
|
247
|
+
appendIdentityRow(mesh, width, height);
|
|
248
|
+
mesh.ys[0] += displacement;
|
|
249
|
+
mesh.ys[1] += displacement;
|
|
250
|
+
mesh.ys[4] += displacement;
|
|
251
|
+
mesh.ys[5] += displacement;
|
|
252
|
+
return mesh;
|
|
253
|
+
}
|
|
254
|
+
return undefined;
|
|
255
|
+
};
|
|
256
|
+
/** 可生成样式判定(psd-lib 词表;none/custom 等 COM 烘焙形态由位图兜底,不走生成器) */
|
|
257
|
+
export const isGeneratableWarpStyle = (style) => {
|
|
258
|
+
return (style === 'arc' ||
|
|
259
|
+
style === 'arch' ||
|
|
260
|
+
style === 'bulge' ||
|
|
261
|
+
style === 'flag' ||
|
|
262
|
+
style === 'wave' ||
|
|
263
|
+
style === 'rise' ||
|
|
264
|
+
style === 'arcLower' ||
|
|
265
|
+
style === 'arcUpper' ||
|
|
266
|
+
style === 'shellLower' ||
|
|
267
|
+
style === 'shellUpper' ||
|
|
268
|
+
style === 'fish' ||
|
|
269
|
+
style === 'fisheye' ||
|
|
270
|
+
style === 'inflate' ||
|
|
271
|
+
style === 'squeeze' ||
|
|
272
|
+
style === 'twist');
|
|
273
|
+
};
|
|
274
|
+
/** 样式 + bend + 方向 → 控制网格(Vrtc = 换轴构造;twist 方向不变量不转置) */
|
|
275
|
+
export const generateStyleWarpMesh = (style, value, rotateVertical, width, height) => {
|
|
276
|
+
if (!isGeneratableWarpStyle(style) || width <= 0 || height <= 0)
|
|
277
|
+
return undefined;
|
|
278
|
+
if (!rotateVertical || style === 'twist')
|
|
279
|
+
return generateHorizontalStyleMesh(style, value, width, height);
|
|
280
|
+
const horizontal = generateHorizontalStyleMesh(style, value, height, width);
|
|
281
|
+
return horizontal ? swapMeshAxes(horizontal) : undefined;
|
|
282
|
+
};
|
|
283
|
+
/** 水平/垂直失真(Warp Text 对话框两失真项):先 v 行后 u 列,每行/列绕其两边缘
|
|
284
|
+
* 点的中点缩放 1+(2·t-1)·percent/100(so_persp capture 以 ~1e-7 px 钉死顺序与中点规则) */
|
|
285
|
+
export const applyWarpDistortion = (mesh, horizontalPercent, verticalPercent) => {
|
|
286
|
+
const vertical = Math.min(100, Math.max(-100, verticalPercent));
|
|
287
|
+
const horizontal = Math.min(100, Math.max(-100, horizontalPercent));
|
|
288
|
+
if (vertical !== 0 && mesh.vOrder > 1) {
|
|
289
|
+
for (let row = 0; row < mesh.vOrder; row++) {
|
|
290
|
+
const first = row * mesh.uOrder;
|
|
291
|
+
const last = first + mesh.uOrder - 1;
|
|
292
|
+
const midX = (mesh.xs[first] + mesh.xs[last]) / 2;
|
|
293
|
+
const midY = (mesh.ys[first] + mesh.ys[last]) / 2;
|
|
294
|
+
const v = row / (mesh.vOrder - 1);
|
|
295
|
+
const scale = 1 + ((2 * v - 1) * vertical) / 100;
|
|
296
|
+
for (let column = 0; column < mesh.uOrder; column++) {
|
|
297
|
+
const index = first + column;
|
|
298
|
+
mesh.xs[index] = midX + (mesh.xs[index] - midX) * scale;
|
|
299
|
+
mesh.ys[index] = midY + (mesh.ys[index] - midY) * scale;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (horizontal !== 0 && mesh.uOrder > 1) {
|
|
304
|
+
for (let column = 0; column < mesh.uOrder; column++) {
|
|
305
|
+
const first = column;
|
|
306
|
+
const last = (mesh.vOrder - 1) * mesh.uOrder + column;
|
|
307
|
+
const midX = (mesh.xs[first] + mesh.xs[last]) / 2;
|
|
308
|
+
const midY = (mesh.ys[first] + mesh.ys[last]) / 2;
|
|
309
|
+
const u = column / (mesh.uOrder - 1);
|
|
310
|
+
const scale = 1 + ((2 * u - 1) * horizontal) / 100;
|
|
311
|
+
for (let row = 0; row < mesh.vOrder; row++) {
|
|
312
|
+
const index = row * mesh.uOrder + column;
|
|
313
|
+
mesh.xs[index] = midX + (mesh.xs[index] - midX) * scale;
|
|
314
|
+
mesh.ys[index] = midY + (mesh.ys[index] - midY) * scale;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
/** 参数 → 完整变形网格(样式构造 + 失真施加)。身份参数(全 0)返回 undefined 由调用方走常规管线。 */
|
|
320
|
+
export const generatePsdWarpMesh = (params, width, height) => {
|
|
321
|
+
const mesh = generateStyleWarpMesh(params.style, params.bend, params.rotate === 'vertical', width, height);
|
|
322
|
+
if (!mesh)
|
|
323
|
+
return undefined;
|
|
324
|
+
applyWarpDistortion(mesh, params.horizontal, params.vertical);
|
|
325
|
+
return mesh;
|
|
326
|
+
};
|
|
327
|
+
/** 元素 raw.psdWarp 的有效变形判定:形状合法 + 样式可生成 + 至少一参数非 0。
|
|
328
|
+
* 身份/不可生成(none、custom COM 烘焙形态)→ null,渲染走位图/常规管线。 */
|
|
329
|
+
export const activePsdTextWarp = (raw) => {
|
|
330
|
+
if (raw === null || typeof raw !== 'object')
|
|
331
|
+
return null;
|
|
332
|
+
const holder = raw;
|
|
333
|
+
const warp = holder.psdWarp;
|
|
334
|
+
if (warp === null || typeof warp !== 'object')
|
|
335
|
+
return null;
|
|
336
|
+
const w = warp;
|
|
337
|
+
const style = typeof w.style === 'string' ? w.style : '';
|
|
338
|
+
if (!isGeneratableWarpStyle(style))
|
|
339
|
+
return null;
|
|
340
|
+
const bend = Number(w.bend) || 0;
|
|
341
|
+
const horizontal = Number(w.horizontal) || 0;
|
|
342
|
+
const vertical = Number(w.vertical) || 0;
|
|
343
|
+
if (bend === 0 && horizontal === 0 && vertical === 0)
|
|
344
|
+
return null;
|
|
345
|
+
return { style, bend, horizontal, vertical, rotate: w.rotate === 'vertical' ? 'vertical' : 'horizontal' };
|
|
346
|
+
};
|
|
347
|
+
/** 源位图窗口 → 前向重采样格点。窗口是源位图的墨迹裁剪(参数区间 [u0,u1]×[v0,v1]
|
|
348
|
+
* 覆盖出盒墨迹的 Bernstein 外推),affine 把网格盒空间合成到输出像素空间
|
|
349
|
+
* (fabric Mat 列向量约定)。源像素 = fraction × sourceSize(窗口即整张源位图)。 */
|
|
350
|
+
export const buildWarpSurfaceGridOverWindow = (mesh, boxLeft, boxTop, boxRight, boxBottom, windowLeft, windowTop, windowRight, windowBottom, sourceWidth, sourceHeight, affine, maxCellDocPixels, maxCells) => {
|
|
351
|
+
const boxWidth = boxRight - boxLeft;
|
|
352
|
+
const boxHeight = boxBottom - boxTop;
|
|
353
|
+
const windowWidth = windowRight - windowLeft;
|
|
354
|
+
const windowHeight = windowBottom - windowTop;
|
|
355
|
+
if (boxWidth <= 0 || boxHeight <= 0 || windowWidth <= 0 || windowHeight <= 0 || sourceWidth <= 0 || sourceHeight <= 0 || maxCellDocPixels <= 0 || mesh.xs.length === 0) {
|
|
356
|
+
return undefined;
|
|
357
|
+
}
|
|
358
|
+
const u0 = (windowLeft - boxLeft) / boxWidth;
|
|
359
|
+
const u1 = (windowRight - boxLeft) / boxWidth;
|
|
360
|
+
const v0 = (windowTop - boxTop) / boxHeight;
|
|
361
|
+
const v1 = (windowBottom - boxTop) / boxHeight;
|
|
362
|
+
const mapPoint = (u, v) => {
|
|
363
|
+
const surface = evaluateWarpMesh(mesh, u, v);
|
|
364
|
+
return {
|
|
365
|
+
x: affine[0] * surface.x + affine[2] * surface.y + affine[4],
|
|
366
|
+
y: affine[1] * surface.x + affine[3] * surface.y + affine[5]
|
|
367
|
+
};
|
|
368
|
+
};
|
|
369
|
+
// 探针趟:由变形后窗口的输出范围定格点密度( Bernstein 外推干净覆盖出盒墨迹)
|
|
370
|
+
let minX = 0;
|
|
371
|
+
let maxX = 0;
|
|
372
|
+
let minY = 0;
|
|
373
|
+
let maxY = 0;
|
|
374
|
+
let firstProbe = true;
|
|
375
|
+
const probe = 8;
|
|
376
|
+
for (let row = 0; row <= probe; row++) {
|
|
377
|
+
const v = v0 + ((v1 - v0) * row) / probe;
|
|
378
|
+
for (let column = 0; column <= probe; column++) {
|
|
379
|
+
const u = u0 + ((u1 - u0) * column) / probe;
|
|
380
|
+
const doc = mapPoint(u, v);
|
|
381
|
+
if (firstProbe) {
|
|
382
|
+
minX = maxX = doc.x;
|
|
383
|
+
minY = maxY = doc.y;
|
|
384
|
+
firstProbe = false;
|
|
385
|
+
}
|
|
386
|
+
else {
|
|
387
|
+
minX = Math.min(minX, doc.x);
|
|
388
|
+
maxX = Math.max(maxX, doc.x);
|
|
389
|
+
minY = Math.min(minY, doc.y);
|
|
390
|
+
maxY = Math.max(maxY, doc.y);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
const clampCells = Math.max(8, maxCells);
|
|
395
|
+
const cellsX = Math.min(clampCells, Math.max(8, Math.ceil((maxX - minX) / maxCellDocPixels)));
|
|
396
|
+
const cellsY = Math.min(clampCells, Math.max(8, Math.ceil((maxY - minY) / maxCellDocPixels)));
|
|
397
|
+
const grid = { columns: cellsX + 1, rows: cellsY + 1, docXs: [], docYs: [], sourceXs: [], sourceYs: [] };
|
|
398
|
+
for (let row = 0; row < grid.rows; row++) {
|
|
399
|
+
const fractionV = row / cellsY;
|
|
400
|
+
for (let column = 0; column < grid.columns; column++) {
|
|
401
|
+
const fractionU = column / cellsX;
|
|
402
|
+
const doc = mapPoint(u0 + (u1 - u0) * fractionU, v0 + (v1 - v0) * fractionV);
|
|
403
|
+
grid.docXs.push(doc.x);
|
|
404
|
+
grid.docYs.push(doc.y);
|
|
405
|
+
grid.sourceXs.push(fractionU * sourceWidth);
|
|
406
|
+
grid.sourceYs.push(fractionV * sourceHeight);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
return grid;
|
|
410
|
+
};
|
|
411
|
+
/** 闭式逆双线性:解 p(s,t) = (1-s)(1-t)p00 + s(1-t)p10 + st·p11 + (1-s)t·p01 = (x,y)。
|
|
412
|
+
* 叉积消 t 得 s 的一元二次;无解(点在格外)返回 undefined。 */
|
|
413
|
+
export const invertBilinearCell = (x, y, x00, y00, x10, y10, x11, y11, x01, y01) => {
|
|
414
|
+
const ax = x10 - x00;
|
|
415
|
+
const ay = y10 - y00;
|
|
416
|
+
const bx = x01 - x00;
|
|
417
|
+
const by = y01 - y00;
|
|
418
|
+
const cx = x00 - x10 - x01 + x11;
|
|
419
|
+
const cy = y00 - y10 - y01 + y11;
|
|
420
|
+
const dx = x - x00;
|
|
421
|
+
const dy = y - y00;
|
|
422
|
+
const k2 = ax * cy - ay * cx;
|
|
423
|
+
const k1 = ax * by - ay * bx - (dx * cy - dy * cx);
|
|
424
|
+
const k0 = -(dx * by - dy * bx);
|
|
425
|
+
let s;
|
|
426
|
+
if (Math.abs(k2) < CELL_EPSILON) {
|
|
427
|
+
if (Math.abs(k1) < CELL_EPSILON)
|
|
428
|
+
return undefined;
|
|
429
|
+
s = -k0 / k1;
|
|
430
|
+
}
|
|
431
|
+
else {
|
|
432
|
+
const discriminant = k1 * k1 - 4 * k2 * k0;
|
|
433
|
+
if (discriminant < 0)
|
|
434
|
+
return undefined;
|
|
435
|
+
const root = Math.sqrt(discriminant);
|
|
436
|
+
const s0 = (-k1 + root) / (2 * k2);
|
|
437
|
+
const s1 = (-k1 - root) / (2 * k2);
|
|
438
|
+
const s0Valid = s0 >= -CELL_EPSILON && s0 <= 1 + CELL_EPSILON;
|
|
439
|
+
const s1Valid = s1 >= -CELL_EPSILON && s1 <= 1 + CELL_EPSILON;
|
|
440
|
+
if (s0Valid && s1Valid) {
|
|
441
|
+
s = Math.min(Math.max(s0, 0), 1);
|
|
442
|
+
}
|
|
443
|
+
else if (s0Valid) {
|
|
444
|
+
s = s0;
|
|
445
|
+
}
|
|
446
|
+
else if (s1Valid) {
|
|
447
|
+
s = s1;
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
return undefined;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const tx = bx + cx * s;
|
|
454
|
+
const ty = by + cy * s;
|
|
455
|
+
let t;
|
|
456
|
+
if (Math.abs(tx) >= Math.abs(ty)) {
|
|
457
|
+
if (Math.abs(tx) < CELL_EPSILON)
|
|
458
|
+
return undefined;
|
|
459
|
+
t = (dx - ax * s) / tx;
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
if (Math.abs(ty) < CELL_EPSILON)
|
|
463
|
+
return undefined;
|
|
464
|
+
t = (dy - ay * s) / ty;
|
|
465
|
+
}
|
|
466
|
+
if (s < -CELL_EPSILON || s > 1 + CELL_EPSILON || t < -CELL_EPSILON || t > 1 + CELL_EPSILON)
|
|
467
|
+
return undefined;
|
|
468
|
+
return { s: Math.min(Math.max(s, 0), 1), t: Math.min(Math.max(t, 0), 1) };
|
|
469
|
+
};
|
|
@@ -72,11 +72,16 @@ export interface SvgTextBitmap {
|
|
|
72
72
|
/** 逻辑尺寸(quad 居中于对象中心——渲染 pushQuad 同构) */
|
|
73
73
|
width: number;
|
|
74
74
|
height: number;
|
|
75
|
+
/** 画布中心相对对象中心的逻辑偏移(批次 365 PSD 变形族:变形输出窗一般不对称
|
|
76
|
+
* 于对象中心;缺省 = 恒居中——arch 族等既有约定不变) */
|
|
77
|
+
offsetX?: number;
|
|
78
|
+
offsetY?: number;
|
|
75
79
|
}
|
|
76
80
|
/** 文本位图烘焙注入的族分派:warp 三族(变形本体位图化,无条件)、arc/circle
|
|
77
81
|
* (bakeArcText)、vertical/plain(bakeTextEffects——仅带活跃特效时位图化,
|
|
78
|
-
* 无特效返 null
|
|
79
|
-
|
|
82
|
+
* 无特效返 null 走矢量)、psd(批次 365 bakePsdWarpText——PSD 变形文本无条件
|
|
83
|
+
* 位图化)。null = 不可烘焙/无需位图,序列化器回退矢量路径。 */
|
|
84
|
+
export type SvgTextBitmapFamily = 'arch' | 'waist' | 'flag' | 'arc' | 'circle' | 'vertical' | 'plain' | 'psd';
|
|
80
85
|
export type SvgTextBitmapBaker = (raw: Record<string, unknown>, family: SvgTextBitmapFamily) => SvgTextBitmap | null;
|
|
81
86
|
/** 一次导出运行的级配置(实例构造期给定,跨页共享) */
|
|
82
87
|
export interface SvgExporterOptions {
|
|
@@ -159,9 +164,10 @@ export declare class SvgExporter {
|
|
|
159
164
|
private emitShadowFilter;
|
|
160
165
|
/** clipPath 形状 → defs;不支持形态返回 null(调用方 skip 节点) */
|
|
161
166
|
private emitClipPath;
|
|
162
|
-
/** 位图文本发射公共件:image 中心锚定(logicalW×H——渲染 pushQuad
|
|
163
|
-
*
|
|
164
|
-
* 绕中心缩放(emitText
|
|
167
|
+
/** 位图文本发射公共件:image 中心锚定(logicalW×H——渲染 pushQuad 同构;
|
|
168
|
+
* PSD 变形族带 offsetX/Y 中心偏移——渲染侧同源)+ fabric 阴影 feDropShadow
|
|
169
|
+
* (位图不含 PSD 体系外的 raw.shadow)+ textScale 绕中心缩放(emitText
|
|
170
|
+
* 家族语义级缩放同款) */
|
|
165
171
|
private emitBakedTextImage;
|
|
166
172
|
private emitText;
|
|
167
173
|
/**
|
package/dist/export/svgExport.js
CHANGED
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
* 供 e2e/排障区分引擎产物与 fabric 产物。
|
|
37
37
|
*/
|
|
38
38
|
import { gradientSpaceMatrix, parseGradientDecl } from '../core/gradient';
|
|
39
|
+
import { activePsdTextWarp } from '../core/warpMesh';
|
|
39
40
|
import { invert, multiply, objectOwnMatrix } from '../core/mat2d';
|
|
40
41
|
import { parsePathData } from '../core/pathData';
|
|
41
42
|
import { computeTableLayout } from '../model/tableLayout';
|
|
@@ -710,16 +711,19 @@ export class SvgExporter {
|
|
|
710
711
|
this.defs.push(`<clipPath id="${id}">${geometryElement(geometry, m, '')}</clipPath>`);
|
|
711
712
|
return ` clip-path="url(#${id})"`;
|
|
712
713
|
}
|
|
713
|
-
/** 位图文本发射公共件:image 中心锚定(logicalW×H——渲染 pushQuad
|
|
714
|
-
*
|
|
715
|
-
* 绕中心缩放(emitText
|
|
714
|
+
/** 位图文本发射公共件:image 中心锚定(logicalW×H——渲染 pushQuad 同构;
|
|
715
|
+
* PSD 变形族带 offsetX/Y 中心偏移——渲染侧同源)+ fabric 阴影 feDropShadow
|
|
716
|
+
* (位图不含 PSD 体系外的 raw.shadow)+ textScale 绕中心缩放(emitText
|
|
717
|
+
* 家族语义级缩放同款) */
|
|
716
718
|
emitBakedTextImage(raw, baked) {
|
|
717
719
|
const shadow = parseShadow(raw);
|
|
718
720
|
if (shadow === undefined) {
|
|
719
721
|
this.skip('shadow-unsupported');
|
|
720
722
|
return null;
|
|
721
723
|
}
|
|
722
|
-
|
|
724
|
+
const x0 = -baked.width / 2 + (baked.offsetX ?? 0);
|
|
725
|
+
const y0 = -baked.height / 2 + (baked.offsetY ?? 0);
|
|
726
|
+
let out = `<image href="${escapeXml(baked.dataUrl)}" xlink:href="${escapeXml(baked.dataUrl)}" x="${fmt(x0)}" y="${fmt(y0)}" width="${fmt(baked.width)}" height="${fmt(baked.height)}"/>`;
|
|
723
727
|
if (shadow)
|
|
724
728
|
out = `<g filter="url(#${this.emitShadowFilter(shadow)})">${out}</g>`;
|
|
725
729
|
const tsx = num(raw, 'textScaleX', 1) || 1;
|
|
@@ -738,6 +742,15 @@ export class SvgExporter {
|
|
|
738
742
|
const warp = warpKindOf(raw);
|
|
739
743
|
if (warp)
|
|
740
744
|
return this.emitWarpedText(raw, warp);
|
|
745
|
+
// 批次 365 PSD 变形文本:转正态(renderSrc 已删,位图 <image> 分支不再命中)
|
|
746
|
+
// 的 psdWarp 无条件位图化;烘焙失败/注入缺省回退下方常规矢量管线(内容
|
|
747
|
+
// 保留、变形丢失——warp 族「烘焙失败特效丢失」同族登记近似)
|
|
748
|
+
if (activePsdTextWarp(raw)) {
|
|
749
|
+
const baked = this.options.bakeTextBitmap?.(raw, 'psd') ?? null;
|
|
750
|
+
if (baked)
|
|
751
|
+
return this.emitBakedTextImage(raw, baked);
|
|
752
|
+
this.skip('text-psdwarp-bitmap-fallback');
|
|
753
|
+
}
|
|
741
754
|
const width = num(raw, 'width', NaN);
|
|
742
755
|
const height = num(raw, 'height', NaN);
|
|
743
756
|
if (!Number.isFinite(width) || !Number.isFinite(height)) {
|
package/dist/index.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ export type { GradientFiller, PatternFiller, TFiller, Control } from './model/fi
|
|
|
14
14
|
export { CompatPoint, Point, type PointLike } from './core/point';
|
|
15
15
|
export { buildEngineShapePath, type EngineShapePathResult } from './model/shapePath';
|
|
16
16
|
export { imageMaskDeclOf, imageMaskOffset, maskOutsideRects, maskPixelsToAlpha, type ImageMaskDecl } from './model/imageMaskBake';
|
|
17
|
+
export { renderPlacedSmartObjectLayer, type SmartObjectPlacedDecl, type SmartObjectRenderOptions, type SmartObjectRenderResult } from './model/smartObjectBake';
|
|
17
18
|
export { attachTableFacade } from './model/tableFacade';
|
|
18
19
|
export { deriveArcCharTransformations } from './model/arcTextTransform';
|
|
19
20
|
export { CompatObject, wrapCompatObject, findParentGroupOf, type CompatObjectContext } from './model/compatObject';
|
|
@@ -51,6 +52,9 @@ export { bakeArcText, glyphStyleOf, type ArcBake } from './render/arcText';
|
|
|
51
52
|
export { planTextHighlight, type TextsSegmentLike } from './render/textHighlight';
|
|
52
53
|
export { glyphStyleAt, verticalGlyphOps } from './render/verticalText';
|
|
53
54
|
export { bakeWarpedText, bakeTextEffects, type WarpBake, type WarpKind } from './render/warpText';
|
|
55
|
+
export { renderTextRuns, renderVerticalRuns, compositeWarpEffects } from './render/warpText';
|
|
56
|
+
export { bakePsdWarpText, type PsdWarpBake } from './render/psdWarpText';
|
|
57
|
+
export { activePsdTextWarp, generatePsdWarpMesh, generateStyleWarpMesh, applyWarpDistortion, buildWarpSurfaceGridOverWindow, identityWarpMesh, evaluateWarpMesh, invertBilinearCell, isGeneratableWarpStyle, type PsdTextWarpParams, type WarpMeshGrid, type WarpSurfaceGrid } from './core/warpMesh';
|
|
54
58
|
export { type CharContour, type CharTransformation, type Abortable, type TDegree, type TMat2D, type TOriginX, type TOriginY, type TRadian, type XY } from './model/schema';
|
|
55
59
|
export type { SerializedBaseProps, SerializedGradientProps, SerializedPatternProps, SerializedFillerProps, SerializedShadowProps, SerializedFillStrokeProps, SerializedClipPathProps, SerializedObjectPropsBase, SerializedCrossOrigin, SerializedImagePropsBase, SerializedLayoutManagerProps, SerializedGroupPropsBase, ImagePropsBase, GroupPropsBase, SchemaTextStyleDeclaration, SchemaTextStyle, SerializedTextPropsBase, SerializedITextPropsBase, SerializedTextboxPropsBase, SerializedArcTextPropsBase, SerializedRectPropsBase, SerializedLinePropsBase, SerializedPathPropsBase, SerializedMaskPropsBase, SerializedGifPropsBase, PatternOptions } from './model/schema';
|
|
56
60
|
export type { TClassProperties, TCornerPoint, ObjectRef, TextStyleDeclaration, CompleteTextStyleDeclaration, TextStyle, TSize, ITextProps, SerializedRectProps, TBBox, TComplexPathData, TOptions, RectProps, TextboxProps, ImageFormat } from './model/fabricPrimitives';
|
package/dist/index.js
CHANGED
|
@@ -15,6 +15,10 @@ export { buildEngineShapePath } from './model/shapePath';
|
|
|
15
15
|
// image 级图层蒙版烘焙纯函数(批次 103 装载烘焙口径;主仓 PDF worker 导出侧
|
|
16
16
|
// 同式消费——decl 校验/灰度→alpha/offset 回退/矩形外涂黑区,parity 一源)
|
|
17
17
|
export { imageMaskDeclOf, imageMaskOffset, maskOutsideRects, maskPixelsToAlpha } from './model/imageMaskBake';
|
|
18
|
+
// 智能对象置入层透视光栅烘焙(批次 363 自主仓 extension/object/FabricSmartObject.ts 迁居;
|
|
19
|
+
// 主仓 template store 智能对象同步管线消费——parity 一源。后续 PS 智能对象新形态
|
|
20
|
+
// (变形/网格变换)在引擎侧统一承接,GPU 原生透视渲染就绪后经 render/ 落地)
|
|
21
|
+
export { renderPlacedSmartObjectLayer } from './model/smartObjectBake';
|
|
18
22
|
export { attachTableFacade } from './model/tableFacade';
|
|
19
23
|
export { deriveArcCharTransformations } from './model/arcTextTransform';
|
|
20
24
|
export { CompatObject, wrapCompatObject, findParentGroupOf } from './model/compatObject';
|
|
@@ -50,3 +54,6 @@ export { bakeArcText, glyphStyleOf } from './render/arcText';
|
|
|
50
54
|
export { planTextHighlight } from './render/textHighlight';
|
|
51
55
|
export { glyphStyleAt, verticalGlyphOps } from './render/verticalText';
|
|
52
56
|
export { bakeWarpedText, bakeTextEffects } from './render/warpText';
|
|
57
|
+
export { renderTextRuns, renderVerticalRuns, compositeWarpEffects } from './render/warpText';
|
|
58
|
+
export { bakePsdWarpText } from './render/psdWarpText';
|
|
59
|
+
export { activePsdTextWarp, generatePsdWarpMesh, generateStyleWarpMesh, applyWarpDistortion, buildWarpSurfaceGridOverWindow, identityWarpMesh, evaluateWarpMesh, invertBilinearCell, isGeneratableWarpStyle } from './core/warpMesh';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** 置入层 smartObject 装载消费字段子集(应用 SmartObjectInfo 的读位投影;多余字段自然兼容) */
|
|
2
|
+
export interface SmartObjectPlacedDecl {
|
|
3
|
+
width?: number;
|
|
4
|
+
height?: number;
|
|
5
|
+
transform?: number[];
|
|
6
|
+
nonAffineTransform?: number[];
|
|
7
|
+
}
|
|
8
|
+
export interface SmartObjectRenderOptions {
|
|
9
|
+
quality?: number;
|
|
10
|
+
format?: 'webp' | 'png';
|
|
11
|
+
maxGridSize?: number;
|
|
12
|
+
objectBounds?: {
|
|
13
|
+
left: number;
|
|
14
|
+
top: number;
|
|
15
|
+
width: number;
|
|
16
|
+
height: number;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** 置入层光栅结果(src 写回对象即普通图片——引擎/fabric 双渲染路径同口径显示) */
|
|
20
|
+
export interface SmartObjectRenderResult {
|
|
21
|
+
src: string;
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
sourceWidth: number;
|
|
25
|
+
sourceHeight: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* 置入层智能对象光栅:子文档合成画布经 smartObject quad(仿射/透视)烘焙成普通图片。
|
|
29
|
+
* 无 quad(无变换元数据)时源画布原样编码返回。
|
|
30
|
+
*/
|
|
31
|
+
export declare const renderPlacedSmartObjectLayer: (sourceCanvas: HTMLCanvasElement, smartObject: SmartObjectPlacedDecl | undefined, options?: SmartObjectRenderOptions) => Promise<SmartObjectRenderResult>;
|