@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.
@@ -0,0 +1,212 @@
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 { buildWarpSurfaceGridOverWindow, evaluateWarpMesh, generatePsdWarpMesh, invertBilinearCell } from '../core/warpMesh';
19
+ import { compositeWarpEffects, renderTextRuns, renderVerticalRuns, warpEffectExtentPad } from './warpText';
20
+ const CACHE_DPR = 2;
21
+ /** 提交级 cell 尺寸(逻辑像素 ×CACHE_DPR——Patchy bake_warp_into_layer 同款 4px) */
22
+ const MAX_CELL_DOC_PIXELS = 4 * CACHE_DPR;
23
+ const MAX_CELLS = 384;
24
+ /** 探针趟:窗口参数区间经网格前向映射 + 盒→源画布仿射后的输出范围(设备像素) */
25
+ const probeOutputExtent = (mesh, u0, u1, v0, v1, meshToSource) => {
26
+ let minX = Infinity;
27
+ let maxX = -Infinity;
28
+ let minY = Infinity;
29
+ let maxY = -Infinity;
30
+ const probe = 8;
31
+ for (let row = 0; row <= probe; row++) {
32
+ const v = v0 + ((v1 - v0) * row) / probe;
33
+ for (let column = 0; column <= probe; column++) {
34
+ const u = u0 + ((u1 - u0) * column) / probe;
35
+ const surface = evaluateWarpMesh(mesh, u, v);
36
+ const x = meshToSource[0] * surface.x + meshToSource[2] * surface.y + meshToSource[4];
37
+ const y = meshToSource[1] * surface.x + meshToSource[3] * surface.y + meshToSource[5];
38
+ minX = Math.min(minX, x);
39
+ maxX = Math.max(maxX, x);
40
+ minY = Math.min(minY, y);
41
+ maxY = Math.max(maxY, y);
42
+ }
43
+ }
44
+ return { minX, maxX, minY, maxY };
45
+ };
46
+ /** PSD 变形文本整图烘焙(输出窗 = 变形后墨迹范围 + 1px AA 余量,中心偏移随行) */
47
+ export const bakePsdWarpText = (node, params, decls = null) => {
48
+ const layout = node.textLayout;
49
+ const fontSize = Number(node.raw.fontSize) || 40;
50
+ const boxW = typeof node.raw.width === 'number' ? node.raw.width : layout.width;
51
+ const boxH = typeof node.raw.height === 'number' ? node.raw.height : layout.height;
52
+ if (!(boxW > 0) || !(boxH > 0))
53
+ return null;
54
+ const padding = fontSize * 0.6;
55
+ const baseTextWidth = Math.max(layout.width, boxW, fontSize);
56
+ const baseTextHeight = Math.max(boxH, fontSize);
57
+ const fxPad = decls ? warpEffectExtentPad(decls) : 0;
58
+ const sourceW = Math.ceil(baseTextWidth + padding * 2 + fxPad * 2);
59
+ const sourceH = Math.ceil(baseTextHeight + padding * 2 + fxPad * 2);
60
+ const cacheW = sourceW * CACHE_DPR;
61
+ const cacheH = sourceH * CACHE_DPR;
62
+ if (!(cacheW > 0) || !(cacheH > 0) || cacheW > 8192 || cacheH > 8192)
63
+ return null;
64
+ const mesh = generatePsdWarpMesh(params, boxW, boxH);
65
+ if (!mesh)
66
+ return null;
67
+ // 源渲染:中心原点系 ×CACHE_DPR(盒居中于画布中心——对象中心即画布中心)
68
+ const canvas = document.createElement('canvas');
69
+ canvas.width = cacheW;
70
+ canvas.height = cacheH;
71
+ const ctx = canvas.getContext('2d', { willReadFrequently: true });
72
+ if (!ctx)
73
+ return null;
74
+ ctx.setTransform(CACHE_DPR, 0, 0, CACHE_DPR, cacheW / 2, cacheH / 2);
75
+ if (node.raw.vertical === true)
76
+ renderVerticalRuns(ctx, node);
77
+ else
78
+ renderTextRuns(ctx, node);
79
+ compositeWarpEffects(ctx, node, decls);
80
+ // 墨迹扫描(alpha > 10,warpText 同阈值)——重采样窗口裁到墨迹
81
+ const img = ctx.getImageData(0, 0, cacheW, cacheH);
82
+ const px = new Uint32Array(img.data.buffer);
83
+ let minX = cacheW;
84
+ let maxX = 0;
85
+ let minY = cacheH;
86
+ let maxY = 0;
87
+ let hasText = false;
88
+ for (let row = 0; row < cacheH; row++) {
89
+ for (let col = 0; col < cacheW; col++) {
90
+ if (((px[row * cacheW + col] >>> 24) & 0xff) > 10) {
91
+ hasText = true;
92
+ if (col < minX)
93
+ minX = col;
94
+ if (col > maxX)
95
+ maxX = col;
96
+ if (row < minY)
97
+ minY = row;
98
+ if (row > maxY)
99
+ maxY = row;
100
+ }
101
+ }
102
+ }
103
+ if (!hasText)
104
+ return null;
105
+ const srcX0 = minX;
106
+ const srcY0 = minY;
107
+ const srcW = maxX + 1 - minX;
108
+ const srcH = maxY + 1 - minY;
109
+ // 盒(mesh 空间 [0,boxW]×[0,boxH])→ 源画布设备像素的仿射(盒居中于画布)
110
+ const cx0 = ((sourceW - boxW) / 2) * CACHE_DPR;
111
+ const cy0 = ((sourceH - boxH) / 2) * CACHE_DPR;
112
+ const meshToSource = [CACHE_DPR, 0, 0, CACHE_DPR, cx0, cy0];
113
+ // 墨迹窗口(源设备像素)→ mesh 参数区间(可越 [0,1]——Bernstein 外推覆盖出盒墨迹)
114
+ const windowLeft = (srcX0 - cx0) / CACHE_DPR;
115
+ const windowRight = (srcX0 + srcW - cx0) / CACHE_DPR;
116
+ const windowTop = (srcY0 - cy0) / CACHE_DPR;
117
+ const windowBottom = (srcY0 + srcH - cy0) / CACHE_DPR;
118
+ const u0 = windowLeft / boxW;
119
+ const u1 = windowRight / boxW;
120
+ const v0 = windowTop / boxH;
121
+ const v1 = windowBottom / boxH;
122
+ // 输出窗:变形后窗口的设备范围 + 1px AA 余量
123
+ const extent = probeOutputExtent(mesh, u0, u1, v0, v1, meshToSource);
124
+ const outLeft = Math.floor(extent.minX) - 1;
125
+ const outTop = Math.floor(extent.minY) - 1;
126
+ const outW = Math.max(1, Math.ceil(extent.maxX + 1) - outLeft);
127
+ const outH = Math.max(1, Math.ceil(extent.maxY + 1) - outTop);
128
+ if (outW > 8192 || outH > 8192)
129
+ return null;
130
+ // 格点仿射:mesh → 输出画布像素(源仿射平移 -outLeft/-outTop)
131
+ const grid = buildWarpSurfaceGridOverWindow(mesh, 0, 0, boxW, boxH, windowLeft, windowTop, windowRight, windowBottom, srcW, srcH, [CACHE_DPR, 0, 0, CACHE_DPR, cx0 - outLeft, cy0 - outTop], MAX_CELL_DOC_PIXELS, MAX_CELLS);
132
+ if (!grid)
133
+ return null;
134
+ // 逐 cell 光栅化:cell 四边形 bbox 内逐像素闭式逆双线性 → (s,t) → 源像素双线性
135
+ // 采样;first-writer-wins(折叠处首写者胜,Patchy resample_warped_rgba8 同语义)
136
+ const out = ctx.createImageData(outW, outH);
137
+ const outPx = new Uint32Array(out.data.buffer);
138
+ const covered = new Uint8Array(outW * outH);
139
+ const sample = (fx, fy) => {
140
+ const x = Math.min(srcW - 1, Math.max(0, fx));
141
+ const y = Math.min(srcH - 1, Math.max(0, fy));
142
+ const x0 = Math.floor(x);
143
+ const y0 = Math.floor(y);
144
+ const x1 = Math.min(srcW - 1, x0 + 1);
145
+ const y1 = Math.min(srcH - 1, y0 + 1);
146
+ const tx = x - x0;
147
+ const ty = y - y0;
148
+ const a = px[(srcY0 + y0) * cacheW + (srcX0 + x0)];
149
+ const b = px[(srcY0 + y0) * cacheW + (srcX0 + x1)];
150
+ const c = px[(srcY0 + y1) * cacheW + (srcX0 + x1)];
151
+ const d = px[(srcY0 + y1) * cacheW + (srcX0 + x0)];
152
+ const aA = (a >>> 24) & 0xff;
153
+ const bA = (b >>> 24) & 0xff;
154
+ const cA = (c >>> 24) & 0xff;
155
+ const dA = (d >>> 24) & 0xff;
156
+ if (aA <= 10 && bA <= 10 && cA <= 10 && dA <= 10)
157
+ return 0;
158
+ const lerp = (v0, v1, t) => v0 + (v1 - v0) * t;
159
+ const r = Math.round(lerp(lerp(a & 0xff, b & 0xff, tx), lerp(d & 0xff, c & 0xff, tx), ty));
160
+ const g = Math.round(lerp(lerp((a >>> 8) & 0xff, (b >>> 8) & 0xff, tx), lerp((d >>> 8) & 0xff, (c >>> 8) & 0xff, tx), ty));
161
+ const bl = Math.round(lerp(lerp((a >>> 16) & 0xff, (b >>> 16) & 0xff, tx), lerp((d >>> 16) & 0xff, (c >>> 16) & 0xff, tx), ty));
162
+ const al = Math.round(lerp(lerp(aA, bA, tx), lerp(dA, cA, tx), ty));
163
+ return (al << 24) | (bl << 16) | (g << 8) | r;
164
+ };
165
+ for (let row = 0; row + 1 < grid.rows; row++) {
166
+ for (let column = 0; column + 1 < grid.columns; column++) {
167
+ const i00 = row * grid.columns + column;
168
+ const i10 = i00 + 1;
169
+ const i01 = i00 + grid.columns;
170
+ const i11 = i01 + 1;
171
+ const x00 = grid.docXs[i00];
172
+ const y00 = grid.docYs[i00];
173
+ const x10 = grid.docXs[i10];
174
+ const y10 = grid.docYs[i10];
175
+ const x11 = grid.docXs[i11];
176
+ const y11 = grid.docYs[i11];
177
+ const x01 = grid.docXs[i01];
178
+ const y01 = grid.docYs[i01];
179
+ const cellMinX = Math.max(0, Math.floor(Math.min(x00, x10, x11, x01)));
180
+ const cellMaxX = Math.min(outW - 1, Math.ceil(Math.max(x00, x10, x11, x01)));
181
+ const cellMinY = Math.max(0, Math.floor(Math.min(y00, y10, y11, y01)));
182
+ const cellMaxY = Math.min(outH - 1, Math.ceil(Math.max(y00, y10, y11, y01)));
183
+ for (let py = cellMinY; py <= cellMaxY; py++) {
184
+ for (let pxCur = cellMinX; pxCur <= cellMaxX; pxCur++) {
185
+ const index = py * outW + pxCur;
186
+ if (covered[index])
187
+ continue;
188
+ const st = invertBilinearCell(pxCur + 0.5, py + 0.5, x00, y00, x10, y10, x11, y11, x01, y01);
189
+ if (!st)
190
+ continue;
191
+ covered[index] = 1;
192
+ const { s, t } = st;
193
+ // 源坐标 = 四角源坐标的双线性(格点源坐标即 fraction×size,规则网格)
194
+ const sx = (1 - s) * (1 - t) * grid.sourceXs[i00] + s * (1 - t) * grid.sourceXs[i10] + s * t * grid.sourceXs[i11] + (1 - s) * t * grid.sourceXs[i01];
195
+ const sy = (1 - s) * (1 - t) * grid.sourceYs[i00] + s * (1 - t) * grid.sourceYs[i10] + s * t * grid.sourceYs[i11] + (1 - s) * t * grid.sourceYs[i01];
196
+ outPx[index] = sample(sx, sy);
197
+ }
198
+ }
199
+ }
200
+ }
201
+ const outCanvas = document.createElement('canvas');
202
+ outCanvas.width = outW;
203
+ outCanvas.height = outH;
204
+ const outCtx = outCanvas.getContext('2d');
205
+ if (!outCtx)
206
+ return null;
207
+ outCtx.putImageData(out, 0, 0);
208
+ // 输出画布中心相对对象中心(源画布中心)的逻辑偏移
209
+ const offsetX = (outLeft + outW / 2) / CACHE_DPR - sourceW / 2;
210
+ const offsetY = (outTop + outH / 2) / CACHE_DPR - sourceH / 2;
211
+ return { canvas: outCanvas, logicalW: outW / CACHE_DPR, logicalH: outH / CACHE_DPR, offsetX, offsetY };
212
+ };
@@ -25,6 +25,9 @@ export declare const warpTextKind: (raw: {
25
25
  type?: unknown;
26
26
  warpStrength?: unknown;
27
27
  }) => WarpKind | null;
