@yft-design/engine 0.0.9 → 0.0.11

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.
Files changed (49) hide show
  1. package/dist/EngineCanvas.d.ts +9 -0
  2. package/dist/EngineCanvas.js +11 -0
  3. package/dist/debug/seed.d.ts +2 -1
  4. package/dist/debug/seed.js +8 -5
  5. package/dist/engineFlag.d.ts +18 -0
  6. package/dist/engineFlag.js +18 -0
  7. package/dist/export/SceneExporter.d.ts +4 -0
  8. package/dist/export/SceneExporter.js +6 -0
  9. package/dist/export/glExport.d.ts +4 -0
  10. package/dist/export/svgExport.js +12 -9
  11. package/dist/index.d.ts +4 -0
  12. package/dist/index.js +4 -0
  13. package/dist/interact/tableCellEditing.js +2 -1
  14. package/dist/model/scene.d.ts +1 -1
  15. package/dist/model/scene.js +4 -3
  16. package/dist/model/tableLayout.js +3 -2
  17. package/dist/model/textAlign.d.ts +37 -0
  18. package/dist/model/textAlign.js +36 -0
  19. package/dist/model/textLayout.d.ts +11 -4
  20. package/dist/model/textLayout.js +46 -26
  21. package/dist/render/QuadBatch.d.ts +15 -5
  22. package/dist/render/QuadBatch.js +80 -9
  23. package/dist/render/SceneRenderer.d.ts +23 -1
  24. package/dist/render/SceneRenderer.js +18 -3
  25. package/dist/render/TextRenderer.js +10 -4
  26. package/dist/render/bakeGate.d.ts +48 -0
  27. package/dist/render/bakeGate.js +32 -0
  28. package/dist/render/imageEffects.d.ts +3 -1
  29. package/dist/render/imageEffects.js +30 -5
  30. package/dist/render/lineDecorationSdf.d.ts +63 -0
  31. package/dist/render/lineDecorationSdf.js +188 -0
  32. package/dist/render/maskRender.d.ts +2 -1
  33. package/dist/render/maskRender.js +6 -7
  34. package/dist/render/psdEffectPainter.d.ts +13 -1
  35. package/dist/render/psdEffectPainter.js +37 -11
  36. package/dist/render/scrollText.js +8 -3
  37. package/dist/render/segmentTable.d.ts +53 -0
  38. package/dist/render/segmentTable.js +117 -0
  39. package/dist/render/shaders.d.ts +4 -2
  40. package/dist/render/shaders.js +142 -6
  41. package/dist/render/shadowPainter.d.ts +7 -1
  42. package/dist/render/shadowPainter.js +15 -2
  43. package/dist/render/strokeMiterOutline.d.ts +39 -0
  44. package/dist/render/strokeMiterOutline.js +224 -0
  45. package/dist/render/strokePainter.d.ts +20 -1
  46. package/dist/render/strokePainter.js +107 -15
  47. package/dist/render/strokeSdf.d.ts +41 -11
  48. package/dist/render/strokeSdf.js +198 -11
  49. package/package.json +1 -1
