@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,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 智能对象置入层透视光栅烘焙(批次 363 自主仓 extension/object/FabricSmartObject.ts 迁居,
|
|
3
|
+
* 逐式 verbatim、仅剥 import.meta.env.DEV 调试日志——引擎构建无 vite env 注入)。
|
|
4
|
+
*
|
|
5
|
+
* PSD placed layer 的 smartObject 元数据携带 8 元组 quad(nonAffineTransform 优先、
|
|
6
|
+
* transform 兜底)——超出仿射的透视形变 fabric 时代无法渲染,主仓把子文档合成画布
|
|
7
|
+
* 经 quad 预光栅成普通图片 src 写回对象(template store 智能对象同步管线唯一消费方,
|
|
8
|
+
* 批次 257/258 建链)。迁居引擎后 parity 一源(同 imageMaskBake 先例):后续 PS 智能
|
|
9
|
+
* 对象新形态(变形/网格变换,warpvas 同族)在引擎侧统一承接;GPU 原生透视渲染
|
|
10
|
+
* (非破坏性编辑,免预光栅)就绪后经 render/ 落地,quad 数学同源复用 core/homography。
|
|
11
|
+
*
|
|
12
|
+
* 口径(迁居零行为变化):
|
|
13
|
+
* - 仿射 quad(br ≈ tr + bl − tl)→ setTransform 直绘;
|
|
14
|
+
* - 透视 quad → 逐像素反投射双线性采样;无 2d 上下文/奇异单应回退网格三角剖分
|
|
15
|
+
* (drawAffineTriangle 逐格 clip + setTransform 近似);
|
|
16
|
+
* - objectBounds 在 quad 外接框内 → 裁剪出 object 可见子区域;跨坐标系(object 在
|
|
17
|
+
* group 内或经 workspace 偏移)→ 整 quad 缩放到 object 尺寸;
|
|
18
|
+
* - 编码优先 canvas.toBlob(Chromium 后台线程编码,大画布 webp 同步编码实测阻塞
|
|
19
|
+
* 主线程 ~1s;主线程只剩 FileReader 读 base64),不可用/返回空回退同步 toDataURL。
|
|
20
|
+
*/
|
|
21
|
+
import { getQuadBounds, getSmartObjectTransformQuad, getSquareToQuadHomography, invertHomography, isAffineQuad, offsetQuad, projectPoint } from '../core/homography';
|
|
22
|
+
const DEFAULT_GRID_SIZE = 32;
|
|
23
|
+
const MAX_GRID_COUNT = 120;
|
|
24
|
+
const EPSILON = 0.000001;
|
|
25
|
+
const createCanvasElement = () => {
|
|
26
|
+
const element = document.createElement('canvas');
|
|
27
|
+
if (!element || typeof element.getContext === 'undefined') {
|
|
28
|
+
throw new Error('Failed to create `canvas` element');
|
|
29
|
+
}
|
|
30
|
+
return element;
|
|
31
|
+
};
|
|
32
|
+
const copyCanvasElement = (canvas) => {
|
|
33
|
+
const newCanvas = createCanvasElement();
|
|
34
|
+
newCanvas.width = canvas.width;
|
|
35
|
+
newCanvas.height = canvas.height;
|
|
36
|
+
newCanvas.getContext('2d', { willReadFrequently: true })?.drawImage(canvas, 0, 0);
|
|
37
|
+
return newCanvas;
|
|
38
|
+
};
|
|
39
|
+
const toDataURL = (canvasEl, format, quality) => canvasEl.toDataURL?.(`image/${format}`, quality);
|
|
40
|
+
const readBlobAsDataURL = (blob) => new Promise((resolve, reject) => {
|
|
41
|
+
const reader = new FileReader();
|
|
42
|
+
reader.onload = () => resolve(String(reader.result));
|
|
43
|
+
reader.onerror = () => reject(reader.error);
|
|
44
|
+
reader.readAsDataURL(blob);
|
|
45
|
+
});
|
|
46
|
+
/** toDataURL 的异步版:优先 canvas.toBlob(离线程编码),不可用/返回空回退同步 toDataURL */
|
|
47
|
+
const toDataURLAsync = async (canvasEl, format, quality) => {
|
|
48
|
+
try {
|
|
49
|
+
const blob = await new Promise((resolve) => canvasEl.toBlob(resolve, `image/${format}`, quality));
|
|
50
|
+
if (blob)
|
|
51
|
+
return await readBlobAsDataURL(blob);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// 跨域污染等异常走同步兜底(同样会抛,由调用方处理)
|
|
55
|
+
}
|
|
56
|
+
return toDataURL(canvasEl, format, quality);
|
|
57
|
+
};
|
|
58
|
+
/** 源画布重采样到 smartObject 记录的文档精确尺寸(canvas 像素可能受 retina 缩放影响) */
|
|
59
|
+
const createLogicalSourceCanvas = (sourceCanvas, width, height) => {
|
|
60
|
+
const canvasWidth = Math.max(1, sourceCanvas.width);
|
|
61
|
+
const canvasHeight = Math.max(1, sourceCanvas.height);
|
|
62
|
+
const logicalWidth = Math.max(1, Math.round(width));
|
|
63
|
+
const logicalHeight = Math.max(1, Math.round(height));
|
|
64
|
+
if (Math.abs(canvasWidth - logicalWidth) < EPSILON && Math.abs(canvasHeight - logicalHeight) < EPSILON) {
|
|
65
|
+
return sourceCanvas;
|
|
66
|
+
}
|
|
67
|
+
const logicalCanvas = document.createElement('canvas');
|
|
68
|
+
logicalCanvas.width = logicalWidth;
|
|
69
|
+
logicalCanvas.height = logicalHeight;
|
|
70
|
+
const ctx = logicalCanvas.getContext('2d');
|
|
71
|
+
if (!ctx)
|
|
72
|
+
return sourceCanvas;
|
|
73
|
+
ctx.imageSmoothingEnabled = true;
|
|
74
|
+
ctx.imageSmoothingQuality = 'high';
|
|
75
|
+
ctx.drawImage(sourceCanvas, 0, 0, canvasWidth, canvasHeight, 0, 0, logicalWidth, logicalHeight);
|
|
76
|
+
return logicalCanvas;
|
|
77
|
+
};
|
|
78
|
+
const sampleBilinear = (sourceData, width, height, x, y) => {
|
|
79
|
+
const sx = Math.max(0, Math.min(width - 1, x));
|
|
80
|
+
const sy = Math.max(0, Math.min(height - 1, y));
|
|
81
|
+
const x0 = Math.floor(sx);
|
|
82
|
+
const y0 = Math.floor(sy);
|
|
83
|
+
const x1 = Math.min(width - 1, x0 + 1);
|
|
84
|
+
const y1 = Math.min(height - 1, y0 + 1);
|
|
85
|
+
const tx = sx - x0;
|
|
86
|
+
const ty = sy - y0;
|
|
87
|
+
const i00 = (y0 * width + x0) * 4;
|
|
88
|
+
const i10 = (y0 * width + x1) * 4;
|
|
89
|
+
const i01 = (y1 * width + x0) * 4;
|
|
90
|
+
const i11 = (y1 * width + x1) * 4;
|
|
91
|
+
const top = 1 - ty;
|
|
92
|
+
const left = 1 - tx;
|
|
93
|
+
const w00 = left * top;
|
|
94
|
+
const w10 = tx * top;
|
|
95
|
+
const w01 = left * ty;
|
|
96
|
+
const w11 = tx * ty;
|
|
97
|
+
return [
|
|
98
|
+
sourceData[i00] * w00 + sourceData[i10] * w10 + sourceData[i01] * w01 + sourceData[i11] * w11,
|
|
99
|
+
sourceData[i00 + 1] * w00 + sourceData[i10 + 1] * w10 + sourceData[i01 + 1] * w01 + sourceData[i11 + 1] * w11,
|
|
100
|
+
sourceData[i00 + 2] * w00 + sourceData[i10 + 2] * w10 + sourceData[i01 + 2] * w01 + sourceData[i11 + 2] * w11,
|
|
101
|
+
sourceData[i00 + 3] * w00 + sourceData[i10 + 3] * w10 + sourceData[i01 + 3] * w01 + sourceData[i11 + 3] * w11
|
|
102
|
+
];
|
|
103
|
+
};
|
|
104
|
+
/** 逐像素反投射透视光栅:输出画布每像素经逆单应映射回源图双线性采样(真透视,非网格近似) */
|
|
105
|
+
const renderPerspectivePixels = (ctx, sourceCanvas, quad) => {
|
|
106
|
+
const homography = getSquareToQuadHomography(quad);
|
|
107
|
+
const inverseHomography = homography ? invertHomography(homography) : undefined;
|
|
108
|
+
if (!inverseHomography) {
|
|
109
|
+
renderPerspectiveMesh(ctx, sourceCanvas, quad, DEFAULT_GRID_SIZE);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const sourceWidth = Math.max(1, sourceCanvas.width);
|
|
113
|
+
const sourceHeight = Math.max(1, sourceCanvas.height);
|
|
114
|
+
const sourceCtx = sourceCanvas.getContext('2d', { willReadFrequently: true });
|
|
115
|
+
if (!sourceCtx) {
|
|
116
|
+
renderPerspectiveMesh(ctx, sourceCanvas, quad, DEFAULT_GRID_SIZE);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const sourceImageData = sourceCtx.getImageData(0, 0, sourceWidth, sourceHeight);
|
|
120
|
+
const outputImageData = ctx.createImageData(ctx.canvas.width, ctx.canvas.height);
|
|
121
|
+
const outputData = outputImageData.data;
|
|
122
|
+
for (let y = 0; y < ctx.canvas.height; y++) {
|
|
123
|
+
for (let x = 0; x < ctx.canvas.width; x++) {
|
|
124
|
+
const sourcePoint = projectPoint(inverseHomography, x + 0.5, y + 0.5);
|
|
125
|
+
if (sourcePoint.x < 0 || sourcePoint.x > 1 || sourcePoint.y < 0 || sourcePoint.y > 1) {
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const sourceX = sourcePoint.x * (sourceWidth - 1);
|
|
129
|
+
const sourceY = sourcePoint.y * (sourceHeight - 1);
|
|
130
|
+
const rgba = sampleBilinear(sourceImageData.data, sourceWidth, sourceHeight, sourceX, sourceY);
|
|
131
|
+
const index = (y * ctx.canvas.width + x) * 4;
|
|
132
|
+
outputData[index] = rgba[0];
|
|
133
|
+
outputData[index + 1] = rgba[1];
|
|
134
|
+
outputData[index + 2] = rgba[2];
|
|
135
|
+
outputData[index + 3] = rgba[3];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
ctx.putImageData(outputImageData, 0, 0);
|
|
139
|
+
};
|
|
140
|
+
/** 仿射三角剖分近似透视:源/目标三角形三点定仿射,clip 后 setTransform 绘源图 */
|
|
141
|
+
const drawAffineTriangle = (ctx, sourceCanvas, source, destination) => {
|
|
142
|
+
const [s0, s1, s2] = source;
|
|
143
|
+
const [d0, d1, d2] = destination;
|
|
144
|
+
const denominator = s0.x * (s1.y - s2.y) + s1.x * (s2.y - s0.y) + s2.x * (s0.y - s1.y);
|
|
145
|
+
if (Math.abs(denominator) < EPSILON)
|
|
146
|
+
return;
|
|
147
|
+
const a = (d0.x * (s1.y - s2.y) + d1.x * (s2.y - s0.y) + d2.x * (s0.y - s1.y)) / denominator;
|
|
148
|
+
const b = (d0.y * (s1.y - s2.y) + d1.y * (s2.y - s0.y) + d2.y * (s0.y - s1.y)) / denominator;
|
|
149
|
+
const c = (d0.x * (s2.x - s1.x) + d1.x * (s0.x - s2.x) + d2.x * (s1.x - s0.x)) / denominator;
|
|
150
|
+
const d = (d0.y * (s2.x - s1.x) + d1.y * (s0.x - s2.x) + d2.y * (s1.x - s0.x)) / denominator;
|
|
151
|
+
const e = (d0.x * (s1.x * s2.y - s2.x * s1.y) + d1.x * (s2.x * s0.y - s0.x * s2.y) + d2.x * (s0.x * s1.y - s1.x * s0.y)) / denominator;
|
|
152
|
+
const f = (d0.y * (s1.x * s2.y - s2.x * s1.y) + d1.y * (s2.x * s0.y - s0.x * s2.y) + d2.y * (s0.x * s1.y - s1.x * s0.y)) / denominator;
|
|
153
|
+
ctx.save();
|
|
154
|
+
ctx.beginPath();
|
|
155
|
+
ctx.moveTo(d0.x, d0.y);
|
|
156
|
+
ctx.lineTo(d1.x, d1.y);
|
|
157
|
+
ctx.lineTo(d2.x, d2.y);
|
|
158
|
+
ctx.closePath();
|
|
159
|
+
ctx.clip();
|
|
160
|
+
ctx.setTransform(a, b, c, d, e, f);
|
|
161
|
+
ctx.drawImage(sourceCanvas, 0, 0);
|
|
162
|
+
ctx.restore();
|
|
163
|
+
};
|
|
164
|
+
/** 网格三角剖分兜底(无 2d 读位/奇异单应时):源面按 maxGridSize 分格逐格双三角仿射近似 */
|
|
165
|
+
const renderPerspectiveMesh = (ctx, sourceCanvas, quad, maxGridSize) => {
|
|
166
|
+
const homography = getSquareToQuadHomography(quad);
|
|
167
|
+
if (!homography) {
|
|
168
|
+
ctx.drawImage(sourceCanvas, 0, 0, ctx.canvas.width, ctx.canvas.height);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const sourceWidth = Math.max(1, sourceCanvas.width);
|
|
172
|
+
const sourceHeight = Math.max(1, sourceCanvas.height);
|
|
173
|
+
const columns = Math.min(MAX_GRID_COUNT, Math.max(1, Math.ceil(sourceWidth / maxGridSize)));
|
|
174
|
+
const rows = Math.min(MAX_GRID_COUNT, Math.max(1, Math.ceil(sourceHeight / maxGridSize)));
|
|
175
|
+
for (let row = 0; row < rows; row++) {
|
|
176
|
+
const y0 = row / rows;
|
|
177
|
+
const y1 = (row + 1) / rows;
|
|
178
|
+
const sy0 = y0 * sourceHeight;
|
|
179
|
+
const sy1 = y1 * sourceHeight;
|
|
180
|
+
for (let col = 0; col < columns; col++) {
|
|
181
|
+
const x0 = col / columns;
|
|
182
|
+
const x1 = (col + 1) / columns;
|
|
183
|
+
const sx0 = x0 * sourceWidth;
|
|
184
|
+
const sx1 = x1 * sourceWidth;
|
|
185
|
+
const p00 = projectPoint(homography, x0, y0);
|
|
186
|
+
const p10 = projectPoint(homography, x1, y0);
|
|
187
|
+
const p11 = projectPoint(homography, x1, y1);
|
|
188
|
+
const p01 = projectPoint(homography, x0, y1);
|
|
189
|
+
drawAffineTriangle(ctx, sourceCanvas, [
|
|
190
|
+
{ x: sx0, y: sy0 },
|
|
191
|
+
{ x: sx1, y: sy0 },
|
|
192
|
+
{ x: sx1, y: sy1 }
|
|
193
|
+
], [p00, p10, p11]);
|
|
194
|
+
drawAffineTriangle(ctx, sourceCanvas, [
|
|
195
|
+
{ x: sx0, y: sy0 },
|
|
196
|
+
{ x: sx1, y: sy1 },
|
|
197
|
+
{ x: sx0, y: sy1 }
|
|
198
|
+
], [p00, p11, p01]);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
/**
|
|
203
|
+
* 置入层智能对象光栅:子文档合成画布经 smartObject quad(仿射/透视)烘焙成普通图片。
|
|
204
|
+
* 无 quad(无变换元数据)时源画布原样编码返回。
|
|
205
|
+
*/
|
|
206
|
+
export const renderPlacedSmartObjectLayer = async (sourceCanvas, smartObject, options = {}) => {
|
|
207
|
+
const quality = options.quality ?? 0.92;
|
|
208
|
+
const format = options.format || 'webp';
|
|
209
|
+
const canvasWidth = Math.max(1, sourceCanvas.width);
|
|
210
|
+
const canvasHeight = Math.max(1, sourceCanvas.height);
|
|
211
|
+
// 优先使用 smartObject 存储的内部文档尺寸,而非 canvas 像素尺寸
|
|
212
|
+
// canvas 尺寸可能受 retina 缩放等因素影响,而 smartObject.width/height 是 PSD 中记录的精确值
|
|
213
|
+
const sourceWidth = Math.max(1, smartObject?.width || canvasWidth);
|
|
214
|
+
const sourceHeight = Math.max(1, smartObject?.height || canvasHeight);
|
|
215
|
+
const logicalSourceCanvas = createLogicalSourceCanvas(sourceCanvas, sourceWidth, sourceHeight);
|
|
216
|
+
const quad = getSmartObjectTransformQuad(smartObject?.nonAffineTransform || smartObject?.transform);
|
|
217
|
+
if (!quad) {
|
|
218
|
+
const canvas = copyCanvasElement(sourceCanvas);
|
|
219
|
+
return {
|
|
220
|
+
src: await toDataURLAsync(canvas, format, quality),
|
|
221
|
+
width: canvasWidth,
|
|
222
|
+
height: canvasHeight,
|
|
223
|
+
sourceWidth,
|
|
224
|
+
sourceHeight
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
const quadBounds = getQuadBounds(quad);
|
|
228
|
+
const localQuad = offsetQuad(quad, -quadBounds.minX, -quadBounds.minY);
|
|
229
|
+
const objectBounds = options.objectBounds;
|
|
230
|
+
// 在 quadBounds 尺寸的 canvas 上完整渲染
|
|
231
|
+
const quadCanvas = document.createElement('canvas');
|
|
232
|
+
quadCanvas.width = quadBounds.width;
|
|
233
|
+
quadCanvas.height = quadBounds.height;
|
|
234
|
+
const ctx = quadCanvas.getContext('2d');
|
|
235
|
+
if (!ctx)
|
|
236
|
+
throw new Error('无法创建智能对象渲染画布');
|
|
237
|
+
ctx.imageSmoothingEnabled = true;
|
|
238
|
+
ctx.imageSmoothingQuality = 'high';
|
|
239
|
+
if (isAffineQuad(localQuad)) {
|
|
240
|
+
const a = (localQuad.tr.x - localQuad.tl.x) / sourceWidth;
|
|
241
|
+
const b = (localQuad.tr.y - localQuad.tl.y) / sourceWidth;
|
|
242
|
+
const c = (localQuad.bl.x - localQuad.tl.x) / sourceHeight;
|
|
243
|
+
const d = (localQuad.bl.y - localQuad.tl.y) / sourceHeight;
|
|
244
|
+
const e = localQuad.tl.x;
|
|
245
|
+
const f = localQuad.tl.y;
|
|
246
|
+
ctx.setTransform(a, b, c, d, e, f);
|
|
247
|
+
ctx.drawImage(logicalSourceCanvas, 0, 0);
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
renderPerspectivePixels(ctx, logicalSourceCanvas, localQuad);
|
|
251
|
+
}
|
|
252
|
+
// 决定输出策略:裁剪还是缩放
|
|
253
|
+
let outputCanvas;
|
|
254
|
+
if (objectBounds) {
|
|
255
|
+
const cropX = Math.round(objectBounds.left - quadBounds.minX);
|
|
256
|
+
const cropY = Math.round(objectBounds.top - quadBounds.minY);
|
|
257
|
+
const cropW = Math.max(1, Math.round(objectBounds.width));
|
|
258
|
+
const cropH = Math.max(1, Math.round(objectBounds.height));
|
|
259
|
+
// 判断 object 是否在 quad 内部(同一坐标系下)
|
|
260
|
+
const canCrop = cropX >= 0 && cropY >= 0 && cropX + cropW <= quadBounds.width + 1 && cropY + cropH <= quadBounds.height + 1;
|
|
261
|
+
if (canCrop) {
|
|
262
|
+
// object 是 quad 的子区域 → 裁剪出 object 可见部分
|
|
263
|
+
outputCanvas = document.createElement('canvas');
|
|
264
|
+
outputCanvas.width = cropW;
|
|
265
|
+
outputCanvas.height = cropH;
|
|
266
|
+
const cropCtx = outputCanvas.getContext('2d');
|
|
267
|
+
if (cropCtx) {
|
|
268
|
+
cropCtx.drawImage(quadCanvas, cropX, cropY, cropW, cropH, 0, 0, cropW, cropH);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
// 坐标不在同一空间(object 在 group 内或经过 workspace 偏移)→ 缩放整个 quad 到 object 尺寸
|
|
273
|
+
outputCanvas = document.createElement('canvas');
|
|
274
|
+
outputCanvas.width = cropW;
|
|
275
|
+
outputCanvas.height = cropH;
|
|
276
|
+
const scaleCtx = outputCanvas.getContext('2d');
|
|
277
|
+
if (scaleCtx) {
|
|
278
|
+
scaleCtx.imageSmoothingEnabled = true;
|
|
279
|
+
scaleCtx.imageSmoothingQuality = 'high';
|
|
280
|
+
scaleCtx.drawImage(quadCanvas, 0, 0, quadBounds.width, quadBounds.height, 0, 0, cropW, cropH);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
outputCanvas = quadCanvas;
|
|
286
|
+
}
|
|
287
|
+
return {
|
|
288
|
+
// toBlob 离线程编码:2267×2333 webp 同步编码实测阻塞主线程 ~1s,异步化后主线程只剩 FileReader
|
|
289
|
+
src: await toDataURLAsync(outputCanvas, format, quality),
|
|
290
|
+
width: outputCanvas.width,
|
|
291
|
+
height: outputCanvas.height,
|
|
292
|
+
sourceWidth,
|
|
293
|
+
sourceHeight
|
|
294
|
+
};
|
|
295
|
+
};
|
|
@@ -5,6 +5,7 @@ import { imageFiltersKey, parseImageFilters } from '../core/imageFilters';
|
|
|
5
5
|
import { imageCornerRadius, rectCornerRadii } from '../core/roundedRect';
|
|
6
6
|
import { AnimatedNode, GroupNode, MaskNode, PuzzleNode, rasterMaskSrc, textBitmapFillAlpha } from '../model/scene';
|
|
7
7
|
import { warpTextKind } from './warpText';
|
|
8
|
+
import { activePsdTextWarp } from '../core/warpMesh';
|
|
8
9
|
import { arcTextKind } from '../model/arcLayout';
|
|
9
10
|
import { GLIsolator } from './GLIsolate';
|
|
10
11
|
import { GLBlender } from './GLBlend';
|
|
@@ -319,6 +320,15 @@ export class SceneRenderer {
|
|
|
319
320
|
this.text.renderWarpedText(node, warp);
|
|
320
321
|
return;
|
|
321
322
|
}
|
|
323
|
+
// 批次 365 PSD 文字变形:分派序在 renderSrc 位图(上分支)与 warp 三族之后
|
|
324
|
+
// ——TextImage 编辑转正(防线删 renderSrc、type 落 Textbox)后 raw.psdWarp
|
|
325
|
+
// 仍在场 → 参数化网格变形接管(「编辑后仍保持变形」);纯文本回退层带
|
|
326
|
+
// psdWarp 立即原生变形。被编辑的本节点走常规管线(PS 同构:编辑未变形文本)
|
|
327
|
+
const psdWarp = activePsdTextWarp(node.raw);
|
|
328
|
+
if (psdWarp && node !== this.host.editingNode) {
|
|
329
|
+
this.text.renderPsdWarpedText(node, psdWarp);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
322
332
|
// P4-5b 弧形/圆形文本:ArcText/CircleText 逐字摆位(curvature=0 也走摆位
|
|
323
333
|
// 机——radius=1e5 的近直渲染);双实现(GPU 批次 4)——逐字图集 quad /
|
|
324
334
|
// 整图烘焙,zoom×dpr 供桶分派。被编辑的本节点走常规管线。分派在
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* 不可做模块级缓存。编辑态门控(变形文本在编辑中走常规管线)留在
|
|
9
9
|
* SceneRenderer 的分派分支。
|
|
10
10
|
*/
|
|
11
|
+
import type { PsdTextWarpParams } from '../core/warpMesh';
|
|
11
12
|
import type { SceneNode, TextNode } from '../model/scene';
|
|
12
13
|
import { GlyphAtlas } from './GlyphAtlas';
|
|
13
14
|
import { QuadBatch } from './QuadBatch';
|
|
@@ -19,6 +20,9 @@ export declare class TextRenderer {
|
|
|
19
20
|
private readonly glyphAtlas;
|
|
20
21
|
/** 变形文本烘焙缓存(P4-5a:节点 → {sig, tex, w, h}) */
|
|
21
22
|
private warpBakes;
|
|
23
|
+
/** PSD 变形文本烘焙缓存(批次 365:节点 → {sig, tex, w, h, offX, offY}——
|
|
24
|
+
* 变形输出窗一般不对称于对象中心,quad 定位携带中心偏移) */
|
|
25
|
+
private psdWarpBakes;
|
|
22
26
|
/** 弧形/圆形文本烘焙缓存(P4-5b:节点 → {sig, tex, w, h}) */
|
|
23
27
|
private arcBakes;
|
|
24
28
|
/** 虚线文本描边烘焙缓存(run 级栅格化;签名 = 文本+布局+描边+桶) */
|
|
@@ -38,6 +42,14 @@ export declare class TextRenderer {
|
|
|
38
42
|
invalidateNodeBakes(node: SceneNode): void;
|
|
39
43
|
/** 变形文本渲染(P4-5a):烘焙整图 quad(logicalW×logicalH 居中于对象中心) */
|
|
40
44
|
renderWarpedText(node: TextNode, kind: WarpKind): void;
|
|
45
|
+
/**
|
|
46
|
+
* PSD 变形文本渲染(批次 365):renderSrc 位图优先链的编辑后承接——转正防线
|
|
47
|
+
* 删 renderSrc 后 raw.psdWarp 在场 → 布局盒重排 + 15 样式网格变形烘焙单 quad
|
|
48
|
+
* (bakePsdWarpText:Patchy 前向格点 + 逐 cell 逆双线性)。签名键与 warp 族
|
|
49
|
+
* 同源 + psdWarp 参数 JSON + 排版扩展键(charSpacing/lineHeight/vertical——
|
|
50
|
+
* 转正后重排的文本这些键都会改墨迹)。
|
|
51
|
+
*/
|
|
52
|
+
renderPsdWarpedText(node: TextNode, params: PsdTextWarpParams): void;
|
|
41
53
|
/**
|
|
42
54
|
* 弧形/圆形文本渲染(P4-5b):双实现分发(GPU 迁移批次 4)——
|
|
43
55
|
* 逐字图集 quad(缩放桶重栅格化,放大始终清晰)/ 整图烘焙 quad(Canvas 参考
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* SceneRenderer 的分派分支。
|
|
10
10
|
*/
|
|
11
11
|
import { parseColor } from '../core/color';
|
|
12
|
-
import { multiply } from '../core/mat2d';
|
|
12
|
+
import { composeMatrix, multiply } from '../core/mat2d';
|
|
13
13
|
import { parsePsdEffects, psdEffectsKey } from '../core/psdEffects';
|
|
14
14
|
import { isArcGlyphEnabled } from '../engineFlag';
|
|
15
15
|
import { charJitterY, drawParagraphPrefix, fontDeclaration, graphemeSplit, lineDecorationRuns, maxCharJitterY, resolveStrokeDecl, verticalAnchorShiftX, verticalAnchorShiftY, verticalCharClass, verticalRenderAdvance } from '../model/textLayout';
|
|
@@ -18,6 +18,7 @@ import { bakeScrollText, scrollTextDecl } from './scrollText';
|
|
|
18
18
|
import { bakeTextHighlight, planTextHighlight } from './textHighlight';
|
|
19
19
|
import { drawVerticalGlyph, glyphStyleAt, verticalGlyphOps } from './verticalText';
|
|
20
20
|
import { bakeWarpedText } from './warpText';
|
|
21
|
+
import { bakePsdWarpText } from './psdWarpText';
|
|
21
22
|
import { bakeArcText, glyphStyleOf } from './arcText';
|
|
22
23
|
import { computeArcTextLayout } from '../model/arcLayout';
|
|
23
24
|
import { createTexture2D } from './textureUpload';
|
|
@@ -50,6 +51,9 @@ export class TextRenderer {
|
|
|
50
51
|
glyphAtlas;
|
|
51
52
|
/** 变形文本烘焙缓存(P4-5a:节点 → {sig, tex, w, h}) */
|
|
52
53
|
warpBakes = new WeakMap();
|
|
54
|
+
/** PSD 变形文本烘焙缓存(批次 365:节点 → {sig, tex, w, h, offX, offY}——
|
|
55
|
+
* 变形输出窗一般不对称于对象中心,quad 定位携带中心偏移) */
|
|
56
|
+
psdWarpBakes = new WeakMap();
|
|
53
57
|
/** 弧形/圆形文本烘焙缓存(P4-5b:节点 → {sig, tex, w, h}) */
|
|
54
58
|
arcBakes = new WeakMap();
|
|
55
59
|
/** 虚线文本描边烘焙缓存(run 级栅格化;签名 = 文本+布局+描边+桶) */
|
|
@@ -71,7 +75,7 @@ export class TextRenderer {
|
|
|
71
75
|
* 烘焙随节点 GC,与既有 sig 更换/删除路径同语义)。
|
|
72
76
|
*/
|
|
73
77
|
invalidateNodeBakes(node) {
|
|
74
|
-
for (const bakes of [this.warpBakes, this.arcBakes, this.textDashBakes, this.scrollBakes, this.highlightBakes]) {
|
|
78
|
+
for (const bakes of [this.warpBakes, this.psdWarpBakes, this.arcBakes, this.textDashBakes, this.scrollBakes, this.highlightBakes]) {
|
|
75
79
|
const bake = bakes.get(node);
|
|
76
80
|
if (bake?.tex)
|
|
77
81
|
this.gl.deleteTexture(bake.tex);
|
|
@@ -119,6 +123,55 @@ export class TextRenderer {
|
|
|
119
123
|
}
|
|
120
124
|
this.batch.pushQuad(node.matrix, bake.w, bake.h, [1, 1, 1, node.renderOpacity], bake.tex);
|
|
121
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* PSD 变形文本渲染(批次 365):renderSrc 位图优先链的编辑后承接——转正防线
|
|
128
|
+
* 删 renderSrc 后 raw.psdWarp 在场 → 布局盒重排 + 15 样式网格变形烘焙单 quad
|
|
129
|
+
* (bakePsdWarpText:Patchy 前向格点 + 逐 cell 逆双线性)。签名键与 warp 族
|
|
130
|
+
* 同源 + psdWarp 参数 JSON + 排版扩展键(charSpacing/lineHeight/vertical——
|
|
131
|
+
* 转正后重排的文本这些键都会改墨迹)。
|
|
132
|
+
*/
|
|
133
|
+
renderPsdWarpedText(node, params) {
|
|
134
|
+
const fxDecls = parsePsdEffects(node.raw.effects, { text: true });
|
|
135
|
+
const sig = [
|
|
136
|
+
node.raw.type,
|
|
137
|
+
node.raw.text,
|
|
138
|
+
JSON.stringify(node.raw.psdWarp ?? null),
|
|
139
|
+
node.raw.fontSize,
|
|
140
|
+
node.raw.fontFamily,
|
|
141
|
+
node.raw.fontWeight,
|
|
142
|
+
node.raw.fontStyle,
|
|
143
|
+
node.raw.width,
|
|
144
|
+
node.raw.height,
|
|
145
|
+
node.raw.lineHeight,
|
|
146
|
+
node.raw.charSpacing,
|
|
147
|
+
node.raw.vertical === true ? 1 : 0,
|
|
148
|
+
node.raw.fill,
|
|
149
|
+
node.raw.stroke,
|
|
150
|
+
node.raw.strokeWidth,
|
|
151
|
+
JSON.stringify(node.raw.styles ?? null),
|
|
152
|
+
JSON.stringify(node.raw.paragraph ?? null),
|
|
153
|
+
fxDecls ? psdEffectsKey(fxDecls) : ''
|
|
154
|
+
].join(';');
|
|
155
|
+
let bake = this.psdWarpBakes.get(node);
|
|
156
|
+
if (bake && bake.sig !== sig) {
|
|
157
|
+
this.gl.deleteTexture(bake.tex);
|
|
158
|
+
bake = undefined;
|
|
159
|
+
}
|
|
160
|
+
if (!bake) {
|
|
161
|
+
const result = bakePsdWarpText(node, params, fxDecls);
|
|
162
|
+
if (!result) {
|
|
163
|
+
this.psdWarpBakes.set(node, undefined);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const tex = createTexture2D(this.gl, result.canvas);
|
|
167
|
+
if (!tex)
|
|
168
|
+
return;
|
|
169
|
+
bake = { sig, tex, w: result.logicalW, h: result.logicalH, offX: result.offsetX, offY: result.offsetY };
|
|
170
|
+
this.psdWarpBakes.set(node, bake);
|
|
171
|
+
}
|
|
172
|
+
const m = bake.offX !== 0 || bake.offY !== 0 ? multiply(node.matrix, composeMatrix({ translateX: bake.offX, translateY: bake.offY })) : node.matrix;
|
|
173
|
+
this.batch.pushQuad(m, bake.w, bake.h, [1, 1, 1, node.renderOpacity], bake.tex);
|
|
174
|
+
}
|
|
122
175
|
/**
|
|
123
176
|
* 弧形/圆形文本渲染(P4-5b):双实现分发(GPU 迁移批次 4)——
|
|
124
177
|
* 逐字图集 quad(缩放桶重栅格化,放大始终清晰)/ 整图烘焙 quad(Canvas 参考
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PSD 文字变形(Warp Text)CPU 烘焙(批次 365)——core/warpMesh 的 15 样式控制
|
|
3
|
+
* 网格 × Patchy「前向格点 + 逐 cell 闭式逆双线性」重采样(4px 提交级 cell,
|
|
4
|
+
* first-writer-wins 折叠语义)。
|
|
5
|
+
*
|
|
6
|
+
* 消费链(SceneRenderer 分派序):TextImage 位图态 renderSrc 单 quad 优先(导入
|
|
7
|
+
* 保真)→ 编辑转正防线删 renderSrc 后 raw.psdWarp 在场 → 本件接管(「编辑后仍
|
|
8
|
+
* 保持变形」);纯文本回退层(导入无图层像素)带 psdWarp 立即原生变形。变形作用
|
|
9
|
+
* 于布局盒(PS bounds 盒语义——PS 每次重排自行重推盒,本件同样按节点当前盒实时
|
|
10
|
+
* 求值不落盘);重采样窗口裁到墨迹(Patchy wt_* 同款——近空盒的 Bernstein 外推
|
|
11
|
+
* 会把格点分辨率饿死在墨迹之外)。
|
|
12
|
+
*
|
|
13
|
+
* 脚手架与 render/warpText.ts 同源:run 渲染(CACHE_DPR=2 中心原点系)+ PSD 特效
|
|
14
|
+
* 合成(compositeWarpEffects)→ alpha>10 墨迹扫描 → 网格变形重采样 → putImageData。
|
|
15
|
+
* 输出 = 变形窗口外接矩形 + 画布中心相对对象中心的逻辑偏移(变形输出窗一般不对称
|
|
16
|
+
* 于对象中心——quad/SVG 定位须携带,arch 族的恒居中约定在此不成立)。
|
|
17
|
+
*/
|
|
18
|
+
import { type PsdTextWarpParams } from '../core/warpMesh';
|
|
19
|
+
import type { TextNode } from '../model/scene';
|
|
20
|
+
import type { PsdEffectsDecls } from '../core/psdEffects';
|
|
21
|
+
import { type WarpBake } from './warpText';
|
|
22
|
+
/** PSD 变形烘焙产物:WarpBake 契约 + 输出窗中心的逻辑偏移(quad 定位用) */
|
|
23
|
+
export interface PsdWarpBake extends WarpBake {
|
|
24
|
+
offsetX: number;
|
|
25
|
+
offsetY: number;
|
|
26
|
+
}
|
|
27
|
+
/** PSD 变形文本整图烘焙(输出窗 = 变形后墨迹范围 + 1px AA 余量,中心偏移随行) */
|
|
28
|
+
export declare const bakePsdWarpText: (node: TextNode, params: PsdTextWarpParams, decls?: PsdEffectsDecls | null) => PsdWarpBake | null;
|