Files
ofdgo/ofdgo_renderer_state_canvas.go
T
2026-10-14 10:40:08 +08:00

205 lines
7.2 KiB
Go

// Copyright 2025-2026 肖其顿 (XIAO QI DUN)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ofdgo
import (
"crypto/sha256"
"encoding/binary"
"image"
"math"
"github.com/tdewolff/canvas"
basefont "github.com/xiaoqidun/ofdgo/internal/base/font"
)
// canvasFontKey 按字体内容、名称及样式区分可复用的字体族,不依赖文档资源编号
type canvasFontKey struct {
digest [32]byte
name string
style canvas.FontStyle
}
// canvasClipCacheKey 区分不依赖文档资源的共享裁剪缓存
type canvasClipCacheKey struct{}
// canvasPathCacheKey 区分不依赖文档资源的共享局部路径缓存
type canvasPathCacheKey struct{}
// canvasFillCacheKey 区分按填充规则规范化的共享路径缓存
type canvasFillCacheKey struct{}
// canvasOutlineCacheKey 区分子渲染器共享的字体轮廓解析缓存
type canvasOutlineCacheKey struct{}
// canvasFillKey 保存完整几何摘要及填充规则
type canvasFillKey struct {
digest [32]byte
rule canvas.FillRule
}
// canvasPathKey 按完整路径文本与几何容差区分解析结果
type canvasPathKey struct {
data string
epsilon float64
}
// canvasBackendState 保存Canvas字体、字形和编码图片适配缓存
type canvasBackendState struct {
images renderCache[*EncodedImage, image.Image]
fontMap map[string]*canvas.FontFamily
svgFontCache map[*canvas.Font]SVGFont
textGlyphPathCache renderCache[textGlyphPathCacheKey, textGlyphPathCacheValue]
fontOutlines map[*canvas.Font]*basefont.Outliner
}
// canvasClipIntersections 获取子渲染器共享的几何交集缓存,限制结果保留量
// 返回: *renderCache[[32]byte, *canvas.Path] 裁剪缓存
func (r *Renderer) canvasClipIntersections() *renderCache[[32]byte, *canvas.Path] {
key := canvasClipCacheKey{}
if state, ok := r.sharedBackendStates[key]; ok {
return state.(*renderCache[[32]byte, *canvas.Path])
}
if r.sharedBackendStates == nil {
r.sharedBackendStates = make(map[any]any)
}
cache := &renderCache[[32]byte, *canvas.Path]{limit: 8 << 20}
r.sharedBackendStates[key] = cache
return cache
}
// canvasParsedPaths 获取子渲染器共享的局部路径缓存,不保留对象变换
// 返回: *renderCache[canvasPathKey, *canvas.Path] 路径缓存
func (r *Renderer) canvasParsedPaths() *renderCache[canvasPathKey, *canvas.Path] {
key := canvasPathCacheKey{}
if state, ok := r.sharedBackendStates[key]; ok {
return state.(*renderCache[canvasPathKey, *canvas.Path])
}
if r.sharedBackendStates == nil {
r.sharedBackendStates = make(map[any]any)
}
cache := &renderCache[canvasPathKey, *canvas.Path]{limit: 16 << 20}
r.sharedBackendStates[key] = cache
return cache
}
// canvasSettledPaths 获取共享填充轮廓缓存,限制结果保留量
// 返回: *renderCache[canvasFillKey, *canvas.Path] 填充轮廓缓存
func (r *Renderer) canvasSettledPaths() *renderCache[canvasFillKey, *canvas.Path] {
key := canvasFillCacheKey{}
if state, ok := r.sharedBackendStates[key]; ok {
return state.(*renderCache[canvasFillKey, *canvas.Path])
}
if r.sharedBackendStates == nil {
r.sharedBackendStates = make(map[any]any)
}
cache := &renderCache[canvasFillKey, *canvas.Path]{limit: 16 << 20}
r.sharedBackendStates[key] = cache
return cache
}
// canvasFontFamilies 获取子渲染器共享的只读字体族缓存,限制解析数据保留量
// 返回: *renderCache[canvasFontKey, *canvas.FontFamily] 字体族缓存
func (r *Renderer) canvasFontFamilies() *renderCache[canvasFontKey, *canvas.FontFamily] {
key := CanvasBackend{}
if state, ok := r.sharedBackendStates[key]; ok {
return state.(*renderCache[canvasFontKey, *canvas.FontFamily])
}
cache := &renderCache[canvasFontKey, *canvas.FontFamily]{limit: 32 << 20}
r.sharedBackendStates[key] = cache
return cache
}
// canvasFontOutlines 获取子渲染器共享的字体轮廓解析缓存,限制字体及解析数据保留量
// 返回: *renderCache[*canvas.Font, *basefont.Outliner] 字体轮廓缓存
func (r *Renderer) canvasFontOutlines() *renderCache[*canvas.Font, *basefont.Outliner] {
key := canvasOutlineCacheKey{}
if state, ok := r.sharedBackendStates[key]; ok {
return state.(*renderCache[*canvas.Font, *basefont.Outliner])
}
cache := &renderCache[*canvas.Font, *basefont.Outliner]{limit: 32 << 20}
r.sharedBackendStates[key] = cache
return cache
}
// canvasState 获取当前渲染器的Canvas私有缓存,按需创建且不向其他后端暴露
// 返回: *canvasBackendState Canvas缓存
func (r *Renderer) canvasState() *canvasBackendState {
key := CanvasBackend{}
if state, ok := r.backendStates[key]; ok {
return state.(*canvasBackendState)
}
state := &canvasBackendState{
images: renderCache[*EncodedImage, image.Image]{limit: imageCacheLimit},
}
state.resetFonts()
r.backendStates[key] = state
return state
}
// resetFonts 清除Canvas字体及字形缓存,保留编码图片适配对象
func (s *canvasBackendState) resetFonts() {
s.fontMap = make(map[string]*canvas.FontFamily)
s.svgFontCache = make(map[*canvas.Font]SVGFont)
s.textGlyphPathCache = renderCache[textGlyphPathCacheKey, textGlyphPathCacheValue]{limit: 16 << 20}
s.fontOutlines = make(map[*canvas.Font]*basefont.Outliner)
}
// canvasGeometryDigest 按完整坐标、路径顺序及几何容差计算摘要
// 入参: paths 非空路径序列
// 返回: [32]byte 几何摘要
func canvasGeometryDigest(paths ...*canvas.Path) [32]byte {
hash := sha256.New()
var data [512]byte
binary.LittleEndian.PutUint64(data[:8], math.Float64bits(canvas.Tolerance))
binary.LittleEndian.PutUint64(data[8:16], math.Float64bits(canvas.Epsilon))
binary.LittleEndian.PutUint64(data[16:24], math.Float64bits(canvas.BentleyOttmannEpsilon))
hash.Write(data[:24])
for _, path := range paths {
values := path.Data()
binary.LittleEndian.PutUint64(data[:8], uint64(len(values)))
hash.Write(data[:8])
for len(values) > 0 {
count := min(len(values), len(data)/8)
for i, value := range values[:count] {
binary.LittleEndian.PutUint64(data[i*8:], math.Float64bits(value))
}
hash.Write(data[:count*8])
values = values[count:]
}
}
var key [32]byte
hash.Sum(key[:0])
return key
}
// settleCanvasPath 复用复杂路径的填充规范化结果,返回独立副本
// 入参: path 页面坐标路径, rule 填充规则
// 返回: *canvas.Path 规范化填充轮廓
func (r *Renderer) settleCanvasPath(path *canvas.Path, rule canvas.FillRule) *canvas.Path {
if len(path.Data()) < 256 {
return path.Settle(rule)
}
key := canvasFillKey{digest: canvasGeometryDigest(path), rule: rule}
cache := r.canvasSettledPaths()
if result, ok := cache.get(key); ok {
return result.Copy()
}
result := path.Settle(rule)
if cost := len(result.Data())*8 + 256; cost <= cache.limit {
cache.put(key, result.Copy(), cost)
}
return result
}