@@ -0,0 +1,188 @@
1
+ import { isStrokeSdfDashEnabled, isStrokeSdfLineDecorationEnabled } from '../engineFlag';
2
+ import { segHash } from './segmentTable';
3
+ /** 描边码位段(与 strokeSdf.ts/shaders.ts 解码一致) */
4
+ const KIND_LINE = 2;
5
+ const KIND_SEGMENT_FILL = 4;
6
+ const CAP_INDEX = { butt: 0, round: 1, square: 2 };
7
+ /** AA 余量(设备像素折算本地单位)——与 strokeSdf.ts 同式(minAxis 最劣方向) */
8
+ const localPerDevicePxOf = (m, zoom, dpr) => {
9
+ const k = Math.max(1e-6, zoom * dpr);
10
+ const axis0 = Math.hypot(m[0], m[1]);
11
+ const axis1 = Math.hypot(m[2], m[3]);
12
+ const minAxis = Math.max(Math.min(axis0, axis1), 1e-6);
13
+ return { aa: 1 / (minAxis * k), minAxis };
14
+ };
15
+ /** dash 三元(两段 pattern butt/square 帽非 uniform——与 strokeSdf.ts line 放行同规;
16
+ * 不合形态返回 false 表示该线片不可直绘(调用方整元素回落) */
17
+ const lineDashTriple = (decl) => {
18
+ if (!decl.dashArray)
19
+ return { dash: null, ok: true };
20
+ if (!isStrokeSdfDashEnabled())
21
+ return { dash: null, ok: false };
22
+ if (decl.dashArray.length > 2)
23
+ return { dash: null, ok: false };
24
+ if (decl.lineCap !== 'butt' && decl.lineCap !== 'square')
25
+ return { dash: null, ok: false };
26
+ if (decl.uniform)
27
+ return { dash: null, ok: false };
28
+ const on = decl.dashArray[0];
29
+ const off = decl.dashArray.length > 1 ? decl.dashArray[1] : on;
30
+ if (on + off <= 0)
31
+ return { dash: null, ok: true };
32
+ return { dash: [on, on + off, decl.dashOffset], ok: true };
33
+ };
34
+ /** 线片打包(片中心系):中心=中点、quad=|dx|×|dy|+2×(带余量+AA)。uniform
35
+ * 笔宽带余量=halfW/minAxis(strokeSdf.ts 同式——着色器像素空间换算的最劣界) */
36
+ const linePiece = (x1, y1, x2, y2, halfW, capSlot, penUniform, dash, aa, minAxis) => {
37
+ const cx = (x1 + x2) / 2;
38
+ const cy = (y1 + y2) / 2;
39
+ const margin = penUniform ? halfW / minAxis : halfW;
40
+ const quadW = Math.abs(x2 - x1) + 2 * (margin + aa);
41
+ const quadH = Math.abs(y2 - y1) + 2 * (margin + aa);
42
+ return {
43
+ centerX: cx,
44
+ centerY: cy,
45
+ shape: [x1 - cx, y1 - cy, x2 - cx, y2 - cy],
46
+ halfW,
47
+ code: -(KIND_LINE + (capSlot << 3) + (penUniform ? 64 : 0)),
48
+ quadW,
49
+ quadH,
50
+ dash,
51
+ segments: null
52
+ };
53
+ };
54
+ /** 三角头片(段表填充):verts 三点闭合轮廓,quad=bbox+2×AA(实心无带余量) */
55
+ const trianglePiece = (verts, aa) => {
56
+ let minX = Infinity;
57
+ let minY = Infinity;
58
+ let maxX = -Infinity;
59
+ let maxY = -Infinity;
60
+ for (const v of verts) {
61
+ if (minX > v.x)
62
+ minX = v.x;
63
+ if (minY > v.y)
64
+ minY = v.y;
65
+ if (maxX < v.x)
66
+ maxX = v.x;
67
+ if (maxY < v.y)
68
+ maxY = v.y;
69
+ }
70
+ const cx = (minX + maxX) / 2;
71
+ const cy = (minY + maxY) / 2;
72
+ const loop = [verts[0], verts[1], verts[2], verts[0]];
73
+ const data = new Float32Array(3 * 4);
74
+ for (let i = 0; i < 3; i++) {
75
+ data[i * 4] = loop[i].x - cx;
76
+ data[i * 4 + 1] = loop[i].y - cy;
77
+ data[i * 4 + 2] = loop[i + 1].x - cx;
78
+ data[i * 4 + 3] = loop[i + 1].y - cy;
79
+ }
80
+ return {
81
+ centerX: cx,
82
+ centerY: cy,
83
+ shape: [0, 0, 0, 0],
84
+ halfW: 0,
85
+ code: -(KIND_SEGMENT_FILL + (1 << 3)),
86
+ quadW: maxX - minX + 2 * aa,
87
+ quadH: maxY - minY + 2 * aa,
88
+ dash: null,
89
+ segments: { data, count: 3, hash: segHash(data) }
90
+ };
91
+ };
92
+ const readPoints = (raw) => {
93
+ const x1 = Number(raw.x1);
94
+ const y1 = Number(raw.y1);
95
+ const x2 = Number(raw.x2);
96
+ const y2 = Number(raw.y2);
97
+ if (!Number.isFinite(x1) || !Number.isFinite(y1) || !Number.isFinite(x2) || !Number.isFinite(y2))
98
+ return null;
99
+ if (x1 === x2 && y1 === y2)
100
+ return null;
101
+ return { x1, y1, x2, y2 };
102
+ };
103
+ /**
104
+ * eligibility 与分片打包:不合形态返回 null(调用方整元素回落烘焙——半解析半
105
+ * 烘焙的混血形态不做,383 原则)。几何源=raw.x1..y2(对象中心系)。
106
+ */
107
+ export const lineDecorationPieces = (raw, decl, matrix, zoom, dpr) => {
108
+ const type = typeof raw.type === 'string' ? raw.type.toLowerCase() : '';
109
+ if (type !== 'arrow' && type !== 'dimension')
110
+ return null;
111
+ if (!isStrokeSdfLineDecorationEnabled())
112
+ return null;
113
+ const p = readPoints(raw);
114
+ if (!p)
115
+ return null;
116
+ if (decl.alignment !== 'center')
117
+ return null;
118
+ const { aa, minAxis } = localPerDevicePxOf(matrix, zoom, dpr);
119
+ const aaPx = 4 * aa;
120
+ const strokeWidth = decl.width;
121
+ const cos = (p.x2 - p.x1) / Math.hypot(p.x2 - p.x1, p.y2 - p.y1);
122
+ const sin = (p.y2 - p.y1) / Math.hypot(p.x2 - p.x1, p.y2 - p.y1);
123
+ if (type === 'arrow') {
124
+ const arrowW = Number(raw.arrowWidth) || 15;
125
+ const arrowH = Number(raw.arrowHeight) || 22.5;
126
+ const arrowType = raw.arrowType === 'start' || raw.arrowType === 'both' ? raw.arrowType : 'end';
127
+ const sizeScale = Math.sqrt(strokeWidth);
128
+ const headW = arrowW * sizeScale;
129
+ const headH = arrowH * sizeScale;
130
+ const atStart = arrowType === 'start' || arrowType === 'both';
131
+ const atEnd = arrowType === 'end' || arrowType === 'both';
132
+ const { dash, ok } = lineDashTriple(decl);
133
+ if (!ok)
134
+ return null;
135
+ // 杆线:端头所在端退缩 headH(bake 同式);帽=decl(butt 缺省)
136
+ const ax1 = p.x1 + cos * (atStart ? headH : 0);
137
+ const ay1 = p.y1 + sin * (atStart ? headH : 0);
138
+ const ax2 = p.x2 - cos * (atEnd ? headH : 0);
139
+ const ay2 = p.y2 - sin * (atEnd ? headH : 0);
140
+ const shaft = linePiece(ax1, ay1, ax2, ay2, strokeWidth / 2, CAP_INDEX[decl.lineCap] ?? 0, decl.uniform, dash, aaPx, minAxis);
141
+ const pieces = [shaft];
142
+ const head = (tipX, tipY, dirCos, dirSin) => {
143
+ const bx = tipX - dirCos * headH;
144
+ const by = tipY - dirSin * headH;
145
+ return trianglePiece([
146
+ { x: tipX, y: tipY },
147
+ { x: bx + -dirSin * (headW / 2), y: by + dirCos * (headW / 2) },
148
+ { x: bx - -dirSin * (headW / 2), y: by - dirCos * (headW / 2) }
149
+ ], aaPx);
150
+ };
151
+ if (atEnd)
152
+ pieces.push(head(p.x2, p.y2, cos, sin));
153
+ if (atStart)
154
+ pieces.push(head(p.x1, p.y1, -cos, -sin));
155
+ return { pieces, text: null };
156
+ }
157
+ // Dimension:主线全语义 + 端刻×2(round 帽短线)+ 文本 spec
158
+ const { dash, ok } = lineDashTriple(decl);
159
+ if (!ok)
160
+ return null;
161
+ const main = linePiece(p.x1, p.y1, p.x2, p.y2, strokeWidth / 2, CAP_INDEX[decl.lineCap] ?? 0, decl.uniform, dash, aaPx, minAxis);
162
+ const fontPx = Number(raw.textFontSize) || 50;
163
+ const nx = -sin;
164
+ const ny = cos;
165
+ const halfTick = fontPx / 2;
166
+ const tickA = linePiece(p.x1 - nx * halfTick, p.y1 - ny * halfTick, p.x1 + nx * halfTick, p.y1 + ny * halfTick, strokeWidth / 2, CAP_INDEX.round, false, null, aaPx, minAxis);
167
+ const tickB = linePiece(p.x2 - nx * halfTick, p.y2 - ny * halfTick, p.x2 + nx * halfTick, p.y2 + ny * halfTick, strokeWidth / 2, CAP_INDEX.round, false, null, aaPx, minAxis);
168
+ // 文本 spec(bakeLineDecoration 同式:measureWidth 由调用方注入——模块零 DOM 依赖)
169
+ const text = typeof raw.text === 'string' && raw.text.trim() ? raw.text : '50cm';
170
+ const pad = Number(raw.textPadding) || 4;
171
+ const offset = fontPx + strokeWidth / 2 + 5;
172
+ return {
173
+ pieces: [main, tickA, tickB],
174
+ text: {
175
+ text,
176
+ fontPx,
177
+ w: 0,
178
+ h: fontPx + pad * 2,
179
+ cx: (p.x1 + p.x2) / 2 + nx * offset,
180
+ cy: (p.y1 + p.y2) / 2 + ny * offset,
181
+ angle: Math.atan2(p.y2 - p.y1, p.x2 - p.x1),
182
+ color: typeof raw.textColor === 'string' ? raw.textColor : '#333333',
183
+ bg: typeof raw.textBackgroundColor === 'string' ? raw.textBackgroundColor : '#ffffff',
184
+ follow: raw.textFollowLine !== false,
185
+ pad
186
+ }
187
+ };
188
+ };
@@ -85,7 +85,8 @@ export declare class MaskBakeCache {
85
85
  */
86
86
  noteVptChange(): void;
87
87
  /** 烘焙入口:sigBase+k 双命中直接复用;内容变化立即重烘;仅 k 变化走
88
- * stale 门(手势窗口/帧预算耗尽沿用旧烘焙,导出窗口恒新鲜) */
88
+ * stale 门(手势窗口/帧预算耗尽沿用旧烘焙,导出窗口恒新鲜)。k 命中经
89
+ * bakeKMatches(批次 377):预览导出窗(bakeKReuseFloor>0)复用更密/相近档 */
89
90
  bake(node: MaskNode, params: MaskBakeContentParams): MaskBakeResult | null;
90
91
  /** 失效(context lost 同四族烘焙口径):缓存整删,下次渲染按签名 miss 全量重跑 */
91
92
  invalidate(): void;
@@ -30,6 +30,7 @@
30
30
  */
31
31
  import { createTexture2D } from './textureUpload';
32
32
  import { SettleScheduler } from './settleScheduler';
33
+ import { bakeKMatches, bakeShouldHoldStale } from './bakeGate';
33
34
  /** base64 src 过大时取首尾特征作签名键(应用 maskSrcKey 同款;SceneRenderer 的
34
35
  * clipImage 滤镜读回缓存同源复用) */
35
36
  export const maskSrcKey = (src) => {
@@ -176,12 +177,13 @@ export class MaskBakeCache {
176
177
  this.settle.noteInteraction();
177
178
  }
178
179
  /** 烘焙入口:sigBase+k 双命中直接复用;内容变化立即重烘;仅 k 变化走
179
- * stale 门(手势窗口/帧预算耗尽沿用旧烘焙,导出窗口恒新鲜) */
180
+ * stale 门(手势窗口/帧预算耗尽沿用旧烘焙,导出窗口恒新鲜)。k 命中经
181
+ * bakeKMatches(批次 377):预览导出窗(bakeKReuseFloor>0)复用更密/相近档 */
180
182
  bake(node, params) {
181
183
  const full = { ...params, zoom: this.host.zoom, dpr: this.host.dpr };
182
184
  const { sigBase, k } = signatureParts(node, full);
183
185
  const cached = this.bakes.get(node);
184
- if (cached && cached.sigBase === sigBase && cached.k === k)
186
+ if (cached && cached.sigBase === sigBase && bakeKMatches(cached.k, k, this.host.bakeKReuseFloor ?? 0))
185
187
  return cached;
186
188
  if (cached) {
187
189
  if (cached.sigBase !== sigBase) {
@@ -190,11 +192,8 @@ export class MaskBakeCache {
190
192
  }
191
193
  else {
192
194
  // 仅 k 变化(缩放链):手势窗口内与帧预算耗尽时沿用 stale
193
- // (逻辑尺寸不变,分辨率暂欠),settle 后补齐精确 k
194
- const now = performance.now();
195
- // 导出窗口(applyExportViewport 接管视口)不受手势门:k 换档立即新鲜烘焙
196
- const gesturing = !this.host.isExporting() && this.settle.isSettling(now);
197
- if (gesturing || now > this.frame.bakeDeadline) {
195
+ // (逻辑尺寸不变,分辨率暂欠),settle 后补齐精确 k;导出窗口恒新鲜
196
+ if (bakeShouldHoldStale(this.host, this.settle, this.frame, performance.now())) {
198
197
  this.frame.bakeMore = true;
199
198
  return cached;
200
199
  }
@@ -21,6 +21,10 @@ export declare class PsdEffectPainter {
21
21
  /** 最近一次视口缩放变化时刻(stale 判定的手势窗口)与 settle 定时器——统一
22
22
  * 收敛至 SettleScheduler(批次372),见其头注 */
23
23
  private readonly settle;
24
+ /** CPU 自建烘焙纹理登记(批次 380 所有权拆分):仅 createTexture2D 本地产物
25
+ * 入册——GPU 画家(SharedGpuPainters)借出纹理不入册不删,生命周期归其
26
+ * 签名缓存自管(WeakMap 按节点回收 + 批次 342 死句柄守卫防御) */
27
+ private readonly cpuTexes;
24
28
  constructor(host: SceneRendererHost, frame: FrameState, gpu: SharedGpuPainters, getPuzzleBox: () => PuzzleBox | null, settle?: SettleScheduler);
25
29
  /**
26
30
  * 视口缩放变化通知(文本特效 stale 重烘的手势窗口):共享 SettleScheduler
@@ -31,6 +35,14 @@ export declare class PsdEffectPainter {
31
35
  render(node: SceneNode, phase: 'below' | 'above'): void;
32
36
  /** 烘焙缓存的 quad 铺放(stale 命中与新烘焙共用出口) */
33
37
  private renderPsdBakeQuads;
34
- /** 失效(context lost 与字体迟到同口径):烘焙整删——GPU 画家侧缓存由 SharedGpuPainters 失效 */
38
+ /** 烘焙纹理删除统一口(批次 380 所有权门):仅删 CPU 自建(登记册内);
39
+ * GPU 画家借出的跳过——无差别删除曾致 fx 签名缓存死句柄复发(批次 342
40
+ * 守卫的源头),拆分后守卫降级为纯防御 */
41
+ private deleteBakeTex;
42
+ /** 失效(context lost 与字体迟到同口径):烘焙整删——GPU 画家侧缓存由 SharedGpuPainters 失效。
43
+ * 批次 380:CPU 自建纹理经登记册真删(原 WeakMap 弃管 = GL 滞留至 context
44
+ * loss);GPU 借出纹理不删(所有权归 fx,其 invalidateTextDerivedBakes/
45
+ * invalidate 自管)+ 纪元 bump(真删后保留式快照若持引用——重放死句柄即
46
+ * 特效层消失,经纪元入门链) */
35
47
  invalidate(): void;
36
48
  }
@@ -25,6 +25,7 @@ import { bakePathEffectShadow, bakePsdShadow, bakePsdSolidFill, bakePsdStrokes,
25
25
  import { createTexture2D } from './textureUpload';
26
26
  import { clampQuadToPuzzleBox } from './puzzleClamp';
27
27
  import { SettleScheduler } from './settleScheduler';
28
+ import { bakeShouldHoldStale } from './bakeGate';
28
29
  /** PSD 烘焙分帧预算(每帧毫秒上限)与缩放 settle 窗口(beginFrame 侧共用) */
29
30
  export const FX_BAKE_BUDGET_MS = 6;
30
31
  /** settle 窗口常量随协议收敛迁至 SettleScheduler(批次372)——旧导入位保留转发 */
@@ -72,6 +73,10 @@ export class PsdEffectPainter {
72
73
  /** 最近一次视口缩放变化时刻(stale 判定的手势窗口)与 settle 定时器——统一
73
74
  * 收敛至 SettleScheduler(批次372),见其头注 */
74
75
  settle;
76
+ /** CPU 自建烘焙纹理登记(批次 380 所有权拆分):仅 createTexture2D 本地产物
77
+ * 入册——GPU 画家(SharedGpuPainters)借出纹理不入册不删,生命周期归其
78
+ * 签名缓存自管(WeakMap 按节点回收 + 批次 342 死句柄守卫防御) */
79
+ cpuTexes = new Set();
75
80
  constructor(host, frame, gpu, getPuzzleBox, settle = new SettleScheduler(() => host.requestRenderAll())) {
76
81
  this.host = host;
77
82
  this.frame = frame;
@@ -147,13 +152,16 @@ export class PsdEffectPainter {
147
152
  const sigBase = [psdEffectsKey(decls), fxNode.matrix.join(','), fxNode.dims.x, fxNode.dims.y, srcFingerprint, animVersion].join(';');
148
153
  let bake = this.psdBakes.get(node);
149
154
  if (bake && bake.sigBase !== sigBase) {
150
- // 内容/声明/矩阵变化:立即重烘(正确性优先,编辑场景单节点可承受)
155
+ // 内容/声明/矩阵变化:立即重烘(正确性优先,编辑场景单节点可承受)。
156
+ // 删除走所有权门(批次 380):GPU 画家借出的纹理(fx.bakeDropShadows/
157
+ // bakeSolidFill 产物归 SharedGpuPainters 签名缓存)不删——此前无差别删除
158
+ // 正是批次 342 gpuFx 死句柄守卫的源头(乒乓回旧签名命中已删句柄)
151
159
  if (bake.shadow)
152
- this.host.gl.deleteTexture(bake.shadow.tex);
160
+ this.deleteBakeTex(bake.shadow.tex);
153
161
  for (const f of bake.fills)
154
- this.host.gl.deleteTexture(f.tex);
162
+ this.deleteBakeTex(f.tex);
155
163
  for (const s of bake.strokes)
156
- this.host.gl.deleteTexture(s.tex);
164
+ this.deleteBakeTex(s.tex);
157
165
  bake = undefined;
158
166
  }
159
167
  if (bake && bake.bucket !== fxRs) {
@@ -163,19 +171,17 @@ export class PsdEffectPainter {
163
171
  // (applyExportViewport 接管视口)不受手势门:桶换档立即新鲜烘焙
164
172
  // (SceneExporter 按 bakeMore 多轮重渲兜底——2026-10-09 描边接入 stale
165
173
  // 协议时一并收口:此前缩放后 160ms 内导出会吃到手势期 stale 特效)
166
- const now = performance.now();
167
- const zooming = !this.host.isExporting() && this.settle.isSettling(now);
168
- if (zooming || now > this.frame.bakeDeadline) {
174
+ if (bakeShouldHoldStale(this.host, this.settle, this.frame, performance.now())) {
169
175
  this.frame.bakeMore = true;
170
176
  this.renderPsdBakeQuads(fxNode, bake, phase);
171
177
  return;
172
178
  }
173
179
  if (bake.shadow)
174
- this.host.gl.deleteTexture(bake.shadow.tex);
180
+ this.deleteBakeTex(bake.shadow.tex);
175
181
  for (const f of bake.fills)
176
- this.host.gl.deleteTexture(f.tex);
182
+ this.deleteBakeTex(f.tex);
177
183
  for (const s of bake.strokes)
178
- this.host.gl.deleteTexture(s.tex);
184
+ this.deleteBakeTex(s.tex);
179
185
  bake = undefined;
180
186
  }
181
187
  if (!bake) {
@@ -218,6 +224,7 @@ export class PsdEffectPainter {
218
224
  if (shadowResult) {
219
225
  const tex = createTexture2D(gl, shadowResult.canvas, { mipmap: true });
220
226
  if (tex) {
227
+ this.cpuTexes.add(tex);
221
228
  // quad 铺放尺寸 = 场景单位(rasterScale>1 时画布像素 ≠ 场景尺寸)
222
229
  shadow = { tex, w: shadowResult.quadW, h: shadowResult.quadH };
223
230
  }
@@ -245,6 +252,7 @@ export class PsdEffectPainter {
245
252
  const tex = createTexture2D(gl, fillCanvas);
246
253
  if (!tex)
247
254
  continue;
255
+ this.cpuTexes.add(tex);
248
256
  fills.push({ tex, dx: fillDecl.offset.x, dy: fillDecl.offset.y });
249
257
  }
250
258
  }
@@ -259,6 +267,7 @@ export class PsdEffectPainter {
259
267
  const tex = createTexture2D(gl, entry.canvas);
260
268
  if (!tex)
261
269
  continue;
270
+ this.cpuTexes.add(tex);
262
271
  strokes.push({ tex, w: entry.quadW, h: entry.quadH, dx: entry.dx, dy: entry.dy });
263
272
  }
264
273
  }
@@ -300,8 +309,25 @@ export class PsdEffectPainter {
300
309
  }
301
310
  }
302
311
  }
303
- /** 失效(context lost 与字体迟到同口径):烘焙整删——GPU 画家侧缓存由 SharedGpuPainters 失效 */
312
+ /** 烘焙纹理删除统一口(批次 380 所有权门):仅删 CPU 自建(登记册内);
313
+ * GPU 画家借出的跳过——无差别删除曾致 fx 签名缓存死句柄复发(批次 342
314
+ * 守卫的源头),拆分后守卫降级为纯防御 */
315
+ deleteBakeTex(tex) {
316
+ if (!this.cpuTexes.has(tex))
317
+ return;
318
+ this.host.gl.deleteTexture(tex);
319
+ this.cpuTexes.delete(tex);
320
+ }
321
+ /** 失效(context lost 与字体迟到同口径):烘焙整删——GPU 画家侧缓存由 SharedGpuPainters 失效。
322
+ * 批次 380:CPU 自建纹理经登记册真删(原 WeakMap 弃管 = GL 滞留至 context
323
+ * loss);GPU 借出纹理不删(所有权归 fx,其 invalidateTextDerivedBakes/
324
+ * invalidate 自管)+ 纪元 bump(真删后保留式快照若持引用——重放死句柄即
325
+ * 特效层消失,经纪元入门链) */
304
326
  invalidate() {
327
+ for (const tex of this.cpuTexes)
328
+ this.host.gl.deleteTexture(tex);
329
+ this.cpuTexes.clear();
305
330
  this.psdBakes = new WeakMap();
331
+ this.host.renderEpoch?.bump('psd-bake-invalidate');
306
332
  }
307
333
  }
@@ -20,7 +20,8 @@
20
20
  * 近似,parity 劣于 canvas 直出。offset 进签名:播放期逐帧重烘(量级同
21
21
  * 动画族逐帧纹理重传),静止态恒缓存。
22
22
  */
23
- import { drawParagraphPrefix, fontDeclaration, resolveStrokeDecl } from '../model/textLayout';
23
+ import { CJK_LINE_RE, drawParagraphPrefix, fontDeclaration, resolveStrokeDecl } from '../model/textLayout';
24
+ import { TextAlignValues, isJustifyFamilyAlign, isStretchAlign } from '../model/textAlign';
24
25
  import { glyphStyleOf } from './arcText';
25
26
  import { drawVerticalGlyph, verticalGlyphOps } from './verticalText';
26
27
  /** 滚动声明解析:只认 ScrollText 类型(普通 textbox 带杂散字段不滚)。
@@ -212,7 +213,11 @@ const drawTextPass = (ctx, node, pass) => {
212
213
  if (endIndex < line.boxes.length)
213
214
  runX = line.boxes[endIndex].left;
214
215
  };
215
- const justify = typeof raw.textAlign === 'string' && raw.textAlign.startsWith('justify');
216
+ const justify = isJustifyFamilyAlign(raw.textAlign);
217
+ const disperse = raw.textAlign === TextAlignValues.DISPERSE;
218
+ // 纯 justify/disperse 的 CJK 行被逐字分布拉伸(textLayout fork 口径)→ run
219
+ // 必须逐字断段(与 TextRenderer.renderTextDashStroke 同语义)
220
+ const charWiseStretched = isStretchAlign(raw.textAlign) && line.graphemes.some((g) => CJK_LINE_RE.test(g));
216
221
  for (let i = 0; i < line.graphemes.length; i++) {
217
222
  const o = line.charStyles[i];
218
223
  const gs = glyphStyleOf(style, o);
@@ -228,7 +233,7 @@ const drawTextPass = (ctx, node, pass) => {
228
233
  runFill = fill;
229
234
  }
230
235
  run += line.graphemes[i];
231
- if (charSpacing || (justify && /[ \t\r]/.test(line.graphemes[i])))
236
+ if (charSpacing || ((justify || disperse) && /[ \t\r]/.test(line.graphemes[i])) || charWiseStretched)
232
237
  flush(i + 1);
233
238
  }
234
239
  flush(line.graphemes.length);
@@ -0,0 +1,53 @@
1
+ /**
2
+ * 段表数据纹理(方向三第二刀,批次 383,
3
+ * docs/2026-10-13_webgl2-sdf-polygon-stroke-blueprint.md §2):多边形族描边
4
+ * SDF 直绘的段数据通道——RGBA32F、宽=MAX_SEG_BATCH、**行=实例槽位**。
5
+ *
6
+ * 生命周期不变式(与 QuadBatch 实例块同源):
7
+ * - 段表行与实例块在 pushQuad 同点双写;实例数据 aSeg=(rowBase=槽位, segCount,
8
+ * geomHash) 三元寻址,**geomHash 参与内容等价判定**——几何变化 → hash 变 →
9
+ * 实例块判脏重传,段表行随之重写(增量上传/保留式既有正确性机制自动覆盖段表)。
10
+ * - rowOwners 记录「纹理行当前持有者+内容 hash」:槽位复用/rewind 后的新 push
11
+ * 必然重写行(owner 或 hash 任一不等);同 owner 同内容落同槽位零重写(稳态
12
+ * 增量上传协议不被打回)。无主 push 恒写(无身份可去重)。
13
+ * - 行写即时 texSubImage2D(≤MAX_SEG_BATCH×16B):纹理在任意 draw 前恒与
14
+ * CPU 数组一致——deferDraws/保留式重放不引入独立的上传排队语义(批次 225
15
+ * 教训的段表版:行内容与命令表脱钩=重放死数据)。
16
+ * - 容量与 QuadBatch 实例容量同步增长(grow 同点 ensureRows);RGBA32F 为
17
+ * WebGL2 核心采样格式(texelFetch 免过滤,无 renderability 要求)——创建
18
+ * 失败置 unhealthy 永久回落烘焙(调用方 strokePainter 门)。
19
+ */
20
+ /** 段表行宽(每行最多段数)——与 shaders.ts MAX_QUAD_SEG 同值(着色器循环上限) */
21
+ export declare const MAX_SEG_BATCH = 64;
22
+ /** 段表内容(方向三第二刀):data=count×4 段坐标(对象中心系,round 模式=中心线
23
+ * 原样端点 / miter 模式=双偏移轮廓,批次 385);hash=内容数值指纹(量化 FNV,
24
+ * 31 位= float32 精确域——参与 QuadBatch ownerBlocks 内容等价判定,几何变化即
25
+ * 判脏重传) */
26
+ export interface StrokeSdfSegments {
27
+ data: Float32Array;
28
+ count: number;
29
+ hash: number;
30
+ }
31
+ /** 量化 FNV-1a 指纹(1/256 本地单位分辨——远小于任何可视差异;>>>1 落 31 位
32
+ * = float32 精确域,ownerBlocks 等值判定不经受舍入)。round/mtier 两打包路
33
+ * 共用(同 hash 语义:内容等价的段表零重传) */
34
+ export declare const segHash: (data: Float32Array) => number;
35
+ export declare class SegmentTable {
36
+ private readonly gl;
37
+ private tex;
38
+ private data;
39
+ private rowOwners;
40
+ private rowHashes;
41
+ private rows;
42
+ private healthy;
43
+ constructor(gl: WebGL2RenderingContext);
44
+ /** 容量保障(懒建/翻倍增长;失败置 unhealthy——调用方永久回落烘焙) */
45
+ ensureRows(rows: number): boolean;
46
+ /** 行写入(同 owner 同 hash 去重——纹理行已持本 owner 当前内容则零 GL 操作) */
47
+ writeRow(row: number, owner: object | null, segs: StrokeSdfSegments): void;
48
+ /** 行内容快照出口(spec 对拍用) */
49
+ rowAt(row: number): Float32Array | null;
50
+ get texture(): WebGLTexture | null;
51
+ get rowCount(): number;
52
+ dispose(): void;
53
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * 段表数据纹理(方向三第二刀,批次 383,
3
+ * docs/2026-10-13_webgl2-sdf-polygon-stroke-blueprint.md §2):多边形族描边
4
+ * SDF 直绘的段数据通道——RGBA32F、宽=MAX_SEG_BATCH、**行=实例槽位**。
5
+ *
6
+ * 生命周期不变式(与 QuadBatch 实例块同源):
7
+ * - 段表行与实例块在 pushQuad 同点双写;实例数据 aSeg=(rowBase=槽位, segCount,
8
+ * geomHash) 三元寻址,**geomHash 参与内容等价判定**——几何变化 → hash 变 →
9
+ * 实例块判脏重传,段表行随之重写(增量上传/保留式既有正确性机制自动覆盖段表)。
10
+ * - rowOwners 记录「纹理行当前持有者+内容 hash」:槽位复用/rewind 后的新 push
11
+ * 必然重写行(owner 或 hash 任一不等);同 owner 同内容落同槽位零重写(稳态
12
+ * 增量上传协议不被打回)。无主 push 恒写(无身份可去重)。
13
+ * - 行写即时 texSubImage2D(≤MAX_SEG_BATCH×16B):纹理在任意 draw 前恒与
14
+ * CPU 数组一致——deferDraws/保留式重放不引入独立的上传排队语义(批次 225
15
+ * 教训的段表版:行内容与命令表脱钩=重放死数据)。
16
+ * - 容量与 QuadBatch 实例容量同步增长(grow 同点 ensureRows);RGBA32F 为
17
+ * WebGL2 核心采样格式(texelFetch 免过滤,无 renderability 要求)——创建
18
+ * 失败置 unhealthy 永久回落烘焙(调用方 strokePainter 门)。
19
+ */
20
+ /** 段表行宽(每行最多段数)——与 shaders.ts MAX_QUAD_SEG 同值(着色器循环上限) */
21
+ export const MAX_SEG_BATCH = 64;
22
+ /** 量化 FNV-1a 指纹(1/256 本地单位分辨——远小于任何可视差异;>>>1 落 31 位
23
+ * = float32 精确域,ownerBlocks 等值判定不经受舍入)。round/mtier 两打包路
24
+ * 共用(同 hash 语义:内容等价的段表零重传) */
25
+ export const segHash = (data) => {
26
+ let h = 0x811c9dc5;
27
+ for (let i = 0; i < data.length; i++)
28
+ h = Math.imul(h ^ Math.round(data[i] * 256), 0x01000193);
29
+ return h >>> 1;
30
+ };
31
+ export class SegmentTable {
32
+ gl;
33
+ tex = null;
34
+ data = new Float32Array(0);
35
+ rowOwners = [];
36
+ rowHashes = [];
37
+ rows = 0;
38
+ healthy = true;
39
+ constructor(gl) {
40
+ this.gl = gl;
41
+ }
42
+ /** 容量保障(懒建/翻倍增长;失败置 unhealthy——调用方永久回落烘焙) */
43
+ ensureRows(rows) {
44
+ if (!this.healthy)
45
+ return false;
46
+ if (rows <= this.rows)
47
+ return true;
48
+ const gl = this.gl;
49
+ try {
50
+ const next = new Float32Array(rows * MAX_SEG_BATCH * 4);
51
+ next.set(this.data);
52
+ const tex = this.tex ?? gl.createTexture();
53
+ if (!tex) {
54
+ this.healthy = false;
55
+ return false;
56
+ }
57
+ gl.activeTexture(gl.TEXTURE2);
58
+ gl.bindTexture(gl.TEXTURE_2D, tex);
59
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA32F, MAX_SEG_BATCH, rows, 0, gl.RGBA, gl.FLOAT, next);
60
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
61
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
62
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
63
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
64
+ gl.activeTexture(gl.TEXTURE0);
65
+ this.tex = tex;
66
+ this.data = next;
67
+ while (this.rowOwners.length < rows) {
68
+ this.rowOwners.push(null);
69
+ this.rowHashes.push(0);
70
+ }
71
+ this.rows = rows;
72
+ return true;
73
+ }
74
+ catch {
75
+ this.healthy = false;
76
+ return false;
77
+ }
78
+ }
79
+ /** 行写入(同 owner 同 hash 去重——纹理行已持本 owner 当前内容则零 GL 操作) */
80
+ writeRow(row, owner, segs) {
81
+ if (!this.healthy || row >= this.rows)
82
+ return;
83
+ if (owner && this.rowOwners[row] === owner && this.rowHashes[row] === segs.hash)
84
+ return;
85
+ const base = row * MAX_SEG_BATCH * 4;
86
+ this.data.fill(0, base, base + MAX_SEG_BATCH * 4);
87
+ this.data.set(segs.data.subarray(0, segs.count * 4), base);
88
+ this.rowOwners[row] = owner;
89
+ this.rowHashes[row] = segs.hash;
90
+ const gl = this.gl;
91
+ gl.activeTexture(gl.TEXTURE2);
92
+ gl.bindTexture(gl.TEXTURE_2D, this.tex);
93
+ gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, row, segs.count, 1, gl.RGBA, gl.FLOAT, this.data, base);
94
+ gl.activeTexture(gl.TEXTURE0);
95
+ }
96
+ /** 行内容快照出口(spec 对拍用) */
97
+ rowAt(row) {
98
+ if (row >= this.rows)
99
+ return null;
100
+ return this.data.slice(row * MAX_SEG_BATCH * 4, (row + 1) * MAX_SEG_BATCH * 4);
101
+ }
102
+ get texture() {
103
+ return this.tex;
104
+ }
105
+ get rowCount() {
106
+ return this.rows;
107
+ }
108
+ dispose() {
109
+ if (this.tex)
110
+ this.gl.deleteTexture(this.tex);
111
+ this.tex = null;
112
+ this.data = new Float32Array(0);
113
+ this.rowOwners = [];
114
+ this.rowHashes = [];
115
+ this.rows = 0;
116
+ }
117
+ }