feat(性能优化): 优化蒙版处理与字体更新性能,保持渲染一致

This commit is contained in:
xiaoqidun committed 2026-09-12 12:25:18 +08:00
1 parent c5abb3667a
commit 565b7f6db4
4 files changed
+36 -17

No files matched your search

+3 -6
View File
@@ -237,18 +237,15 @@ func (s *Session) Close() error {
// 入参: fonts 字体文件列表 // 入参: fonts 字体文件列表
// 返回: error 错误信息 // 返回: error 错误信息
func (s *Session) SetFonts(fonts []FontFile) error { func (s *Session) SetFonts(fonts []FontFile) error {
opts := []ofdgo.RendererOption{
ofdgo.WithDPI(s.Renderer.DPI),
ofdgo.WithAnnotations(s.Renderer.RenderAnnotations),
}
if len(fonts) > 0 { if len(fonts) > 0 {
fontFS := ofdgo.NewFontFS(fonts) fontFS := ofdgo.NewFontFS(fonts)
if fontFS.Len() == 0 { if fontFS.Len() == 0 {
return fmt.Errorf("invalid font file") return fmt.Errorf("invalid font file")
} }
opts = append(opts, ofdgo.WithFontFS(fontFS)) s.Renderer.SetFontFS(fontFS)
} else {
s.Renderer.SetFontFS()
} }
s.Renderer = ofdgo.NewRenderer(s.Reader, opts...)
return nil return nil
} }
+1 -11
View File
@@ -20,8 +20,6 @@ import (
"image" "image"
"io" "io"
"io/fs" "io/fs"
"github.com/tdewolff/canvas"
) )
// Open 打开OFD文件 // Open 打开OFD文件
@@ -71,18 +69,10 @@ func NewRenderer(reader *Reader, opts ...RendererOption) *Renderer {
RenderAnnotations: true, RenderAnnotations: true,
DrawParams: reader.drawParamCache, DrawParams: reader.drawParamCache,
CompositeGraphicUnits: reader.compositeGraphicUnitCache, CompositeGraphicUnits: reader.compositeGraphicUnitCache,
FontMap: make(map[string]*canvas.FontFamily),
FontGIDMap: make(map[string]map[uint16]rune),
FontCIDMap: make(map[string]map[uint16]rune),
fontCache: make(map[fontCacheKey]*canvas.FontFamily),
fontSourceCache: make(map[string][]fontSource),
fontSourceUsed: make(map[string]fontSource),
fontDirCandidates: make(map[string][]fontFileCandidate),
fontFSCandidates: make(map[int][]fontFileCandidate),
textGlyphPathCache: make(map[textGlyphPathCacheKey]textGlyphPathCacheValue),
templatePageCache: make(map[string]*PageContent), templatePageCache: make(map[string]*PageContent),
imageCache: make(map[string]image.Image), imageCache: make(map[string]image.Image),
} }
r.resetFontCache()
for _, opt := range opts { for _, opt := range opts {
opt(r) opt(r)
} }
+20
View File
@@ -50,6 +50,26 @@ type Renderer struct {
// RendererOption 渲染器配置选项 // RendererOption 渲染器配置选项
type RendererOption func(*Renderer) type RendererOption func(*Renderer)
// SetFontFS 替换外部字体文件系统并重置字体缓存
// 入参: fsys 字体文件系统
func (r *Renderer) SetFontFS(fsys ...fs.FS) {
r.fontFS = append([]fs.FS(nil), fsys...)
r.resetFontCache()
}
// resetFontCache 重置字体匹配、加载和字形缓存
func (r *Renderer) resetFontCache() {
r.FontMap = make(map[string]*canvas.FontFamily)
r.FontGIDMap = make(map[string]map[uint16]rune)
r.FontCIDMap = make(map[string]map[uint16]rune)
r.fontCache = make(map[fontCacheKey]*canvas.FontFamily)
r.fontSourceCache = make(map[string][]fontSource)
r.fontSourceUsed = make(map[string]fontSource)
r.fontDirCandidates = make(map[string][]fontFileCandidate)
r.fontFSCandidates = make(map[int][]fontFileCandidate)
r.textGlyphPathCache = make(map[textGlyphPathCacheKey]textGlyphPathCacheValue)
}
// RenderPage 渲染特定页面内容 // RenderPage 渲染特定页面内容
// 入参: page 页面内容 // 入参: page 页面内容
// 返回: *canvas.Canvas 画布实例, error 错误信息 // 返回: *canvas.Canvas 画布实例, error 错误信息
+12
View File
@@ -140,6 +140,18 @@ func imageWithMask(img, mask image.Image) image.Image {
maskBounds = bounds maskBounds = bounds
} }
out := image.NewNRGBA(bounds) out := image.NewNRGBA(bounds)
if src, ok := source.(*image.NRGBA); ok {
for y := 0; y < bounds.Dy(); y++ {
offset := src.PixOffset(bounds.Min.X, bounds.Min.Y+y)
row := out.Pix[y*out.Stride : (y+1)*out.Stride]
copy(row, src.Pix[offset:offset+len(row)])
for x := 3; x < len(row); x += 4 {
a := imageGrayAt(opacity, maskBounds.Min.X+x/4, maskBounds.Min.Y+y).Y
row[x] = uint8(int(row[x]) * int(a) / 255)
}
}
return out
}
for y := bounds.Min.Y; y < bounds.Max.Y; y++ { for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ { for x := bounds.Min.X; x < bounds.Max.X; x++ {
mx := maskBounds.Min.X + x - bounds.Min.X mx := maskBounds.Min.X + x - bounds.Min.X