feat(性能优化): 优化资源复用、字体校验和图片导出性能
build.yaml / build (push) Successful in 35s

This commit is contained in:
xiaoqidun committed 2026-09-12 17:34:43 +08:00
1 parent 3c15a18546
commit 3b2950a9c1
3 files changed
+40 -3

No files matched your search

+9 -1
View File
@@ -168,10 +168,18 @@ func (r *Renderer) fontInfo(font Font) FontInfo {
Embedded: font.FontFile != "",
}
if info.Embedded {
file, err := r.Reader.openFile(r.Reader.ResPath(font.FontFile))
name := r.Reader.ResPath(font.FontFile)
var err error
if !r.Reader.fontFilesChecked[name] {
var file io.ReadCloser
file, err = r.Reader.openFile(name)
if err == nil {
_, err = io.Copy(io.Discard, file)
file.Close()
if err == nil {
r.Reader.fontFilesChecked[name] = true
}
}
}
if err == nil {
info.Status = FontStatusEmbedded
+8
View File
@@ -31,8 +31,10 @@ type Reader struct {
OFD *OFD
RootDir string
ResMap map[string]string
resourcesRead map[string]bool
fontCache map[string]*Font
fontResourcesRead bool
fontFilesChecked map[string]bool
colorSpaceCache map[string]*ColorSpace
drawParamCache map[string]*DrawParam
compositeGraphicUnitCache map[string]*CompositeGraphicUnit
@@ -76,7 +78,9 @@ func (r *Reader) initRoot() error {
}
r.OFD = &ofd
r.ResMap = make(map[string]string)
r.resourcesRead = make(map[string]bool)
r.fontCache = make(map[string]*Font)
r.fontFilesChecked = make(map[string]bool)
r.colorSpaceCache = make(map[string]*ColorSpace)
r.drawParamCache = make(map[string]*DrawParam)
r.compositeGraphicUnitCache = make(map[string]*CompositeGraphicUnit)
@@ -182,6 +186,9 @@ func (r *Reader) loadRes(resPath string) {
return
}
fullPath := r.ResPath(resPath)
if r.resourcesRead[fullPath] {
return
}
data, err := r.readFile(fullPath)
if err != nil {
return
@@ -217,6 +224,7 @@ func (r *Reader) loadRes(resPath string) {
cgu := &res.CompositeGraphicUnits.CompositeGraphicUnit[i]
r.compositeGraphicUnitCache[cgu.ID] = cgu
}
r.resourcesRead[fullPath] = true
}
// resolveResourcePath 解析资源文件路径
+23 -2
View File
@@ -26,16 +26,37 @@ import (
"github.com/tdewolff/canvas/renderers/rasterizer"
)
// rasterRenderer 保留页面物理尺寸的光栅渲染器
type rasterRenderer struct {
*rasterizer.Rasterizer
width, height float64
}
// Size 返回页面物理尺寸
// 返回: float64 宽度, float64 高度
func (r *rasterRenderer) Size() (float64, float64) {
return r.width, r.height
}
// RenderToImage 渲染为光栅图
// 入参: page 页面内容
// 返回: image.Image 图像对象, error 错误信息
func (r *Renderer) RenderToImage(page *PageContent) (image.Image, error) {
c, err := r.RenderPage(page)
box, err := r.GetPageBox(page)
if err != nil {
return nil, err
}
dpmm := r.DPI / 25.4
return rasterizer.Draw(c, canvas.DPMM(dpmm), canvas.DefaultColorSpace), nil
raster := &rasterRenderer{
Rasterizer: rasterizer.New(box.W, box.H, canvas.DPMM(dpmm), canvas.DefaultColorSpace),
width: box.W,
height: box.H,
}
if err := r.RenderPageToContext(canvas.NewContext(raster), page); err != nil {
return nil, err
}
raster.Close()
return raster.Image, nil
}
// RenderToSVG 渲染为SVG