@yft-design/engine 0.0.17 → 0.0.19
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/README.md +23 -11
- package/dist/EngineCanvas.d.ts +0 -255
- package/dist/core/color.d.ts +0 -11
- package/dist/core/distanceField.d.ts +0 -13
- package/dist/core/events.d.ts +0 -25
- package/dist/core/gradient.d.ts +0 -47
- package/dist/core/homography.d.ts +0 -20
- package/dist/core/imageFilters.d.ts +0 -31
- package/dist/core/mat2d.d.ts +0 -62
- package/dist/core/pathData.d.ts +0 -36
- package/dist/core/pattern.d.ts +0 -14
- package/dist/core/point.d.ts +0 -14
- package/dist/core/psdEffects.d.ts +0 -48
- package/dist/core/roundedRect.d.ts +0 -48
- package/dist/core/shadow.d.ts +0 -23
- package/dist/core/snap.d.ts +0 -103
- package/dist/core/stroke.d.ts +0 -13
- package/dist/core/triangulate.d.ts +0 -31
- package/dist/core/warpMesh.d.ts +0 -38
- package/dist/debug/canvasInspection.d.ts +0 -20
- package/dist/debug/pixelDiff.d.ts +0 -7
- package/dist/debug/pixelDiff.js +1 -64
- package/dist/debug/seed.d.ts +0 -426
- package/dist/debug/seed.js +28 -3806
- package/dist/decoders/webp2sprite.d.ts +0 -16
- package/dist/engineFlag.d.ts +0 -76
- package/dist/export/SceneExporter.d.ts +0 -58
- package/dist/export/glExport.d.ts +0 -41
- package/dist/export/svgExport.d.ts +0 -181
- package/dist/index.d.ts +1 -14
- package/dist/index.js +1060 -64
- package/dist/interact/animationPlayback.d.ts +0 -62
- package/dist/interact/canvasInteractions.d.ts +1 -38
- package/dist/interact/cropSession.d.ts +0 -68
- package/dist/interact/dblClick.d.ts +0 -32
- package/dist/interact/expandSession.d.ts +0 -46
- package/dist/interact/hotkeyActions.d.ts +0 -12
- package/dist/interact/pageTransfer.d.ts +0 -2
- package/dist/interact/pathSession.d.ts +0 -42
- package/dist/interact/pointer.d.ts +0 -87
- package/dist/interact/pointerTablePuzzle.d.ts +0 -60
- package/dist/interact/pointerTransform.d.ts +0 -48
- package/dist/interact/pointerTypes.d.ts +6 -148
- package/dist/interact/snapGuide.d.ts +0 -68
- package/dist/interact/tableCellEditing.d.ts +0 -34
- package/dist/interact/tableInteraction.d.ts +0 -31
- package/dist/interact/textEditing.d.ts +0 -44
- package/dist/interact/warpFacade.d.ts +0 -5
- package/dist/interact/warpSession.d.ts +0 -55
- package/dist/interact/wheelScroll.d.ts +0 -42
- package/dist/model/alphaHit.d.ts +0 -51
- package/dist/model/animation.d.ts +0 -99
- package/dist/model/arcLayout.d.ts +0 -33
- package/dist/model/arcTextTransform.d.ts +0 -4
- package/dist/model/compatEvents.d.ts +0 -72
- package/dist/model/compatObject.d.ts +0 -360
- package/dist/model/cropEdit.d.ts +0 -60
- package/dist/model/document.d.ts +0 -167
- package/dist/model/elementTypes.d.ts +0 -47
- package/dist/model/expandEdit.d.ts +0 -36
- package/dist/model/fabricPrimitives.d.ts +0 -24
- package/dist/model/filler.d.ts +0 -12
- package/dist/model/groupAssemble.d.ts +0 -5
- package/dist/model/groupChildMove.d.ts +0 -37
- package/dist/model/groupResize.d.ts +0 -42
- package/dist/model/handles.d.ts +0 -24
- package/dist/model/imageMaskBake.d.ts +0 -50
- package/dist/model/lineConstruct.d.ts +0 -8
- package/dist/model/maskCropEdit.d.ts +0 -34
- package/dist/model/pathBackground.d.ts +0 -33
- package/dist/model/pathConstruct.d.ts +0 -24
- package/dist/model/pathEdit.d.ts +0 -55
- package/dist/model/puzzleEdit.d.ts +0 -66
- package/dist/model/puzzleLayout.d.ts +0 -31
- package/dist/model/scene.d.ts +0 -603
- package/dist/model/schema.d.ts +0 -48
- package/dist/model/shapePath.d.ts +0 -5
- package/dist/model/smartObjectBake.d.ts +0 -6
- package/dist/model/tableEdit.d.ts +0 -90
- package/dist/model/tableFacade.d.ts +0 -5
- package/dist/model/tableLayout.d.ts +0 -40
- package/dist/model/textAlign.d.ts +0 -22
- package/dist/model/textLayout.d.ts +0 -213
- package/dist/render/GLBlend.d.ts +0 -24
- package/dist/render/GLContext.d.ts +0 -11
- package/dist/render/GLIsolate.d.ts +0 -25
- package/dist/render/GLTextureCache.d.ts +0 -72
- package/dist/render/GlyphAtlas.d.ts +0 -98
- package/dist/render/MeshBatch.d.ts +0 -71
- package/dist/render/QuadBatch.d.ts +0 -87
- package/dist/render/SceneRenderer.d.ts +0 -137
- package/dist/render/SourceManager.d.ts +0 -82
- package/dist/render/TextRenderer.d.ts +0 -120
- package/dist/render/arcText.d.ts +0 -28
- package/dist/render/bakeGate.d.ts +0 -35
- package/dist/render/batchOrder.d.ts +0 -60
- package/dist/render/blendMode.d.ts +0 -27
- package/dist/render/clipMaskPainter.d.ts +0 -42
- package/dist/render/fontInvalidation.d.ts +0 -18
- package/dist/render/fontInvalidationController.d.ts +0 -52
- package/dist/render/frameOverlay.d.ts +0 -32
- package/dist/render/framePipeline.d.ts +0 -27
- package/dist/render/gpuFilterPasses.d.ts +0 -15
- package/dist/render/gpuFx.d.ts +0 -82
- package/dist/render/gpuImageFilters.d.ts +0 -41
- package/dist/render/gpuShadow.d.ts +0 -38
- package/dist/render/gradientRamp.d.ts +0 -14
- package/dist/render/imageEffects.d.ts +0 -99
- package/dist/render/lineDecorationSdf.d.ts +0 -32
- package/dist/render/maskRender.d.ts +0 -60
- package/dist/render/multiPage.d.ts +0 -91
- package/dist/render/progressiveLoad.d.ts +0 -27
- package/dist/render/psdEffectPainter.d.ts +0 -25
- package/dist/render/psdWarpText.d.ts +0 -19
- package/dist/render/puzzleClamp.d.ts +0 -15
- package/dist/render/renderEpoch.d.ts +0 -21
- package/dist/render/retainedFrame.d.ts +0 -36
- package/dist/render/ruler.d.ts +0 -36
- package/dist/render/screenClip.d.ts +0 -31
- package/dist/render/scrollText.d.ts +0 -46
- package/dist/render/segmentTable.d.ts +0 -30
- package/dist/render/selectionOverlay.d.ts +0 -203
- package/dist/render/settleScheduler.d.ts +0 -24
- package/dist/render/shaders.d.ts +4 -24
- package/dist/render/shadowBake.d.ts +0 -28
- package/dist/render/shadowPainter.d.ts +0 -8
- package/dist/render/sharedGpuPainters.d.ts +0 -13
- package/dist/render/sourceHydration.d.ts +0 -8
- package/dist/render/strokeBake.d.ts +0 -59
- package/dist/render/strokeMiterOutline.d.ts +0 -29
- package/dist/render/strokePainter.d.ts +0 -31
- package/dist/render/strokeSdf.d.ts +0 -52
- package/dist/render/svgRaster.d.ts +0 -34
- package/dist/render/tableOverlay.d.ts +0 -35
- package/dist/render/textHighlight.d.ts +0 -67
- package/dist/render/textureUpload.d.ts +0 -13
- package/dist/render/verticalText.d.ts +0 -21
- package/dist/render/warpText.d.ts +0 -48
- package/dist/render/workspaceBackdrop.d.ts +0 -22
- package/dist/render/workspaceChecker.d.ts +0 -34
- package/dist/render/workspaceGlow.d.ts +0 -38
- package/package.json +4 -3
- package/dist/EngineCanvas.js +0 -1202
- package/dist/core/canvasPerf.js +0 -170
- package/dist/core/color.js +0 -107
- package/dist/core/distanceField.js +0 -92
- package/dist/core/events.js +0 -80
- package/dist/core/gradient.js +0 -175
- package/dist/core/homography.js +0 -123
- package/dist/core/imageFilters.js +0 -330
- package/dist/core/mat2d.js +0 -241
- package/dist/core/pathData.js +0 -926
- package/dist/core/pattern.js +0 -40
- package/dist/core/point.js +0 -119
- package/dist/core/psdEffects.js +0 -158
- package/dist/core/roundedRect.js +0 -164
- package/dist/core/shadow.js +0 -47
- package/dist/core/snap.js +0 -793
- package/dist/core/stroke.js +0 -45
- package/dist/core/triangulate.js +0 -194
- package/dist/core/warpMesh.js +0 -469
- package/dist/debug/canvasInspection.js +0 -113
- package/dist/decoders/gif2sprite.js +0 -84
- package/dist/decoders/webp2sprite.js +0 -133
- package/dist/engineFlag.js +0 -104
- package/dist/export/SceneExporter.js +0 -286
- package/dist/export/glExport.js +0 -79
- package/dist/export/svgExport.js +0 -1803
- package/dist/interact/animationPlayback.js +0 -358
- package/dist/interact/canvasInteractions.js +0 -174
- package/dist/interact/cropSession.js +0 -527
- package/dist/interact/dblClick.js +0 -120
- package/dist/interact/expandSession.js +0 -282
- package/dist/interact/hotkeyActions.js +0 -42
- package/dist/interact/pageTransfer.js +0 -56
- package/dist/interact/pathSession.js +0 -480
- package/dist/interact/pointer.js +0 -1692
- package/dist/interact/pointerTablePuzzle.js +0 -358
- package/dist/interact/pointerTransform.js +0 -441
- package/dist/interact/pointerTypes.js +0 -96
- package/dist/interact/snapGuide.js +0 -377
- package/dist/interact/tableCellEditing.js +0 -207
- package/dist/interact/tableInteraction.js +0 -219
- package/dist/interact/textEditing.js +0 -423
- package/dist/interact/warpFacade.js +0 -51
- package/dist/interact/warpSession.js +0 -583
- package/dist/interact/wheelScroll.js +0 -100
- package/dist/model/alphaHit.js +0 -267
- package/dist/model/animation.js +0 -510
- package/dist/model/arcLayout.js +0 -308
- package/dist/model/arcTextTransform.js +0 -87
- package/dist/model/compatEvents.js +0 -96
- package/dist/model/compatObject.js +0 -1583
- package/dist/model/cropEdit.js +0 -497
- package/dist/model/document.js +0 -590
- package/dist/model/elementTypes.js +0 -119
- package/dist/model/expandEdit.js +0 -161
- package/dist/model/fabricPrimitives.js +0 -9
- package/dist/model/filler.js +0 -1
- package/dist/model/groupAssemble.js +0 -98
- package/dist/model/groupChildMove.js +0 -145
- package/dist/model/groupResize.js +0 -109
- package/dist/model/handles.js +0 -29
- package/dist/model/imageMaskBake.js +0 -136
- package/dist/model/lineConstruct.js +0 -79
- package/dist/model/maskCropEdit.js +0 -90
- package/dist/model/pathBackground.js +0 -196
- package/dist/model/pathConstruct.js +0 -55
- package/dist/model/pathEdit.js +0 -378
- package/dist/model/puzzleEdit.js +0 -742
- package/dist/model/puzzleLayout.js +0 -143
- package/dist/model/scene.js +0 -2930
- package/dist/model/schema.js +0 -13
- package/dist/model/shapePath.js +0 -137
- package/dist/model/smartObjectBake.js +0 -295
- package/dist/model/tableEdit.js +0 -751
- package/dist/model/tableFacade.js +0 -808
- package/dist/model/tableLayout.js +0 -194
- package/dist/model/textAlign.js +0 -36
- package/dist/model/textLayout.js +0 -842
- package/dist/render/GLBlend.js +0 -270
- package/dist/render/GLContext.js +0 -48
- package/dist/render/GLIsolate.js +0 -188
- package/dist/render/GLTextureCache.js +0 -224
- package/dist/render/GlyphAtlas.js +0 -412
- package/dist/render/MeshBatch.js +0 -416
- package/dist/render/QuadBatch.js +0 -561
- package/dist/render/SceneRenderer.js +0 -753
- package/dist/render/SourceManager.js +0 -523
- package/dist/render/TextRenderer.js +0 -854
- package/dist/render/arcText.js +0 -187
- package/dist/render/bakeGate.js +0 -32
- package/dist/render/batchOrder.js +0 -168
- package/dist/render/blendMode.js +0 -142
- package/dist/render/clipMaskPainter.js +0 -244
- package/dist/render/fontInvalidation.js +0 -77
- package/dist/render/fontInvalidationController.js +0 -119
- package/dist/render/frameOverlay.js +0 -124
- package/dist/render/framePipeline.js +0 -96
- package/dist/render/gpuFilterPasses.js +0 -883
- package/dist/render/gpuFx.js +0 -673
- package/dist/render/gpuImageFilters.js +0 -295
- package/dist/render/gpuShadow.js +0 -327
- package/dist/render/gradientRamp.js +0 -74
- package/dist/render/imageEffects.js +0 -415
- package/dist/render/lineDecorationSdf.js +0 -189
- package/dist/render/maskRender.js +0 -217
- package/dist/render/multiPage.js +0 -420
- package/dist/render/progressiveLoad.js +0 -86
- package/dist/render/psdEffectPainter.js +0 -333
- package/dist/render/psdWarpText.js +0 -212
- package/dist/render/puzzleClamp.js +0 -30
- package/dist/render/renderEpoch.js +0 -35
- package/dist/render/renderViewport.js +0 -18
- package/dist/render/retainedFrame.js +0 -192
- package/dist/render/ruler.js +0 -287
- package/dist/render/screenClip.js +0 -67
- package/dist/render/scrollText.js +0 -266
- package/dist/render/segmentTable.js +0 -117
- package/dist/render/selectionOverlay.js +0 -1008
- package/dist/render/settleScheduler.js +0 -51
- package/dist/render/shaders.js +0 -450
- package/dist/render/shadowBake.js +0 -203
- package/dist/render/shadowPainter.js +0 -93
- package/dist/render/sharedGpuPainters.js +0 -42
- package/dist/render/sourceHydration.js +0 -48
- package/dist/render/strokeBake.js +0 -516
- package/dist/render/strokeMiterOutline.js +0 -224
- package/dist/render/strokePainter.js +0 -207
- package/dist/render/strokeSdf.js +0 -262
- package/dist/render/svgRaster.js +0 -71
- package/dist/render/tableOverlay.js +0 -233
- package/dist/render/textHighlight.js +0 -380
- package/dist/render/textureUpload.js +0 -26
- package/dist/render/verticalText.js +0 -63
- package/dist/render/warpText.js +0 -384
- package/dist/render/workspaceBackdrop.js +0 -60
- package/dist/render/workspaceChecker.js +0 -170
- package/dist/render/workspaceGlow.js +0 -198
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 纹理上传与缓存(main-webgl2 Phase 0 → P6-2 纹理内存预算)。
|
|
3
|
-
*
|
|
4
|
-
* 以图像源对象(HTMLImageElement/HTMLCanvasElement/HTMLVideoElement)为 key
|
|
5
|
-
* 缓存 WebGLTexture,重复渲染零上传。P6-2 起纳入内存预算(批次 321 语义修订):
|
|
6
|
-
* - **LRU 淘汰(插入压力驱动)**:Map 插入序即访问序(命中 delete+set 前移),
|
|
7
|
-
* 新纹理插入触顶时从最旧端淘汰;淘汰只是丢缓存条目(源元素仍在场景节点上),
|
|
8
|
-
* 下次 get() 全量重传——正确性由构造保证。**帧界(markFrame)不再强制结算**:
|
|
9
|
-
* 工作集超预算时逐帧「逐出→重传」是必现颠簸(452MB PSD 导入实测:793MiB
|
|
10
|
-
* 工作集 vs 512MiB 预算,每帧强逐 ~280MB 再全量重传,帧渲染 2.5~5s 永不
|
|
11
|
-
* 收敛;预算抬到覆盖工作集后同场景 3ms/帧——A/B 定谳)。孤儿/换页残留
|
|
12
|
-
* 不再被 get 命中即沉到 LRU 头,下一次插入压力先逐它们,预算仍长期生效。
|
|
13
|
-
* - **帧界保护(插入压力三防线,批次 321 补丁)**:渲染中插入路径的淘汰依次
|
|
14
|
-
* 受 ①手势期冻结(markFrame(gestureActive))②近帧宽限(evictGraceFrames
|
|
15
|
-
* 帧内 touch 过的不逐——句柄可能已被批渲染器捕获,保留式帧让活动工作集
|
|
16
|
-
* 间歇不 touch)③代价感知(先逐 <32MiB 小条目,耗尽才动大条目)拦护;
|
|
17
|
-
* 全被拦下时允许留驻超额——颠簸 = 永不可用,留驻 = 一次性上传后稳定,
|
|
18
|
-
* 是显式取舍(细节见 evictIfNeeded 头注)
|
|
19
|
-
* - 字节口径:宽×高×4(RGBA8),mipmap 源 ×4/3(三级以上链约数)
|
|
20
|
-
*
|
|
21
|
-
* Phase 4 动态源(动画 GIF 单帧缓冲 / HTMLVideoElement):
|
|
22
|
-
* - 内容随时间变化,get() 仅首传不够——update() 重新 texImage2D 当前内容;
|
|
23
|
-
* - 视频源不生成 mipmap(每帧 generateMipmap 不可承受),MIN_FILTER=LINEAR;
|
|
24
|
-
* - remove() 供动画节点移除时显式释放。LRU 淘汰动态源同样安全:条目被逐后
|
|
25
|
-
* update() miss 无操作,下一帧 get() 重传当前帧。
|
|
26
|
-
*
|
|
27
|
-
* context lost:invalidateAll() 清空(恢复事件里由渲染层重建)。
|
|
28
|
-
*/
|
|
29
|
-
import { RenderEpoch } from './renderEpoch';
|
|
30
|
-
const DEFAULT_BUDGET_BYTES = 512 * 1024 * 1024;
|
|
31
|
-
const DEFAULT_MAX_ENTRIES = 2048;
|
|
32
|
-
const DEFAULT_EVICT_GRACE_FRAMES = 30;
|
|
33
|
-
/** 代价感知门槛:≥32MiB 的条目视为大条目,插入压力先耗尽小条目才逐它
|
|
34
|
-
* (全幅位图纹理逐出后全量重传是秒级,小烘焙纹理重传无感) */
|
|
35
|
-
const BIG_ENTRY_BYTES = 32 * 1024 * 1024;
|
|
36
|
-
export class GLTextureCache {
|
|
37
|
-
gl;
|
|
38
|
-
cache = new Map();
|
|
39
|
-
budgetBytes;
|
|
40
|
-
maxEntries;
|
|
41
|
-
evictGraceFrames;
|
|
42
|
-
gestureActive = false;
|
|
43
|
-
frameStamp = 0;
|
|
44
|
-
frameTouches = 0;
|
|
45
|
-
evictions = 0;
|
|
46
|
-
trackedBytes = 0;
|
|
47
|
-
disposed = false;
|
|
48
|
-
/** 统一失效纪元(批次 374 方向一):淘汰即 bump,保留式门链经宿主共享实例消费 */
|
|
49
|
-
epoch;
|
|
50
|
-
constructor(gl, options = {}) {
|
|
51
|
-
this.gl = gl;
|
|
52
|
-
this.budgetBytes = options.budgetBytes ?? DEFAULT_BUDGET_BYTES;
|
|
53
|
-
this.maxEntries = options.maxEntries ?? DEFAULT_MAX_ENTRIES;
|
|
54
|
-
this.evictGraceFrames = options.evictGraceFrames ?? DEFAULT_EVICT_GRACE_FRAMES;
|
|
55
|
-
this.epoch = options.epoch ?? new RenderEpoch();
|
|
56
|
-
}
|
|
57
|
-
/** 帧界推进(EngineCanvas.render 顶部调用):此后的取用都算「本帧」。
|
|
58
|
-
* 批次 321:不再做预算强制结算——帧界强逐在工作集超预算时构成
|
|
59
|
-
* 「逐出→重传」死循环(同头注),淘汰统一改由插入压力驱动
|
|
60
|
-
* (get miss → evictIfNeeded);预算的长期收敛性由孤儿沉底 LRU 头保证。
|
|
61
|
-
* gestureActive(指针手势进行中):本帧插入压力淘汰整体冻结(见
|
|
62
|
-
* evictIfNeeded 头注的手势期防线) */
|
|
63
|
-
markFrame(gestureActive = false) {
|
|
64
|
-
this.frameStamp += 1;
|
|
65
|
-
this.frameTouches = 0;
|
|
66
|
-
this.gestureActive = gestureActive;
|
|
67
|
-
}
|
|
68
|
-
/** 累计逐出计数(批次 223 重落保留式回退门):逐出删除纹理句柄会使快照命令
|
|
69
|
-
* 里引用的句柄失效——计数变化即作废保留式快照走全量帧 */
|
|
70
|
-
get evictionCount() {
|
|
71
|
-
return this.evictions;
|
|
72
|
-
}
|
|
73
|
-
/** 运行时调整预算(e2e/诊断用;立即强制淘汰,帧界保护不适用——调用点在帧间) */
|
|
74
|
-
setBudget(bytes) {
|
|
75
|
-
if (!(bytes > 0))
|
|
76
|
-
return;
|
|
77
|
-
this.budgetBytes = bytes;
|
|
78
|
-
this.evictIfNeeded(0, false);
|
|
79
|
-
}
|
|
80
|
-
get(source) {
|
|
81
|
-
if (this.disposed)
|
|
82
|
-
return null;
|
|
83
|
-
const cached = this.cache.get(source);
|
|
84
|
-
if (cached) {
|
|
85
|
-
cached.lastFrame = this.frameStamp;
|
|
86
|
-
this.frameTouches += 1;
|
|
87
|
-
// LRU 前移:Map 尾部 = 最近访问
|
|
88
|
-
this.cache.delete(source);
|
|
89
|
-
this.cache.set(source, cached);
|
|
90
|
-
return cached.tex;
|
|
91
|
-
}
|
|
92
|
-
const texture = this.gl.createTexture();
|
|
93
|
-
if (!texture)
|
|
94
|
-
return null;
|
|
95
|
-
// 视频源:每帧 update 重传,generateMipmap 逐帧成本不可行——LINEAR 即可
|
|
96
|
-
// (Canvas2D drawImage(video) 同样无 mip);其余源保留三线性
|
|
97
|
-
const mipmap = !(source instanceof HTMLVideoElement);
|
|
98
|
-
this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
|
|
99
|
-
// 不做 FLIP_Y:图像数据首行(顶部)自然落在 v=0,与 quad 的 uv 约定一致
|
|
100
|
-
// (local (0,0)=左上角 ↔ uv(u0,v0)=图像左上角)。y 翻转已由视图矩阵处理,
|
|
101
|
-
// 上传再翻一次会导致图像上下镜像
|
|
102
|
-
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, source);
|
|
103
|
-
if (mipmap) {
|
|
104
|
-
// mipmap + 三线性:对齐 Canvas2D 的高质量降采样(无 mip 的 LINEAR 在缩小时
|
|
105
|
-
// 欠采样,与 fabric drawImage 的过滤差异会覆盖整个图像区域而非仅边缘)
|
|
106
|
-
this.gl.generateMipmap(this.gl.TEXTURE_2D);
|
|
107
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR_MIPMAP_LINEAR);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);
|
|
111
|
-
}
|
|
112
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
|
|
113
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE);
|
|
114
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE);
|
|
115
|
-
const bytes = estimateSourceBytes(source, mipmap);
|
|
116
|
-
const entry = { tex: texture, mipmap, bytes, lastFrame: this.frameStamp };
|
|
117
|
-
this.evictIfNeeded(bytes, true);
|
|
118
|
-
this.trackedBytes += bytes;
|
|
119
|
-
this.frameTouches += 1;
|
|
120
|
-
this.cache.set(source, entry);
|
|
121
|
-
return texture;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* 预算淘汰(仅插入路径/setBudget 调用):从最旧端逐条淘汰到 reserveBytes
|
|
125
|
-
* 可容纳。protectCurrentFrame=true(渲染中插入路径)三道防线(思阳素材
|
|
126
|
-
* 431.9MiB PSD 拖动每步 2.2s 定谳后的批次 321 补丁——保留式帧只重推脏节点,
|
|
127
|
-
* 活动大图整段不 touch,插入压力会把刚用过的大图逐出,下一全量帧 490MiB
|
|
128
|
-
* 级重传):
|
|
129
|
-
* ① 手势期冻结——markFrame(gestureActive) 期间插入压力整体 no-op(拖动/
|
|
130
|
-
* 缩放/旋转中绝不逐出;烘焙小纹理持续插入照常,超额留驻=头注取舍);
|
|
131
|
-
* ② 近帧宽限——距当前帧不足 evictGraceFrames(缺省 30)的条目跳过:非
|
|
132
|
-
* 手势期的活动工作集(动画/编辑间歇 touch)不被插入压力逐出;真孤儿
|
|
133
|
-
* (远旧帧号沉底)照逐,预算长期收敛性不变;
|
|
134
|
-
* ③ 代价感知——先逐小条目(< BIG_ENTRY_BYTES),小条目耗尽才动大条目:
|
|
135
|
-
* 逐出恢复代价 ∝ 条目字节数(小纹理重传无感,全幅大图重传秒级),偶发
|
|
136
|
-
* 结算优先牺牲无感的小条目。
|
|
137
|
-
* 全被防线拦下时允许临时超额(正确性优先)。false(setBudget 显式调压):
|
|
138
|
-
* 强制淘汰——调用点在帧间,无批次持有句柄,三道防线均不适用。
|
|
139
|
-
*/
|
|
140
|
-
evictIfNeeded(reserveBytes, protectCurrentFrame) {
|
|
141
|
-
if (this.disposed)
|
|
142
|
-
return;
|
|
143
|
-
if (protectCurrentFrame && this.gestureActive)
|
|
144
|
-
return;
|
|
145
|
-
while (this.trackedBytes + reserveBytes > this.budgetBytes || this.cache.size >= this.maxEntries) {
|
|
146
|
-
const evicted = this.evictOldestEligible(protectCurrentFrame, false) || this.evictOldestEligible(protectCurrentFrame, true);
|
|
147
|
-
// 无可逐条目(全被宽限/尺寸防线拦下,或缓存已空仍超预算——reserveBytes 本身超限)
|
|
148
|
-
if (!evicted)
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
/** 从 LRU 最旧端逐一条目;allowBig=false 跳过大条目(代价感知第一轮) */
|
|
153
|
-
evictOldestEligible(protectCurrentFrame, allowBig) {
|
|
154
|
-
for (const [source, entry] of this.cache) {
|
|
155
|
-
if (protectCurrentFrame && this.frameStamp - entry.lastFrame < this.evictGraceFrames)
|
|
156
|
-
continue;
|
|
157
|
-
if (!allowBig && entry.bytes >= BIG_ENTRY_BYTES)
|
|
158
|
-
continue;
|
|
159
|
-
this.gl.deleteTexture(entry.tex);
|
|
160
|
-
this.cache.delete(source);
|
|
161
|
-
this.trackedBytes -= entry.bytes;
|
|
162
|
-
this.evictions += 1;
|
|
163
|
-
// 统一失效纪元(批次 374):被逐句柄可能已被保留式快照命令表引用
|
|
164
|
-
this.epoch.bump('texture-evict');
|
|
165
|
-
return true;
|
|
166
|
-
}
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
/**
|
|
170
|
-
* 动态源内容更新后重传(GIF 换帧 / 视频新帧)。仅对已缓存源生效——
|
|
171
|
-
* 未创建过的源由下一次 get() 全量上传;纹理对象不变,调用方无需重取。
|
|
172
|
-
*/
|
|
173
|
-
update(source) {
|
|
174
|
-
if (this.disposed)
|
|
175
|
-
return;
|
|
176
|
-
const entry = this.cache.get(source);
|
|
177
|
-
if (!entry)
|
|
178
|
-
return;
|
|
179
|
-
this.gl.bindTexture(this.gl.TEXTURE_2D, entry.tex);
|
|
180
|
-
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, source);
|
|
181
|
-
if (entry.mipmap)
|
|
182
|
-
this.gl.generateMipmap(this.gl.TEXTURE_2D);
|
|
183
|
-
}
|
|
184
|
-
/** 显式释放单条(动画节点移除/场景重建时释放视频与帧缓冲纹理) */
|
|
185
|
-
remove(source) {
|
|
186
|
-
const entry = this.cache.get(source);
|
|
187
|
-
if (!entry)
|
|
188
|
-
return;
|
|
189
|
-
this.gl.deleteTexture(entry.tex);
|
|
190
|
-
this.cache.delete(source);
|
|
191
|
-
this.trackedBytes -= entry.bytes;
|
|
192
|
-
}
|
|
193
|
-
/** context lost 后调用:清空缓存,恢复事件里由渲染层重建 */
|
|
194
|
-
invalidateAll() {
|
|
195
|
-
for (const entry of this.cache.values()) {
|
|
196
|
-
this.gl.deleteTexture(entry.tex);
|
|
197
|
-
}
|
|
198
|
-
this.cache.clear();
|
|
199
|
-
this.trackedBytes = 0;
|
|
200
|
-
}
|
|
201
|
-
stats() {
|
|
202
|
-
return {
|
|
203
|
-
entries: this.cache.size,
|
|
204
|
-
bytes: this.trackedBytes,
|
|
205
|
-
budgetBytes: this.budgetBytes,
|
|
206
|
-
maxEntries: this.maxEntries,
|
|
207
|
-
evictions: this.evictions,
|
|
208
|
-
frameTouches: this.frameTouches
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
dispose() {
|
|
212
|
-
if (this.disposed)
|
|
213
|
-
return;
|
|
214
|
-
this.disposed = true;
|
|
215
|
-
this.invalidateAll();
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
/** 估算源纹理显存占用:宽×高×4,mipmap 链 ×4/3(≈ Σ4⁻ⁿ 上界) */
|
|
219
|
-
export const estimateSourceBytes = (source, mipmap) => {
|
|
220
|
-
const w = source instanceof HTMLVideoElement ? source.videoWidth : source instanceof HTMLImageElement ? source.naturalWidth || source.width : source.width;
|
|
221
|
-
const h = source instanceof HTMLVideoElement ? source.videoHeight : source instanceof HTMLImageElement ? source.naturalHeight || source.height : source.height;
|
|
222
|
-
const base = Math.max(1, w) * Math.max(1, h) * 4;
|
|
223
|
-
return Math.round(mipmap ? base * (4 / 3) : base);
|
|
224
|
-
};
|
|
@@ -1,412 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 字形图集(main-webgl2 Phase 2a)。
|
|
3
|
-
*
|
|
4
|
-
* 懒栅格化:按 (字体声明, 栅格化字号, grapheme, 描边声明) 把字形用 2D
|
|
5
|
-
* fillText/strokeText 画进 1024² 图集页(shelf 行打包),texSubImage2D 增量
|
|
6
|
-
* 上传。渲染时每个字形一个 quad,采样同一张图集纹理——整段文本通常一个 draw call。
|
|
7
|
-
*
|
|
8
|
-
* 描边层(P2-3):同一字形可按描边声明再栅格化一层(strokeText,只画轮廓环、
|
|
9
|
-
* 不填内部),与填充层共享 quad 管线;cell 按 miterLimit×线宽留白以容纳尖角外扩
|
|
10
|
-
* (fabric 缺省 miterLimit=4 → 最多 2×线宽),留白计入 padPx 后渲染定位公式不变。
|
|
11
|
-
* 描边色不进 key(栅格化恒为白,颜色走 quad 调制)。
|
|
12
|
-
*
|
|
13
|
-
* 栅格化字号 = fontSize × 缩放桶 × dpr(桶取 [0.5,1,2,4] 中 ≥ 有效缩放的最小
|
|
14
|
-
* 值):桶高于当前缩放时相当于超采样,缩放/平移全程字形清晰,fabric 的
|
|
15
|
-
* 对象缓存重栅格化(_suspendCacheForZoom 那类补偿)在此无必要性。
|
|
16
|
-
*
|
|
17
|
-
* 图集页不生成 mipmap(cell 间会串色),MIN_FILTER 用 LINEAR——桶切换瞬间的
|
|
18
|
-
* 轻微欠采样可接受,超采样桶已覆盖绝大多数场景。Phase 2b 如需出图质量,
|
|
19
|
-
* 再做 pad+ankle mipmap 或 SDF。
|
|
20
|
-
*/
|
|
21
|
-
import { fontDeclaration, GENERIC_FONTS } from '../model/textLayout';
|
|
22
|
-
import { isRetainedDebugEnabled } from '../engineFlag';
|
|
23
|
-
import { normalizeFamily } from './fontInvalidation';
|
|
24
|
-
import { RenderEpoch } from './renderEpoch';
|
|
25
|
-
const PAGE_SIZE = 1024;
|
|
26
|
-
const CELL_PAD = 2;
|
|
27
|
-
/** miter 留白上限(px):防极端 miterLimit 让 cell 撑爆整页而让字形整体消失 */
|
|
28
|
-
const MITER_PAD_CAP = 256;
|
|
29
|
-
/** 缩放桶:有效缩放(zoom×dpr)向上取到桶值,字形按桶字号栅格化 */
|
|
30
|
-
export const pickZoomBucket = (effectiveScale) => {
|
|
31
|
-
const buckets = [0.5, 1, 2, 4];
|
|
32
|
-
for (const b of buckets) {
|
|
33
|
-
if (effectiveScale <= b + 1e-6)
|
|
34
|
-
return b;
|
|
35
|
-
}
|
|
36
|
-
return buckets[buckets.length - 1];
|
|
37
|
-
};
|
|
38
|
-
/**
|
|
39
|
-
* 字形 cell 光栅尺寸封顶(px,2026-10-09 模板 273 图集风暴):该模板 ~208 字
|
|
40
|
-
* 库存 @桶 4(40px→cell≈176px)需 ~9 页 > 旧 pageCap 8 → 「填满→reset→全量
|
|
41
|
-
* 重栅格化」每帧循环(缩放期每帧 ~85ms、每帧 ~44 张 1024² 页纹理分配/释放;
|
|
42
|
-
* zoom>2 后任何渲染帧都撞)。修复主承重=pageCap 8→12(容量 ≈300 字);
|
|
43
|
-
* 本封顶再兜超大字号类(fontSize×bucket > 256 时砍超采样余量、**永不低于
|
|
44
|
-
* 字号本身**——1× 保真不降):256 恰保 parity 用例的最大档(56px×桶4=224)
|
|
45
|
-
* 全分辨率,65px+ 大字在高倍下 2-4× 超采样(与「桶切换欠采样可接受」同权衡)。
|
|
46
|
-
*/
|
|
47
|
-
export const GLYPH_RASTER_MAX_PX = 256;
|
|
48
|
-
/** cell 栅格化字号与渲染缩放:未封顶时保持既有口径(rs=ceil(fontSize×bucket)、
|
|
49
|
-
* scale=bucket——measure 按栅格像素、摆放按桶缩放的既有微差不动),封顶时
|
|
50
|
-
* rs=max(256, fontSize)、scale=rs/fontSize(cell 度量/摆放同源,视觉尺寸不变) */
|
|
51
|
-
export const glyphRasterPlan = (fontSize, bucket) => {
|
|
52
|
-
const uncapped = Math.max(6, Math.ceil(fontSize * bucket));
|
|
53
|
-
const ceiling = Math.max(GLYPH_RASTER_MAX_PX, Math.ceil(fontSize));
|
|
54
|
-
if (uncapped <= ceiling)
|
|
55
|
-
return { rs: uncapped, scale: bucket };
|
|
56
|
-
return { rs: ceiling, scale: ceiling / Math.max(1, fontSize) };
|
|
57
|
-
};
|
|
58
|
-
export class GlyphAtlas {
|
|
59
|
-
gl;
|
|
60
|
-
pages = [];
|
|
61
|
-
cells = new Map();
|
|
62
|
-
tmpCanvas;
|
|
63
|
-
disposed = false;
|
|
64
|
-
/** 图集页上限(P6-2 内存预算,2026-10-09 8→12):触顶登记 pendingReset,帧界整体回收重启。
|
|
65
|
-
* cell 消费方(TextRenderer)逐帧 atlas.get 重取,重置后下一帧自愈重栅格化
|
|
66
|
-
* ——批次 223 重落注:该自愈前提是每帧全量重推;保留式帧不重推,页纹理
|
|
67
|
-
* 全删必须经 generation 计数进保留式门链(快照命令表持有页纹理活引用,
|
|
68
|
-
* 重放死句柄 = 文本整层不绘制,模板 273 回归根因)。
|
|
69
|
-
* 12 页 = 48MB 预算;典型 CJK 40px@桶4 cell≈176px → 25 cell/页 → 容量
|
|
70
|
-
* ≈300 字(8 页时代 ~200 字:模板 273 的 208 字库存触顶即「填满→reset→
|
|
71
|
-
* 全量重栅格化」每帧风暴,实测每帧 44 张页纹理分配、~85ms/帧)。更大
|
|
72
|
-
* 库存触顶仍会风暴(存量缺口,细胞级 LRU/逐桶驱逐另册);超大字号由
|
|
73
|
-
* GLYPH_RASTER_MAX_PX 封顶 cell 上限缓解页占用 */
|
|
74
|
-
pageCap = 12;
|
|
75
|
-
pendingReset = false;
|
|
76
|
-
/** 纹理世代(批次 223 重落):reset() 自增——纹理级失效天然是全量帧信号,
|
|
77
|
-
* 保留式门链比对不等即回退全量帧(比逐节点染脏更符合语义:页是全局共享) */
|
|
78
|
-
_generation = 0;
|
|
79
|
-
/** 批次 42/44 字体迟到自愈:face 未就绪(check 假或 family 未注册)时烘出的
|
|
80
|
-
* fallback 形态 cell 登记表(key → 重探声明所需的最小样式)。清除口有三:
|
|
81
|
-
* ①loadingdone 事件整体丢弃;②帧界复查发现不再 pending 就地丢弃(事件失联
|
|
82
|
-
* 兜底——长驻 HMR 页 loadingdone 链实测会失联,批次 44 刷新竞态同症状);
|
|
83
|
-
* ③reset()/dispose() 连带清。cell 键只含字体声明串不含 face 加载态,没有此
|
|
84
|
-
* 登记则 fallback 形态永久留脏(宿主失效链失联时无任何恢复路径) */
|
|
85
|
-
provisionalCells = new Map();
|
|
86
|
-
/** provisional cell 丢弃后的重绘通知(EngineCanvas 接 requestRenderAll——
|
|
87
|
-
* 图集自身不持渲染循环,不通知则空转等下一次外部触发) */
|
|
88
|
-
onCellsInvalidated;
|
|
89
|
-
/** 批次 46:帧界 settle(事件失联自愈路径)丢弃 provisional cell 时,把
|
|
90
|
-
* 涉及的归一化 family 集上报宿主——宿主借此补齐同一失效链的其余两环
|
|
91
|
-
* (排版/度量缓存 + 文本派生烘焙),否则字形自愈而特效层/排版停留
|
|
92
|
-
* fallback 形态(用户报障「垂直文本的特效层字体没有更新」根因)。
|
|
93
|
-
* 仅 settle 路径上报:loadingdone 事件路径宿主自有 handleFontsLoaded
|
|
94
|
-
* 全量失效,重复上报只会带来重复重排 */
|
|
95
|
-
onProvisionalSettled;
|
|
96
|
-
detachFontsListener = null;
|
|
97
|
-
/** 统一失效纪元(批次 374 方向一):reset 删页纹理即 bump;EngineCanvas 注入
|
|
98
|
-
* 共享实例,缺省自建(直构单测=独立纪元) */
|
|
99
|
-
epoch;
|
|
100
|
-
constructor(gl, onCellsInvalidated, onProvisionalSettled, epoch = new RenderEpoch()) {
|
|
101
|
-
this.gl = gl;
|
|
102
|
-
this.onCellsInvalidated = onCellsInvalidated;
|
|
103
|
-
this.onProvisionalSettled = onProvisionalSettled;
|
|
104
|
-
this.epoch = epoch;
|
|
105
|
-
this.tmpCanvas = document.createElement('canvas');
|
|
106
|
-
this.tmpCanvas.width = 8;
|
|
107
|
-
this.tmpCanvas.height = 8;
|
|
108
|
-
// 自听 loadingdone(批次 42):不做 family 匹配——provisional 集合本来就只含
|
|
109
|
-
// 「等字体」的 cell,整体丢弃重烘的成本可忽略(over-invalidate 安全)。
|
|
110
|
-
// jsdom/非浏览器宿主 fonts 集或 addEventListener 缺席时静默跳过
|
|
111
|
-
const fonts = this.fontSet();
|
|
112
|
-
if (fonts && typeof fonts.addEventListener === 'function') {
|
|
113
|
-
const onLoaded = () => this.dropProvisionalCells();
|
|
114
|
-
fonts.addEventListener('loadingdone', onLoaded);
|
|
115
|
-
this.detachFontsListener = () => fonts.removeEventListener('loadingdone', onLoaded);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
/** document.fonts 的防御式取位(jsdom 旧版/非浏览器宿主可能缺) */
|
|
119
|
-
fontSet() {
|
|
120
|
-
if (typeof document === 'undefined')
|
|
121
|
-
return null;
|
|
122
|
-
return document.fonts ?? null;
|
|
123
|
-
}
|
|
124
|
-
/** 声明的 face 是否尚未就绪。两态都算 pending:
|
|
125
|
-
* ①check 假(face 已注册未加载——canvas2d 的 font 赋值不触发加载);
|
|
126
|
-
* ②family 根本未注册(批次 44)——平台语义:check 对无匹配 face 的声明恒真
|
|
127
|
-
* (视为系统字体可用),刷新热缓存竞态下面板/图集可能在 face 注册前烘出
|
|
128
|
-
* fallback cell,且此形态无人清洗。把未注册也视为 pending,face 注册加载
|
|
129
|
-
* 完成即丢弃重烘,自愈不依赖宿主接线与节点时序。泛型族(sans-serif 等)无
|
|
130
|
-
* face 属常态不探测;含逗号的回退列表声明整体照旧走 check,不逐段探测。 */
|
|
131
|
-
isFacePending(decl, style) {
|
|
132
|
-
const fonts = this.fontSet();
|
|
133
|
-
if (!fonts || typeof fonts.check !== 'function')
|
|
134
|
-
return false;
|
|
135
|
-
try {
|
|
136
|
-
if (!fonts.check(decl))
|
|
137
|
-
return true;
|
|
138
|
-
const family = style.fontFamily?.trim().replace(/^['"]|['"]$/g, '') ?? '';
|
|
139
|
-
if (!family || family.includes(',') || GENERIC_FONTS.has(family.toLowerCase()))
|
|
140
|
-
return false;
|
|
141
|
-
if (typeof fonts.forEach !== 'function')
|
|
142
|
-
return false;
|
|
143
|
-
let registered = false;
|
|
144
|
-
fonts.forEach((face) => {
|
|
145
|
-
if (registered || typeof face?.family !== 'string')
|
|
146
|
-
return;
|
|
147
|
-
if (face.family === family || face.family.toLowerCase() === family.toLowerCase())
|
|
148
|
-
registered = true;
|
|
149
|
-
});
|
|
150
|
-
return !registered;
|
|
151
|
-
}
|
|
152
|
-
catch {
|
|
153
|
-
return false;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
/** canvas2d 的 font 赋值不会触发字体加载——face 未就绪时显式踢一脚,
|
|
157
|
-
* 完成时 loadingdone 触发 provisional cell 丢弃重烘(注册了但无人触发的
|
|
158
|
-
* face 由此走出「永是 fallback」状态) */
|
|
159
|
-
kickFaceLoad(decl) {
|
|
160
|
-
const fonts = this.fontSet();
|
|
161
|
-
if (!fonts || typeof fonts.load !== 'function')
|
|
162
|
-
return;
|
|
163
|
-
try {
|
|
164
|
-
fonts.load(decl).catch(() => { });
|
|
165
|
-
}
|
|
166
|
-
catch {
|
|
167
|
-
// load 同步抛(非法声明等)忽略——fallback cell 已在位
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
/** loadingdone 收口:丢弃全部 provisional cell 并通知重绘(下帧重烘真字形) */
|
|
171
|
-
dropProvisionalCells() {
|
|
172
|
-
if (this.disposed || this.provisionalCells.size === 0)
|
|
173
|
-
return;
|
|
174
|
-
for (const key of this.provisionalCells.keys())
|
|
175
|
-
this.cells.delete(key);
|
|
176
|
-
this.provisionalCells.clear();
|
|
177
|
-
this.onCellsInvalidated?.();
|
|
178
|
-
}
|
|
179
|
-
/** 帧界推进(EngineCanvas.render 顶部调用):pendingReset 在此消费——
|
|
180
|
-
* 重置删页纹理必须发生在任何绘制之前(本帧批次可能已捕获页纹理句柄)。
|
|
181
|
-
* 批次 44:附带复查 provisional cell 的 pending 态——不再 pending(face 注册
|
|
182
|
-
* 加载完成)的就地丢弃,本帧渲染即按声明重烘真字形。事件失联(长驻 HMR 页
|
|
183
|
-
* loadingdone 链实测会断)时这是唯一的自愈驱动;集空零成本。 */
|
|
184
|
-
markFrame() {
|
|
185
|
-
if (this.pendingReset) {
|
|
186
|
-
this.pendingReset = false;
|
|
187
|
-
this.reset();
|
|
188
|
-
}
|
|
189
|
-
this.settleProvisionalCells();
|
|
190
|
-
}
|
|
191
|
-
/** provisional 复查:pending 态消失的 cell 丢弃 + 补帧通知(仍 pending 的保留)。
|
|
192
|
-
* 批次 46:settled 的归一化 family 集一并上报宿主(排版/烘焙失效链补齐) */
|
|
193
|
-
settleProvisionalCells() {
|
|
194
|
-
if (this.disposed || this.provisionalCells.size === 0)
|
|
195
|
-
return;
|
|
196
|
-
let settled = false;
|
|
197
|
-
const families = new Set();
|
|
198
|
-
for (const [key, info] of this.provisionalCells) {
|
|
199
|
-
const decl = fontDeclaration(info.style, info.rasterSize);
|
|
200
|
-
if (!this.isFacePending(decl, info.style)) {
|
|
201
|
-
this.cells.delete(key);
|
|
202
|
-
this.provisionalCells.delete(key);
|
|
203
|
-
settled = true;
|
|
204
|
-
const family = normalizeFamily(info.style.fontFamily ?? '');
|
|
205
|
-
if (family)
|
|
206
|
-
families.add(family);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
if (settled) {
|
|
210
|
-
this.onCellsInvalidated?.();
|
|
211
|
-
if (families.size > 0)
|
|
212
|
-
this.onProvisionalSettled?.([...families]);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
/** 整体回收:删页纹理 + 清 cell 表(字形按需重栅格化,逐帧 get 自愈)。
|
|
216
|
-
* context lost 恢复链同用(页纹理已死,重置后首帧重建)。
|
|
217
|
-
* 批次 223 重落:世代自增——保留式快照命令表持有页纹理活引用,重放死
|
|
218
|
-
* 句柄 = 文本整层不绘制(模板 273 回归根因),门链比对世代即回退全量。
|
|
219
|
-
* 不发补帧通知:reset 的三个调用点(markFrame 帧界——本帧已在跑且门链
|
|
220
|
-
* 紧随其后即回退全量;handleFontsLoaded——自带同步 render;
|
|
221
|
-
* recoverGlResources——恢复链必跟补帧)均有帧驱动,静默是既有语义 */
|
|
222
|
-
reset() {
|
|
223
|
-
const hadContent = this.pages.length > 0;
|
|
224
|
-
if (!this.disposed && hadContent) {
|
|
225
|
-
for (const page of this.pages)
|
|
226
|
-
this.gl.deleteTexture(page.texture);
|
|
227
|
-
}
|
|
228
|
-
this.pages.length = 0;
|
|
229
|
-
this.cells.clear();
|
|
230
|
-
this.provisionalCells.clear();
|
|
231
|
-
if (hadContent) {
|
|
232
|
-
this._generation++;
|
|
233
|
-
// 统一失效纪元(批次 374):与 generation 同守卫块——reset 删全部页纹理,
|
|
234
|
-
// 快照命令表持有的活引用变死句柄,纪元 bump 进保留式门链
|
|
235
|
-
this.epoch.bump('atlas-reset');
|
|
236
|
-
if (isRetainedDebugEnabled()) {
|
|
237
|
-
console.warn('[retained] GlyphAtlas.reset → generation', this._generation, new Error('reset 调用栈取证').stack);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
/** 纹理世代(批次 223 重落保留式门):reset() 自增,快照收录时记录比对 */
|
|
242
|
-
get generation() {
|
|
243
|
-
return this._generation;
|
|
244
|
-
}
|
|
245
|
-
stats() {
|
|
246
|
-
return { pages: this.pages.length, cells: this.cells.size, pageCap: this.pageCap, pendingReset: this.pendingReset, generation: this._generation };
|
|
247
|
-
}
|
|
248
|
-
/** 字形栅格化键外的独立度量(按需调用,命中缓存零成本)。
|
|
249
|
-
* stroke 非空 = 取该描边声明下的「描边层」cell(白色轮廓环,只画不填)。 */
|
|
250
|
-
get(style, rasterSize, grapheme, stroke = null) {
|
|
251
|
-
if (this.disposed)
|
|
252
|
-
return null;
|
|
253
|
-
if (grapheme === ' ' || grapheme === '' || grapheme === '\n' || grapheme === '\t')
|
|
254
|
-
return null;
|
|
255
|
-
let key = `${style.fontFamily}|${style.fontWeight ?? ''}|${style.fontStyle ?? ''}|${rasterSize}|${grapheme}`;
|
|
256
|
-
// 描边的栅格化参数(线宽/连接/端点)决定像素结果,必须进 key;颜色不进(走 quad 调制)
|
|
257
|
-
if (stroke)
|
|
258
|
-
key += `|S${stroke.width}|${stroke.lineJoin ?? ''}|${stroke.lineCap ?? ''}|${stroke.miterLimit ?? ''}`;
|
|
259
|
-
const cached = this.cells.get(key);
|
|
260
|
-
if (cached)
|
|
261
|
-
return cached;
|
|
262
|
-
// 批次 42/44:烘焙前探 face 就绪态(check 假或 family 未注册)——pending 的
|
|
263
|
-
// cell 登记 provisional(loadingdone 或帧界复查发现就绪即丢弃重烘),并主动
|
|
264
|
-
// 触发 face 加载。已就绪(含纯回退无注册 face 的泛型族)不受影响
|
|
265
|
-
const decl = fontDeclaration(style, rasterSize);
|
|
266
|
-
const facePending = this.isFacePending(decl, style);
|
|
267
|
-
if (facePending)
|
|
268
|
-
this.kickFaceLoad(decl);
|
|
269
|
-
const cell = this.rasterize(style, rasterSize, grapheme, stroke);
|
|
270
|
-
if (cell) {
|
|
271
|
-
this.cells.set(key, cell);
|
|
272
|
-
if (facePending) {
|
|
273
|
-
// 登记最小样式快照(fontDeclaration 只读字体四件套)——帧界复查重建声明用;
|
|
274
|
-
// 不存调用方的 style 引用(节点 textStyle 可能被复用/换新)
|
|
275
|
-
this.provisionalCells.set(key, {
|
|
276
|
-
style: { fontFamily: style.fontFamily, fontSize: style.fontSize, fontWeight: style.fontWeight, fontStyle: style.fontStyle },
|
|
277
|
-
rasterSize
|
|
278
|
-
});
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return cell;
|
|
282
|
-
}
|
|
283
|
-
rasterize(style, rasterSize, grapheme, stroke) {
|
|
284
|
-
let page = this.pages[this.pages.length - 1];
|
|
285
|
-
if (!page)
|
|
286
|
-
page = this.newPage();
|
|
287
|
-
let pageIndex = this.pages.length - 1;
|
|
288
|
-
const ctx = page.ctx;
|
|
289
|
-
ctx.font = fontDeclaration(style, rasterSize);
|
|
290
|
-
ctx.textBaseline = 'alphabetic';
|
|
291
|
-
ctx.textAlign = 'left';
|
|
292
|
-
ctx.fillStyle = '#ffffff';
|
|
293
|
-
const m = ctx.measureText(grapheme);
|
|
294
|
-
if (!isFinite(m.width) || (m.width <= 0 && grapheme.trim() === ''))
|
|
295
|
-
return null;
|
|
296
|
-
const bboxLeft = m.actualBoundingBoxLeft ?? 0;
|
|
297
|
-
const bboxRight = m.actualBoundingBoxRight ?? m.width;
|
|
298
|
-
// 浏览器缺实际边界度量(旧实现/怪字体)时的保守回退
|
|
299
|
-
const ascent = m.actualBoundingBoxAscent ?? rasterSize * 0.88;
|
|
300
|
-
const descent = m.actualBoundingBoxDescent ?? rasterSize * 0.24;
|
|
301
|
-
// 栅格化空间 / 文本空间 的比例(rasterSize 可能被调用方抬到最小字号)
|
|
302
|
-
const rasterScale = rasterSize / Math.max(1, style.fontSize);
|
|
303
|
-
// fabric 的 strokeWidth 是文本空间量:栅格化时要按同一比例放大
|
|
304
|
-
const strokeWidthPx = stroke ? Math.max(0.5, stroke.width * rasterScale) : 0;
|
|
305
|
-
// 描边沿轮廓居中展开 → 最多外扩 miterLimit×线宽/2(缺省 4 → 2×线宽)+ 1px AA 余量
|
|
306
|
-
const miterAllow = stroke ? Math.min((strokeWidthPx * Math.max(1, stroke.miterLimit ?? 4)) / 2, MITER_PAD_CAP) : 0;
|
|
307
|
-
const pad = CELL_PAD + (stroke ? Math.ceil(miterAllow) + 1 : 0);
|
|
308
|
-
const wPx = Math.max(1, Math.ceil(bboxLeft + bboxRight) + pad * 2);
|
|
309
|
-
const hPx = Math.max(1, Math.ceil(ascent + descent) + pad * 2);
|
|
310
|
-
// 极端字号×线宽下 cell 会超过整页:宁可不画(调用方拿到 null 即跳过)也不写坏图集页
|
|
311
|
-
if (wPx > PAGE_SIZE || hPx > PAGE_SIZE)
|
|
312
|
-
return null;
|
|
313
|
-
// shelf 打包:行满换行,页满开新页
|
|
314
|
-
if (page.cursorX + wPx > PAGE_SIZE) {
|
|
315
|
-
page.cursorX = 0;
|
|
316
|
-
page.cursorY += page.rowHeight;
|
|
317
|
-
page.rowHeight = 0;
|
|
318
|
-
}
|
|
319
|
-
if (page.cursorY + hPx > PAGE_SIZE) {
|
|
320
|
-
page = this.newPage();
|
|
321
|
-
pageIndex = this.pages.length - 1;
|
|
322
|
-
}
|
|
323
|
-
// 页满换页后必须重取最终页的 ctx 再画:ctx 在打包前绑定的是旧页,
|
|
324
|
-
// 字形会画进旧页画布而新页纹理区域保持全空——该字形在渲染中整体消失
|
|
325
|
-
// (缩放跨桶重栅格化时按当页布局命中,表现为随机某字缺填充或缺描边)
|
|
326
|
-
const targetCtx = page.ctx;
|
|
327
|
-
targetCtx.font = fontDeclaration(style, rasterSize);
|
|
328
|
-
targetCtx.textBaseline = 'alphabetic';
|
|
329
|
-
targetCtx.textAlign = 'left';
|
|
330
|
-
targetCtx.fillStyle = '#ffffff';
|
|
331
|
-
const tlX = page.cursorX;
|
|
332
|
-
const tlY = page.cursorY;
|
|
333
|
-
// 笔画原点 + 留白后的墨迹落点:描边层的墨迹会在字形轮廓外扩,故 pad 更大
|
|
334
|
-
const penX = tlX + pad + bboxLeft;
|
|
335
|
-
const penY = tlY + pad + ascent;
|
|
336
|
-
// save/restore 围住:图集页 ctx 跨 cell 复用,描边的线宽/连接必须还原
|
|
337
|
-
targetCtx.save();
|
|
338
|
-
if (stroke) {
|
|
339
|
-
targetCtx.lineWidth = strokeWidthPx;
|
|
340
|
-
targetCtx.lineJoin = (stroke.lineJoin ?? 'miter');
|
|
341
|
-
targetCtx.lineCap = (stroke.lineCap ?? 'butt');
|
|
342
|
-
targetCtx.miterLimit = Math.max(1, stroke.miterLimit ?? 4);
|
|
343
|
-
targetCtx.strokeStyle = '#ffffff';
|
|
344
|
-
targetCtx.strokeText(grapheme, penX, penY);
|
|
345
|
-
}
|
|
346
|
-
else {
|
|
347
|
-
targetCtx.fillText(grapheme, penX, penY);
|
|
348
|
-
}
|
|
349
|
-
targetCtx.restore();
|
|
350
|
-
// 增量上传:cell 区域经临时 canvas texSubImage2D(整 canvas 上传会覆盖整页)
|
|
351
|
-
if (this.tmpCanvas.width < wPx || this.tmpCanvas.height < hPx) {
|
|
352
|
-
this.tmpCanvas.width = Math.max(this.tmpCanvas.width, wPx);
|
|
353
|
-
this.tmpCanvas.height = Math.max(this.tmpCanvas.height, hPx);
|
|
354
|
-
}
|
|
355
|
-
const tctx = this.tmpCanvas.getContext('2d');
|
|
356
|
-
tctx.clearRect(0, 0, this.tmpCanvas.width, this.tmpCanvas.height);
|
|
357
|
-
tctx.drawImage(page.canvas, tlX, tlY, wPx, hPx, 0, 0, wPx, hPx);
|
|
358
|
-
this.gl.bindTexture(this.gl.TEXTURE_2D, page.texture);
|
|
359
|
-
this.gl.texSubImage2D(this.gl.TEXTURE_2D, 0, tlX, tlY, wPx, hPx, this.gl.RGBA, this.gl.UNSIGNED_BYTE, this.tmpCanvas);
|
|
360
|
-
page.cursorX += wPx;
|
|
361
|
-
page.rowHeight = Math.max(page.rowHeight, hPx);
|
|
362
|
-
return {
|
|
363
|
-
pageIndex,
|
|
364
|
-
u0: tlX / PAGE_SIZE,
|
|
365
|
-
v0: tlY / PAGE_SIZE,
|
|
366
|
-
u1: (tlX + wPx) / PAGE_SIZE,
|
|
367
|
-
v1: (tlY + hPx) / PAGE_SIZE,
|
|
368
|
-
wPx,
|
|
369
|
-
hPx,
|
|
370
|
-
inkLeftPx: -bboxLeft,
|
|
371
|
-
ascentPx: ascent,
|
|
372
|
-
padPx: pad,
|
|
373
|
-
advancePx: m.width
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
/** cell → 归属页纹理(渲染循环按页分组提交) */
|
|
377
|
-
textureOf(cell) {
|
|
378
|
-
return this.pages[cell.pageIndex]?.texture ?? null;
|
|
379
|
-
}
|
|
380
|
-
newPage() {
|
|
381
|
-
// 页上限触顶:登记整体回收(下个帧界消费——此刻本帧批次可能已捕获既有页
|
|
382
|
-
// 纹理,且当前字形仍需一页落位),本帧临时超限一页
|
|
383
|
-
if (this.pages.length >= this.pageCap)
|
|
384
|
-
this.pendingReset = true;
|
|
385
|
-
const canvas = document.createElement('canvas');
|
|
386
|
-
canvas.width = PAGE_SIZE;
|
|
387
|
-
canvas.height = PAGE_SIZE;
|
|
388
|
-
const ctx = canvas.getContext('2d');
|
|
389
|
-
const texture = this.gl.createTexture();
|
|
390
|
-
this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
|
|
391
|
-
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, PAGE_SIZE, PAGE_SIZE, 0, this.gl.RGBA, this.gl.UNSIGNED_BYTE, null);
|
|
392
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);
|
|
393
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
|
|
394
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE);
|
|
395
|
-
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE);
|
|
396
|
-
const page = { canvas, ctx, texture, cursorX: 0, cursorY: 0, rowHeight: 0 };
|
|
397
|
-
this.pages.push(page);
|
|
398
|
-
return page;
|
|
399
|
-
}
|
|
400
|
-
dispose() {
|
|
401
|
-
if (this.disposed)
|
|
402
|
-
return;
|
|
403
|
-
this.disposed = true;
|
|
404
|
-
this.detachFontsListener?.();
|
|
405
|
-
this.detachFontsListener = null;
|
|
406
|
-
for (const page of this.pages)
|
|
407
|
-
this.gl.deleteTexture(page.texture);
|
|
408
|
-
this.pages.length = 0;
|
|
409
|
-
this.cells.clear();
|
|
410
|
-
this.provisionalCells.clear();
|
|
411
|
-
}
|
|
412
|
-
}
|