@yft-design/engine 0.0.10 → 0.0.12

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 (46) hide show
  1. package/dist/EngineCanvas.d.ts +6 -0
  2. package/dist/EngineCanvas.js +8 -0
  3. package/dist/debug/seed.d.ts +2 -1
  4. package/dist/debug/seed.js +8 -5
  5. package/dist/engineFlag.d.ts +12 -0
  6. package/dist/engineFlag.js +12 -0
  7. package/dist/export/svgExport.js +12 -9
  8. package/dist/index.d.ts +4 -0
  9. package/dist/index.js +4 -0
  10. package/dist/interact/tableCellEditing.js +2 -1
  11. package/dist/model/scene.d.ts +1 -1
  12. package/dist/model/scene.js +4 -3
  13. package/dist/model/tableLayout.js +3 -2
  14. package/dist/model/textAlign.d.ts +37 -0
  15. package/dist/model/textAlign.js +36 -0
  16. package/dist/model/textLayout.d.ts +11 -4
  17. package/dist/model/textLayout.js +46 -26
  18. package/dist/render/QuadBatch.d.ts +14 -5
  19. package/dist/render/QuadBatch.js +66 -10
  20. package/dist/render/SceneRenderer.d.ts +10 -0
  21. package/dist/render/SceneRenderer.js +13 -1
  22. package/dist/render/TextRenderer.js +10 -4
  23. package/dist/render/gpuFx.d.ts +14 -2
  24. package/dist/render/gpuFx.js +34 -4
  25. package/dist/render/gpuShadow.d.ts +15 -3
  26. package/dist/render/gpuShadow.js +37 -4
  27. package/dist/render/imageEffects.d.ts +3 -1
  28. package/dist/render/imageEffects.js +30 -5
  29. package/dist/render/lineDecorationSdf.d.ts +63 -0
  30. package/dist/render/lineDecorationSdf.js +188 -0
  31. package/dist/render/scrollText.js +8 -3
  32. package/dist/render/segmentTable.d.ts +53 -0
  33. package/dist/render/segmentTable.js +117 -0
  34. package/dist/render/shaders.d.ts +4 -2
  35. package/dist/render/shaders.js +98 -7
  36. package/dist/render/shadowPainter.d.ts +1 -1
  37. package/dist/render/shadowPainter.js +3 -3
  38. package/dist/render/sharedGpuPainters.d.ts +4 -2
  39. package/dist/render/sharedGpuPainters.js +7 -5
  40. package/dist/render/strokeMiterOutline.d.ts +39 -0
  41. package/dist/render/strokeMiterOutline.js +224 -0
  42. package/dist/render/strokePainter.d.ts +11 -0
  43. package/dist/render/strokePainter.js +80 -7
  44. package/dist/render/strokeSdf.d.ts +34 -13
  45. package/dist/render/strokeSdf.js +177 -13
  46. package/package.json +1 -1
@@ -19,6 +19,7 @@ export const QUAD_VERT_SRC = `#version 300 es
19
19
  layout(location = 6) in vec4 aColor;
20
20
  layout(location = 7) in vec2 aRadius;
21
21
  layout(location = 8) in vec3 aDash;
22
+ layout(location = 9) in vec3 aSeg;
22
23
 
23
24
  uniform mat3 uView;
24
25
 
@@ -33,6 +34,9 @@ export const QUAD_VERT_SRC = `#version 300 es
33
34
  /** aDash 直通(方向三第一刀,2026-10-13):(on, cycle, phase) 实例常量,
34
35
  * 与 vShape 同直通管线(divisor=1 逐实例,插值恒等) */
35
36
  out vec3 vDash;
37
+ /** aSeg 直通(方向三第二刀,2026-10-13):(rowBase, segCount, geomHash)
38
+ * 实例常量——多边形族段表寻址(uSegTex 行=实例槽位) */
39
+ out vec3 vSeg;
36
40
 