28
+ /** 文本 run(同样式连续段)逐 run 画到 ctx(中心原点本地系,×CACHE_DPR)。
29
+ * 批次 365 起导出:psdWarpText 烘焙复用(PSD 变形文本的未变形源渲染同脚手架) */
30
+ export declare const renderTextRuns: (ctx: CanvasRenderingContext2D, node: TextNode) => void;
28
31
  export interface WarpBake {
29
32
  canvas: HTMLCanvasElement;
30
33
  logicalW: number;
@@ -44,8 +47,18 @@ export declare const bakeWarpShadowLayer: (decls: PsdDropShadowDecl[], source: H
44
47
  w: number;
45
48
  h: number;
46
49
  } | null;
50
+ /** 变形特效合成(fabric _renderText 同序:投影垫底 → run 文本 → solidFill → stroke)。
51
+ * ctx 为变形烘焙画布上下文(中心原点本地系 ×CACHE_DPR);投影在 run 之后合成,
52
+ * 用 destination-over 落到已有文本之下。批次 365 起导出(psdWarpText 复用) */
53
+ export declare const compositeWarpEffects: (ctx: CanvasRenderingContext2D, node: TextNode, decls: PsdEffectsDecls | null) => void;
47
54
  /** 变形文本整图烘焙(本地中心系 logicalW×logicalH,内容居中) */
48
55
  export declare const bakeWarpedText: (node: TextNode, kind: WarpKind, decls?: PsdEffectsDecls | null) => WarpBake | null;
56
+ /** 竖排逐字彩色渲染(shadowBake.drawTextGlyphs 竖排分支的彩色版):摆位/
57
+ * 姿态(verticalGlyphOps + drawVerticalGlyph)与 K-line jitter 同剪影口径,
58
+ * fill/stroke 逐字覆写与 renderTextRuns 同语义(fill 覆写 ?? 对象级、
59
+ * 描边 resolveStrokeDecl;paintFirst 逐字粒度不区分——竖排 quad 管线同款)。
60
+ * 批次 365 起导出(psdWarpText 竖排分支复用) */
61
+ export declare const renderVerticalRuns: (ctx: CanvasRenderingContext2D, node: TextNode) => void;
49
62
  /**
50
63
  * 常规/竖排文本 + PSD 特效整图烘焙(SVG 导出位图路径):横排复用
51
64
  * renderTextRuns、竖排 renderVerticalRuns,特效层 compositeWarpEffects 合成
@@ -34,8 +34,9 @@ export const warpTextKind = (raw) => {
34
34
  return null;
35
35
  };
36
36
  const CACHE_DPR = 2;
37
- /** 文本 run(同样式连续段)逐 run 画到 ctx(中心原点本地系,×CACHE_DPR) */
38
- const renderTextRuns = (ctx, node) => {
37
+ /** 文本 run(同样式连续段)逐 run 画到 ctx(中心原点本地系,×CACHE_DPR)。
38
+ * 批次 365 起导出:psdWarpText 烘焙复用(PSD 变形文本的未变形源渲染同脚手架) */
39
+ export const renderTextRuns = (ctx, node) => {
39
40
  const layout = node.textLayout;
40
41
  const style = node.textStyle;
41
42
  const baseStroke = node.textStroke;
@@ -149,8 +150,8 @@ export const bakeWarpShadowLayer = (decls, source, rs) => {
149
150
  };
150
151
  /** 变形特效合成(fabric _renderText 同序:投影垫底 → run 文本 → solidFill → stroke)。
151
152
  * ctx 为变形烘焙画布上下文(中心原点本地系 ×CACHE_DPR);投影在 run 之后合成,
152
- * 用 destination-over 落到已有文本之下。 */
153
- const compositeWarpEffects = (ctx, node, decls) => {
153
+ * 用 destination-over 落到已有文本之下。批次 365 起导出(psdWarpText 复用) */
154
+ export const compositeWarpEffects = (ctx, node, decls) => {
154
155
  if (!decls)
155
156
  return;
156
157
  if (decls.dropShadows.length === 0 && decls.solidFills.length === 0 && decls.strokes.length === 0)
@@ -309,8 +310,9 @@ export const bakeWarpedText = (node, kind, decls = null) => {
309
310
  /** 竖排逐字彩色渲染(shadowBake.drawTextGlyphs 竖排分支的彩色版):摆位/
310
311
  * 姿态(verticalGlyphOps + drawVerticalGlyph)与 K-line jitter 同剪影口径,
311
312
  * fill/stroke 逐字覆写与 renderTextRuns 同语义(fill 覆写 ?? 对象级、
312
- * 描边 resolveStrokeDecl;paintFirst 逐字粒度不区分——竖排 quad 管线同款)。 */
313
- const renderVerticalRuns = (ctx, node) => {
313
+ * 描边 resolveStrokeDecl;paintFirst 逐字粒度不区分——竖排 quad 管线同款)。
314
+ * 批次 365 起导出(psdWarpText 竖排分支复用) */
315
+ export const renderVerticalRuns = (ctx, node) => {
314
316
  const layout = node.textLayout;
315
317
  const style = node.textStyle;
316
318
  const baseStroke = node.textStroke;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yft-design/engine",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "yft-design 的 WebGL2 画布引擎:接受 fabric toObject 格式 JSON,输出 GL 画布 / 位图(任意倍率 FBO 读回)/ SVG(纯数据序列化器)",
5
5
  "repository": {
6
6
  "type": "git",