37
41
  void main() {
38
42
  vec2 local = (aCorner - 0.5) * aDim;
@@ -46,6 +50,7 @@ export const QUAD_VERT_SRC = `#version 300 es
46
50
  vHalf = aDim * 0.5;
47
51
  vShape = aUv;
48
52
  vDash = aDash;
53
+ vSeg = aSeg;
49
54
  }
50
55
  `;
51
56
  /**
@@ -106,11 +111,17 @@ in vec2 vRadius;
106
111
  in vec2 vHalf;
107
112
  in vec4 vShape;
108
113
  in vec3 vDash;
114
+ in vec3 vSeg;
109
115
 
110
116
  uniform sampler2D uTex;
111
117
  /** uniform 笔宽换算(GS 描边 SDF):画布空间笔半宽 = halfW·uPenScale
112
118
  * (zoom×dpr,随 uView 同点上传;与烘焙路径 params.zoom×dpr 同源) */
113
119
  uniform float uPenScale;
120
+ /** 段表数据纹理(方向三第二刀):RGBA32F、宽=MAX_QUAD_SEG、行=实例槽位;
121
+ * 每纹素 1 段 (ax, ay, bx, by)——kind=4 实例 texelFetch 逐段求值 */
122
+ uniform sampler2D uSegTex;
123
+ /** 段表行宽上限(与 render/segmentTable.ts MAX_SEG_BATCH 同值) */
124
+ const int MAX_QUAD_SEG = 64;
114
125
  ${GRADIENT_FRAG_BODY}
115
126
  out vec4 fragColor;
116
127
 
@@ -171,6 +182,12 @@ float sdEllipseDist(vec2 p, vec2 ab) {
171
182
  * - line butt/square = 轴向旋转盒(square 沿轴伸 hh;盒本身即墨迹,无带减);
172
183
  * round = capsule 距离走通用带
173
184
  * - ellipse = sdEllipseDist(Newton 精确距离,见其注)
185
+ * - 段表(kind=4,方向三第二刀):uSegTex 逐段 texelFetch——join/cap 语义在
186
+ * CPU 打包时化进段数据,cap 位复用为模式位(0=capsule 并=round,端点不
187
+ * 延长;1=双偏移轮廓 winding 填充=miter,批次 385——段数据=区域轮廓本身,
188
+ * 见下方分支注)。闭合/开放无分叉(closing edge/帽边由 CPU 补)。逐实例
189
+ * 常量分支(segCount/mode 全实例一致)——循环 divergence 对整个实例
190
+ * uniform,导数良定义
174
191
  * - 笔宽两式:non-uniform = 本地带 |d|≤halfW(随矩阵缩放);uniform = 像素
175
192
  * 空间 |d|/fw − halfW·uPenScale(画布空间恒定;fw=fwidth(d) 即本地单位/像素)
176
193
  * AA = fwidth 线性 ramp(roundedCoverage 同式),自动适配旋转/各向异性/视口。
@@ -179,14 +196,16 @@ float sdEllipseDist(vec2 p, vec2 ab) {
179
196
  * - line:从端点 a 起算的轴向投影(canvas moveTo(a) lineTo(b) 同序)
180
197
  * - 直角 rect:canvas rect(x,y,w,h) 左上角起顺时针四段累计(上右下左),
181
198
  * 最近边分类(p 到四边线垂直距离取最小)——角区两邻段公式在角点取值相等
182
- * (如 (hx,−hy):上边 2hx = 右边 2hx),dash 跨角天然连续不断裂
199
+ * (如 (hx,−hy):上边 2hx = 右边 2hx),dash 跨角天然连续不断裂;
200
+ * 圆角 rect(rx==ry 圆角)= 角方块径向投影+直边段轴向投影(批次 386,
201
+ * 见分支注);square 帽 dash=窗两侧延 halfW(批次 386,见门公式注)
183
202
  * 门 = m∈[0,on) 布尔 × 到窗口两边界带符号内距的 fwidth 线性 ramp(复刻 canvas
184
203
  * 逐 dash butt 端 ~1px AA;整数对齐场景 ramp 恰 0/1 与 canvas 位级一致)。
185
204
  * phase 符号=canvas lineDashOffset 语义:m = mod(s + phase, cycle)——正 offset
186
205
  * 使图案向路径起点回退(着色器弧长+门公式与 JS 镜像对拍锁定,见
187
206
  * __tests__/strokeSdfDash.spec.ts;相位符号经 e2e 像素 A/B 对烘焙链实证)。
188
207
  */
189
- float strokeCoverage(vec2 local, vec2 pen, vec4 shape, vec3 dash, float k) {
208
+ float strokeCoverage(vec2 local, vec2 pen, vec4 shape, vec3 dash, vec3 seg, float k) {
190
209
  int code = int(-pen.y + 0.5);
191
210
  int kind = code & 7;
192
211
  int cap = (code >> 3) & 7;
@@ -221,6 +240,37 @@ float strokeCoverage(vec2 local, vec2 pen, vec4 shape, vec3 dash, float k) {
221
240
  d = max(abs(pl.x) - (0.5 * L + ext), abs(pl.y) - hh);
222
241
  boxBand = true;
223
242
  }
243
+ } else if (kind == 4) {
244
+ // 段表循环(方向三第二刀):join/cap 语义 CPU 化进段数据。模式位(cap 位段,
245
+ // strokeSdf.ts modeSlot):0=capsule 并(round,批次 383——端点不延长,canvas
246
+ // 逐接头圆弧的精确等价);1=双偏移轮廓 winding 填充(miter,批次 385——段
247
+ // 数据=描边区域轮廓本身(strokeMiterOutline.ts),halfW 已烘进坐标,带公式
248
+ // 不再减 halfW 而走填充式覆盖率(boxBand 臂)。绕数=+x 射线半开约定逐段累积
249
+ // ((a.y>py)!=(b.y>py) 且交点在右侧——水平段零贡献),带内 |wind|≥1、洞 0;
250
+ // 粗笔宽内轮廓反转/自交由 nonzero 语义天然正确(canvas stroke 同填充口径)。
251
+ // wind 翻转只发生在轮廓上——该处 best→0,d 连续,fwidth 良定义
252
+ int cnt = int(seg.y + 0.5);
253
+ int row = int(seg.x + 0.5);
254
+ bool fillMode = cap == 1;
255
+ float best = 1e9;
256
+ float wind = 0.0;
257
+ for (int i = 0; i < MAX_QUAD_SEG; i++) {
258
+ if (i >= cnt) break;
259
+ vec4 s = texelFetch(uSegTex, ivec2(i, row), 0);
260
+ vec2 ba = s.zw - s.xy;
261
+ float t = clamp(dot(local - s.xy, ba) / max(dot(ba, ba), 1e-12), 0.0, 1.0);
262
+ best = min(best, length(local - s.xy - ba * t));
263
+ if (fillMode && ((s.y > local.y) != (s.w > local.y))) {
264
+ float xi = s.x + (local.y - s.y) / (s.w - s.y) * (s.z - s.x);
265
+ if (xi > local.x) wind += (s.w > s.y) ? 1.0 : -1.0;
266
+ }
267
+ }
268
+ if (fillMode) {
269
+ d = wind != 0.0 ? -best : best;
270
+ boxBand = true;
271
+ } else {
272
+ d = best;
273
+ }
224
274
  } else {
225
275
  // ellipse:Newton 精确距离(带宽度量在扁平极点须精确,见 sdEllipseDist 注)
226
276
  d = sdEllipseDist(local, shape.zw);
@@ -240,6 +290,40 @@ float strokeCoverage(vec2 local, vec2 pen, vec4 shape, vec3 dash, float k) {
240
290
  vec2 a = shape.xy;
241
291
  vec2 ba = shape.zw - a;
242
292
  s = dot(local - a, ba) / max(sqrt(dot(ba, ba)), 1e-12);
293
+ } else if (shape.x > 1e-4 && abs(shape.x - shape.y) < 1e-4) {
294
+ // 圆角 rect(rx==ry,批次 386):烘焙参考 traceRoundedRectPath 同锚
295
+ // (moveTo(x+rx,y)=顶直段左端起顺时针)——四角方块径向投影
296
+ // s=段前缀+r·(φ−段起始角)(TL 角 atan2∈(−π,−π/2] 需 +2π 归 [π,3π/2])、
297
+ // 四直边段轴向投影;段前缀程序化(周长 4ex+4ey+2πr 闭环);κ-bezier
298
+ // 角与理想圆弧的弧长差 ~1e-4·r 亚像素
299
+ float r = shape.x;
300
+ vec2 h = shape.zw;
301
+ float ex = h.x - r;
302
+ float ey = h.y - r;
303
+ float acx = abs(local.x);
304
+ float acy = abs(local.y);
305
+ float pTR = 2.0 * ex;
306
+ float pRight = pTR + 1.5707963 * r;
307
+ float pBR = pRight + 2.0 * ey;
308
+ float pBottom = pBR + 1.5707963 * r;
309
+ float pBL = pBottom + 2.0 * ex;
310
+ float pLeft = pBL + 1.5707963 * r;
311
+ float pTL = pLeft + 2.0 * ey;
312
+ if (acx > ex && acy > ey) {
313
+ float cxs = local.x > 0.0 ? ex : -ex;
314
+ float cys = local.y > 0.0 ? ey : -ey;
315
+ float phi = atan(local.y - cys, local.x - cxs);
316
+ if (local.x > 0.0 && local.y < 0.0) s = pTR + r * (phi + 1.5707963);
317
+ else if (local.x > 0.0) s = pBR + r * phi;
318
+ else if (local.y > 0.0) s = pBL + r * (phi - 1.5707963);
319
+ else s = pTL + r * (phi + 6.2831853 - 3.14159265);
320
+ } else if (acx <= ex) {
321
+ if (local.y < 0.0) s = local.x + ex;
322
+ else s = pBottom + (ex - local.x);
323
+ } else {
324
+ if (local.x > 0.0) s = pRight + (local.y + ey);
325
+ else s = pLeft + (ey - local.y);
326
+ }
243
327
  } else {
244
328
  // 直角 rect 四段累计 + 最近边分类(角区连续,见函数头注)
245
329
  vec2 h = shape.zw;
@@ -253,10 +337,16 @@ float strokeCoverage(vec2 local, vec2 pen, vec4 shape, vec3 dash, float k) {
253
337
  else s = 4.0 * h.x + 2.0 * h.y + (h.y - local.y);
254
338
  }
255
339
  float m = mod(s + dash.z, dash.y);
256
- // 到 on 窗口 [0,on) 边界的带符号内距:m<on 取 min(m, on−m)(两侧边界
257
- // 各为路径正向/尾向);m≥on 取到尾侧 (m−on) 与环回侧 (cycle−m) 最近者
258
- // 的负值——wrap 边界即下周期 m=0,两侧 ramp 在边界处对称衔接
259
- float e = m < dash.x ? min(m, dash.x - m) : -min(m - dash.x, dash.y - m);
340
+ // 方帽扩展窗(批次 386):cap==2 时窗两侧各延 halfW(u=mod(m+capExt,
341
+ // cycle),on2=on+2·capExt)——band×扩展窗交集=端边垂直路径的方帽精确形
342
+ // (canvas 逐 dash 半宽区间扩展同义;gap≤2·halfW 帽衔接成实线);
343
+ // butt=capExt 0 逐字退化现状。到窗 [0,on2) 边界的带符号内距:u<on2 取
344
+ // min(u, on2−u);u≥on2 取到尾侧 (u−on2) 与环回侧 (cycle−u) 最近者的
345
+ // 负值——wrap 边界即下周期 u=0,两侧 ramp 在边界处对称衔接
346
+ float capExt = cap == 2 ? halfW : 0.0;
347
+ float u = mod(m + capExt, dash.y);
348
+ float on2 = dash.x + 2.0 * capExt;
349
+ float e = u < on2 ? min(u, on2 - u) : -min(u - on2, dash.y - u);
260
350
  cov *= clamp(e / max(fwidth(s), 2e-4) + 0.5, 0.0, 1.0);
261
351
  }
262
352
  return cov;
@@ -265,7 +355,7 @@ float strokeCoverage(vec2 local, vec2 pen, vec4 shape, vec3 dash, float k) {
265
355
  void main() {
266
356
  fragColor = paintColor(vLocal, texture(uTex, vUv)) * vColor;
267
357
  // 描边码取负(vRadius.y<−0.5 即描边实例);填充/纹理实例 radius.y≥0 走圆角
268
- fragColor.a *= -vRadius.y > 0.5 ? strokeCoverage(vLocal, vRadius, vShape, vDash, uPenScale) : roundedCoverage(vLocal, vHalf, vRadius);
358
+ fragColor.a *= -vRadius.y > 0.5 ? strokeCoverage(vLocal, vRadius, vShape, vDash, vSeg, uPenScale) : roundedCoverage(vLocal, vHalf, vRadius);
269
359
  }
270
360
  `;
271
361
  /**
@@ -348,6 +438,7 @@ export const compileProgram = (gl, vertSrc, fragSrc) => {
348
438
  view: gl.getUniformLocation(program, 'uView'),
349
439
  tex: gl.getUniformLocation(program, 'uTex'),
350
440
  penScale: gl.getUniformLocation(program, 'uPenScale'),
441
+ segTex: gl.getUniformLocation(program, 'uSegTex'),
351
442
  paintMode: gl.getUniformLocation(program, 'uPaintMode'),
352
443
  paintInv: gl.getUniformLocation(program, 'uPaintInv'),
353
444
  paintA: gl.getUniformLocation(program, 'uPaintA'),
@@ -13,7 +13,7 @@ export declare class ShadowPainter {
13
13
  render(node: SceneNode): void;
14
14
  /** 失效(context lost 与字体迟到同口径):烘焙真删(批次 375——Canvas 回退
15
15
  * 路径自建纹理经登记册出清,原整换 WeakMap 弃管 = GL 滞留至 context loss;
16
- * GPU 路径纹理归 gpuShadowBaker 自管,维持其既有弃管口径)+ 纪元 bump
16
+ * GPU 路径纹理经批次 392 登记册同真删)+ 纪元 bump
17
17
  * (真删后保留式快照若持引用——重放死句柄即阴影消失,经纪元入门链) */
18
18
  invalidate(): void;
19
19
  }
@@ -47,7 +47,7 @@ export class ShadowPainter {
47
47
  const sig = [shadowKey(decl), node.raw.type, node.matrix.join(','), node.dims.x, node.dims.y, content, animVersion].join(';');
48
48
  if (isGpuShadowEnabled()) {
49
49
  if (!this.gpuShadowBaker)
50
- this.gpuShadowBaker = new GpuShadowBaker();
50
+ this.gpuShadowBaker = new GpuShadowBaker(this.host.renderEpoch);
51
51
  const bake = this.gpuShadowBaker.render(this.host.gl, node, decl, this.host.canvasEl.width, this.host.canvasEl.height);
52
52
  if (bake)
53
53
  this.host.batch.pushQuad([1, 0, 0, 1, node.matrix[4], node.matrix[5]], bake.w, bake.h, [1, 1, 1, node.renderOpacity], bake.tex);
@@ -80,7 +80,7 @@ export class ShadowPainter {
80
80
  }
81
81
  /** 失效(context lost 与字体迟到同口径):烘焙真删(批次 375——Canvas 回退
82
82
  * 路径自建纹理经登记册出清,原整换 WeakMap 弃管 = GL 滞留至 context loss;
83
- * GPU 路径纹理归 gpuShadowBaker 自管,维持其既有弃管口径)+ 纪元 bump
83
+ * GPU 路径纹理经批次 392 登记册同真删)+ 纪元 bump
84
84
  * (真删后保留式快照若持引用——重放死句柄即阴影消失,经纪元入门链) */
85
85
  invalidate() {
86
86
  for (const tex of this.liveTexes)
@@ -88,6 +88,6 @@ export class ShadowPainter {
88
88
  this.liveTexes.clear();
89
89
  this.shadowBakes = new WeakMap();
90
90
  this.host.renderEpoch?.bump('shadow-bake-invalidate');
91
- this.gpuShadowBaker?.invalidate();
91
+ this.gpuShadowBaker?.invalidate(this.host.gl);
92
92
  }
93
93
  }
@@ -9,12 +9,14 @@
9
9
  */
10
10
  import { GpuFxPainter } from './gpuFx';
11
11
  import { GpuImageFilterChain } from './gpuImageFilters';
12
+ import type { RenderEpoch } from './renderEpoch';
12
13
  export declare class SharedGpuPainters {
13
14
  private readonly gl;
15
+ private readonly epoch?;
14
16
  private fxPainter;
15
17
  private filterChain;
16
- constructor(gl: WebGL2RenderingContext);
17
- /** PS 图层样式画家(惰性;clipMask/PSD 效果共用) */
18
+ constructor(gl: WebGL2RenderingContext, epoch?: RenderEpoch | undefined);
19
+ /** PS 图层样式画家(惰性;clipMask/PSD 效果共用;纪元接入批次 392) */
18
20
  get fx(): GpuFxPainter;
19
21
  /** 图片滤镜 GL 链(惰性;图片分支/Mask clipImage 读回共用) */
20
22
  get filters(): GpuImageFilterChain;
@@ -11,15 +11,17 @@ import { GpuFxPainter } from './gpuFx';
11
11
  import { GpuImageFilterChain } from './gpuImageFilters';
12
12
  export class SharedGpuPainters {
13
13
  gl;
14
+ epoch;
14
15
  fxPainter = null;
15
16
  filterChain = null;
16
- constructor(gl) {
17
+ constructor(gl, epoch) {
17
18
  this.gl = gl;
19
+ this.epoch = epoch;
18
20
  }
19
- /** PS 图层样式画家(惰性;clipMask/PSD 效果共用) */
21
+ /** PS 图层样式画家(惰性;clipMask/PSD 效果共用;纪元接入批次 392) */
20
22
  get fx() {
21
23
  if (!this.fxPainter)
22
- this.fxPainter = new GpuFxPainter();
24
+ this.fxPainter = new GpuFxPainter(this.epoch);
23
25
  return this.fxPainter;
24
26
  }
25
27
  /** 图片滤镜 GL 链(惰性;图片分支/Mask clipImage 读回共用) */
@@ -30,11 +32,11 @@ export class SharedGpuPainters {
30
32
  }
31
33
  /** context lost 恢复:程序/RT/签名缓存全失效(实例保留,走惰性重建) */
32
34
  invalidate() {
33
- this.fxPainter?.invalidate();
35
+ this.fxPainter?.invalidate(this.gl);
34
36
  this.filterChain?.invalidate();
35
37
  }
36
38
  /** 字体迟到失效:仅 fx 的文本派生源/目标缓存 */
37
39
  invalidateTextDerivedBakes() {
38
- this.fxPainter?.invalidateTextDerivedBakes();
40
+ this.fxPainter?.invalidateTextDerivedBakes(this.gl);
39
41
  }
40
42
  }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * miter join 描边的双偏移轮廓构造(方向三第二刀收尾,批次 385,
3
+ * docs/2026-10-14_webgl2-sdf-miter-stroke-blueprint.md §0)。
4
+ *
5
+ * 383 两条近路(延长端点盒并/凸线距)的死因都是「用中心线段的距离逼近描边
6
+ * 区域」——miter 区域在角部不是中心线等距带。本模块反过来**直接表示区域**:
7
+ * 描边区域 = +h/-h 两条偏移轮廓围成的环带,每侧轮廓逐顶点取「该侧相邻偏移线
8
+ * 的交点」(凸顶点一侧=miter 尖、另一侧=内角点;凹顶点角色互换——同一构造
9
+ * 无分支)。着色器侧单次 winding 填充(shaders.ts kind==4 模式 1):带内
10
+ * |wind|≥1、洞 wind=0;粗笔宽内轮廓反转/自交由绕数语义天然正确(蓝图 §0.4
11
+ * 三重自愈论证——canvas 对 stroke 几何按 nonzero 填充同口径)。
12
+ *
13
+ * 内角点=偏移线交点(非两内端点的弦)的依据(蓝图 §0.3):canvas 逐段 quad
14
+ * 是垂直端边,单独一条 quad 已覆盖端边后的整条方角——90° 角解析例中并集的
15
+ * 内边界角点落在 (−h,h) 即两内偏移线交点上。
16
+ *
17
+ * 回落(返回 null)——canvas 语义不可由本构造精确表达的形态:
18
+ * - 任一顶点 miter 比 1/sin(θ/2) > miterLimit(canvas 超限走 bevel 截断,
19
+ * 「只减不增」语义轮廓不承接;整节点回落避免半解析半烘焙混血形态);
20
+ * - 相邻共线反向(回折尖刺,θ=0 的退化);
21
+ * - 段数(闭合 2N / 开放 2N)超 MAX_SEG_BATCH、坐标非有限。
22
+ */
23
+ import { type StrokeSdfSegments } from './segmentTable';
24
+ export interface MiterOutlinePack {
25
+ segments: StrokeSdfSegments;
26
+ /** 墨迹超中心线 bbox 的逐轴余量(轮廓顶点外沿 − bbox 半宽;halfW 已在轮廓里) */
27
+ marginX: number;
28
+ marginY: number;
29
+ }
30
+ /**
31
+ * @param pts 中心系折线点(调用方已做 points − bbox 中心归一)
32
+ * @param closed 闭合 polygon(含 triangle);开放 polyline 的端帽语义=butt
33
+ * @param halfW 本地半笔宽(non-uniform——uniform 笔宽本地各向异性无静态轮廓)
34
+ * @param miterLimit 缺省 4(core/stroke.ts 同缺省)
35
+ */
36
+ export declare const buildMiterOutline: (pts: ReadonlyArray<{
37
+ x: number;
38
+ y: number;
39
+ }>, closed: boolean, halfW: number, miterLimit: number) => MiterOutlinePack | null;
@@ -0,0 +1,224 @@
1
+ /**
2
+ * miter join 描边的双偏移轮廓构造(方向三第二刀收尾,批次 385,
3
+ * docs/2026-10-14_webgl2-sdf-miter-stroke-blueprint.md §0)。
4
+ *
5
+ * 383 两条近路(延长端点盒并/凸线距)的死因都是「用中心线段的距离逼近描边
6
+ * 区域」——miter 区域在角部不是中心线等距带。本模块反过来**直接表示区域**:
7
+ * 描边区域 = +h/-h 两条偏移轮廓围成的环带,每侧轮廓逐顶点取「该侧相邻偏移线
8
+ * 的交点」(凸顶点一侧=miter 尖、另一侧=内角点;凹顶点角色互换——同一构造
9
+ * 无分支)。着色器侧单次 winding 填充(shaders.ts kind==4 模式 1):带内
10
+ * |wind|≥1、洞 wind=0;粗笔宽内轮廓反转/自交由绕数语义天然正确(蓝图 §0.4
11
+ * 三重自愈论证——canvas 对 stroke 几何按 nonzero 填充同口径)。
12
+ *
13
+ * 内角点=偏移线交点(非两内端点的弦)的依据(蓝图 §0.3):canvas 逐段 quad
14
+ * 是垂直端边,单独一条 quad 已覆盖端边后的整条方角——90° 角解析例中并集的
15
+ * 内边界角点落在 (−h,h) 即两内偏移线交点上。
16
+ *
17
+ * 回落(返回 null)——canvas 语义不可由本构造精确表达的形态:
18
+ * - 任一顶点 miter 比 1/sin(θ/2) > miterLimit(canvas 超限走 bevel 截断,
19
+ * 「只减不增」语义轮廓不承接;整节点回落避免半解析半烘焙混血形态);
20
+ * - 相邻共线反向(回折尖刺,θ=0 的退化);
21
+ * - 段数(闭合 2N / 开放 2N)超 MAX_SEG_BATCH、坐标非有限。
22
+ */
23
+ import { MAX_SEG_BATCH, segHash } from './segmentTable';
24
+ /**
25
+ * @param pts 中心系折线点(调用方已做 points − bbox 中心归一)
26
+ * @param closed 闭合 polygon(含 triangle);开放 polyline 的端帽语义=butt
27
+ * @param halfW 本地半笔宽(non-uniform——uniform 笔宽本地各向异性无静态轮廓)
28
+ * @param miterLimit 缺省 4(core/stroke.ts 同缺省)
29
+ */
30
+ export const buildMiterOutline = (pts, closed, halfW, miterLimit) => {
31
+ // 相邻零长点去重(fabric Polyline moveTo(p0)+lineTo(p0) 怪癖族);闭合首尾重复同去
32
+ const p = [];
33
+ for (const q of pts) {
34
+ if (!Number.isFinite(q.x) || !Number.isFinite(q.y))
35
+ return null;
36
+ const last = p[p.length - 1];
37
+ if (last && last.x === q.x && last.y === q.y)
38
+ continue;
39
+ p.push({ x: q.x, y: q.y });
40
+ }
41
+ if (closed && p.length > 1) {
42
+ const first = p[0];
43
+ const last = p[p.length - 1];
44
+ if (first.x === last.x && first.y === last.y)
45
+ p.pop();
46
+ }
47
+ const n = p.length;
48
+ if (n < (closed ? 3 : 2))
49
+ return null;
50
+ // 逐边单位方向(边 i:p[i] → p[(i+1)%n];开放链末边 = p[n−2]→p[n−1])
51
+ const edgeCount = closed ? n : n - 1;
52
+ const ux = [];
53
+ const uy = [];
54
+ for (let i = 0; i < edgeCount; i++) {
55
+ const a = p[i];
56
+ const b = p[(i + 1) % n];
57
+ const len = Math.hypot(b.x - a.x, b.y - a.y);
58
+ if (!(len > 1e-12))
59
+ return null;
60
+ ux.push((b.x - a.x) / len);
61
+ uy.push((b.y - a.y) / len);
62
+ }
63
+ // 双侧轮廓点(顶点序):+ 侧 = +h·n(左法线),− 侧 = −h·n
64
+ const px = [];
65
+ const py = [];
66
+ const mx = [];
67
+ const my = [];
68
+ for (let i = 0; i < n; i++) {
69
+ const v = p[i];
70
+ const hasPrev = closed || i > 0;
71
+ const hasNext = closed || i < edgeCount;
72
+ if (!hasPrev || !hasNext) {
73
+ // 开放链端点:无接头,侧点 = v ± h·(相邻边法线)——butt 帽的垂直端边端点
74
+ const e = hasPrev ? i - 1 : 0;
75
+ const nx = -uy[e];
76
+ const ny = ux[e];
77
+ px.push(v.x + halfW * nx);
78
+ py.push(v.y + halfW * ny);
79
+ mx.push(v.x - halfW * nx);
80
+ my.push(v.y - halfW * ny);
81
+ continue;
82
+ }
83
+ const u1x = ux[(i - 1 + edgeCount) % edgeCount];
84
+ const u1y = uy[(i - 1 + edgeCount) % edgeCount];
85
+ const u2x = ux[i % edgeCount];
86
+ const u2y = uy[i % edgeCount];
87
+ // 接头内角 θ(顶点两射线 −u1 与 u2 夹角):cosθ = −(u1·u2);miter 比 = 1/sin(θ/2)。
88
+ // sinHalf=0 ↔ θ=0 回折尖刺(1/sinHalf=Infinity 自然被比检查拦下)
89
+ const cosTheta = -(u1x * u2x + u1y * u2y);
90
+ const sinHalf = Math.sqrt(Math.max(0, (1 - cosTheta) / 2));
91
+ if (1 / Math.max(sinHalf, 1e-300) > miterLimit * (1 + 1e-9))
92
+ return null;
93
+ const denom = u1x * u2y - u1y * u2x;
94
+ for (const s of [1, -1]) {
95
+ // 偏移线 1 过 prev + s·h·n1(方向 u1)、偏移线 2 过 v + s·h·n2(方向 u2);
96
+ // 交点 V = A1 + t·u1,t = cross(A2−A1, u2)/cross(u1, u2)
97
+ const a1x = p[(i - 1 + n) % n].x + halfW * s * -u1y;
98
+ const a1y = p[(i - 1 + n) % n].y + halfW * s * u1x;
99
+ const a2x = v.x + halfW * s * -u2y;
100
+ const a2y = v.y + halfW * s * u2x;
101
+ if (Math.abs(denom) < 1e-12) {
102
+ // 共线同向(θ=π 已过比检查):两侧偏移线重合,直通点 = v + s·h·n
103
+ const qx = v.x + halfW * s * -u2y;
104
+ const qy = v.y + halfW * s * u2x;
105
+ if (!Number.isFinite(qx) || !Number.isFinite(qy))
106
+ return null;
107
+ if (s === 1) {
108
+ px.push(qx);
109
+ py.push(qy);
110
+ }
111
+ else {
112
+ mx.push(qx);
113
+ my.push(qy);
114
+ }
115
+ continue;
116
+ }
117
+ const wx = a2x - a1x;
118
+ const wy = a2y - a1y;
119
+ const t = (wx * u2y - wy * u2x) / denom;
120
+ const qx = a1x + t * u1x;
121
+ const qy = a1y + t * u1y;
122
+ if (!Number.isFinite(qx) || !Number.isFinite(qy))
123
+ return null;
124
+ if (s === 1) {
125
+ px.push(qx);
126
+ py.push(qy);
127
+ }
128
+ else {
129
+ mx.push(qx);
130
+ my.push(qy);
131
+ }
132
+ }
133
+ }
134
+ // − 侧反向遍历(带内 wind=±1、洞=0 的方向配对;多边形原始绕向两种都成立)
135
+ // 方向守卫先于 reverse(顶点序下 ± 链边 i 与中心线边 i 同索引):
136
+ // 偏移边相对源边**反向** = 内缩反转域(粗笔宽 h>内切半径,如方 h≥hx 时内链
137
+ // 折叠成反向小方——顶点交点链的 winding 填充会产生 canvas 没有的洞:canvas
138
+ // 逐段矩形并集此时盖满全形)/ 外链被双端接头吞没——这些域顶点交点链不再
139
+ // 表示区域,整节点回落烘焙(canvas 语义原样)。零长偏移边(恰退化)同拒
140
+ for (let i = 0; i < edgeCount; i++) {
141
+ const j = i + 1;
142
+ if ((px[j] - px[i]) * ux[i] + (py[j] - py[i]) * uy[i] <= 0)
143
+ return null;
144
+ if ((mx[j] - mx[i]) * ux[i] + (my[j] - my[i]) * uy[i] <= 0)
145
+ return null;
146
+ }
147
+ mx.reverse();
148
+ my.reverse();
149
+ // 闭合=+链 n + −链 n;开放=两链各 (n−1) + 两帽边 = 2n
150
+ const total = 2 * n;
151
+ if (total > MAX_SEG_BATCH || total < 3)
152
+ return null;
153
+ const ox = [];
154
+ const oy = [];
155
+ const pushChain = (cx, cy, closeChain) => {
156
+ for (let i = 0; i + 1 < cx.length; i++) {
157
+ ox.push(cx[i], cx[i + 1]);
158
+ oy.push(cy[i], cy[i + 1]);
159
+ }
160
+ if (closeChain && cx.length > 2) {
161
+ ox.push(cx[cx.length - 1], cx[0]);
162
+ oy.push(cy[cy.length - 1], cy[0]);
163
+ }
164
+ };
165
+ pushChain(px, py, closed);
166
+ pushChain(mx, my, closed);
167
+ if (!closed) {
168
+ // 开放链两端的 butt 帽边:+侧末端 → −侧首端(即几何末端),−侧末端 → +侧首端
169
+ ox.push(px[px.length - 1], mx[0]);
170
+ oy.push(py[py.length - 1], my[0]);
171
+ ox.push(mx[mx.length - 1], px[0]);
172
+ oy.push(my[my.length - 1], py[0]);
173
+ }
174
+ const count = ox.length / 2;
175
+ if (count !== total || count > MAX_SEG_BATCH)
176
+ return null;
177
+ // 全对真交守卫:任两条发射段严格相交(同链自交=凹形粗笔宽内/外轮廓跨过
178
+ // 自己;跨链相交=两轮廓不嵌套)→ winding 语义与 canvas 并集不再一致,回落。
179
+ // 严格判定天然豁免共端点的相邻段与零测度接触;零长段(恰退化接头)跳过
180
+ const cross2 = (ax, ay, bx, by, cx, cy) => (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
181
+ const properCross = (i, j) => {
182
+ const ax = ox[i * 2];
183
+ const ay = oy[i * 2];
184
+ const bx = ox[i * 2 + 1];
185
+ const by = oy[i * 2 + 1];
186
+ const cx = ox[j * 2];
187
+ const cy = oy[j * 2];
188
+ const dx = ox[j * 2 + 1];
189
+ const dy = oy[j * 2 + 1];
190
+ if ((ax === bx && ay === by) || (cx === dx && cy === dy))
191
+ return false;
192
+ const d1 = cross2(ax, ay, bx, by, cx, cy);
193
+ const d2 = cross2(ax, ay, bx, by, dx, dy);
194
+ const d3 = cross2(cx, cy, dx, dy, ax, ay);
195
+ const d4 = cross2(cx, cy, dx, dy, bx, by);
196
+ return ((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0));
197
+ };
198
+ for (let i = 0; i < count; i++) {
199
+ for (let j = i + 2; j < count; j++) {
200
+ if (properCross(i, j))
201
+ return null;
202
+ }
203
+ }
204
+ const data = new Float32Array(count * 4);
205
+ let absX = 0;
206
+ let absY = 0;
207
+ let hx = 0;
208
+ let hy = 0;
209
+ for (let i = 0; i < count; i++) {
210
+ data[i * 4] = ox[i * 2];
211
+ data[i * 4 + 1] = oy[i * 2];
212
+ data[i * 4 + 2] = ox[i * 2 + 1];
213
+ data[i * 4 + 3] = oy[i * 2 + 1];
214
+ absX = Math.max(absX, Math.abs(ox[i * 2]), Math.abs(ox[i * 2 + 1]));
215
+ absY = Math.max(absY, Math.abs(oy[i * 2]), Math.abs(oy[i * 2 + 1]));
216
+ }
217
+ for (const q of p) {
218
+ hx = Math.max(hx, Math.abs(q.x));
219
+ hy = Math.max(hy, Math.abs(q.y));
220
+ }
221
+ if (!(hx > 0) || !(hy > 0))
222
+ return null;
223
+ return { segments: { data, count, hash: segHash(data) }, marginX: absX - hx, marginY: absY - hy };
224
+ };
@@ -15,6 +15,9 @@ export declare class StrokePainter {
15
15
  * 缩放/平移手势,见 noteVptChange)。lx/ly = 对齐后铺放左上角(snapped 时随
16
16
  * vpt 平移跨半像素界翻转同样延迟重烘,sceneW/H 供确定性重算) */
17
17
  private strokeBakes;
18
+ /** 线族装饰文本标签桶化烘焙缓存(批次 388):sig 含 pickZoomBucket(有效
19
+ * 缩放)而非连续 k——重烘只在桶边界(~2× 步进),画布=标签盒小纹理 */
20
+ private lineDecoTextBakes;
18
21
  /** 最近一次视口写点时刻(stale 判定的手势窗口)与 settle 定时器——统一
19
22
  * 收敛至 SettleScheduler(批次372),见其头注 */
20
23
  constructor(host: SceneRendererHost, frame: FrameState, settle?: SettleScheduler);
@@ -29,6 +32,14 @@ export declare class StrokePainter {
29
32
  render(node: SceneNode): void;
30
33
  /** 烘焙纹理删除统一口(批次 375):登记册同步出册 */
31
34
  private deleteBakeTex;
35
+ /**
36
+ * Dimension 文本标签桶化小烘焙(批次 388):bakeLineDecoration 文本段同式
37
+ * (居中/follow 旋转/bg 盒),光栅分辨率=pickZoomBucket(zoom×dpr×max 世界
38
+ * 缩放)——与字形桶同口径的量化档,重烘只在桶边界(对照整元素烘焙的连续
39
+ * k=zoom×dpr 每格滚轮重烘)。标签 quad=盒尺寸,矩阵=node.matrix∘T(标签
40
+ * 中心)∘R(follow 角)
41
+ */
42
+ private renderLineDecoText;
32
43
  /** 失效(context lost 与字体迟到同口径):烘焙真删(批次 375——原整换
33
44
  * WeakMap 弃管 = GL 纹理滞留至 context loss)+ 纪元 bump:真删后保留式
34
45
  * 快照命令表若持引用,重放死句柄即描边消失(批次 225 同族,纹理级失效