refactor(重构代码): 专注于性能提升

This commit is contained in:
2026-07-28 11:12:21 +08:00
parent efe3324d4b
commit 9462425a25
4 changed files with 37 additions and 13 deletions
+1
View File
@@ -76,6 +76,7 @@ func NewRenderer(reader *Reader, opts ...RendererOption) *Renderer {
fontCache: make(map[fontCacheKey]*canvas.FontFamily), fontCache: make(map[fontCacheKey]*canvas.FontFamily),
fontSourceCache: make(map[string][]fontSource), fontSourceCache: make(map[string][]fontSource),
fontSourceUsed: make(map[string]fontSource), fontSourceUsed: make(map[string]fontSource),
fontDirCandidates: make(map[string][]fontFileCandidate),
textGlyphPathCache: make(map[textGlyphPathCacheKey]textGlyphPathCacheValue), textGlyphPathCache: make(map[textGlyphPathCacheKey]textGlyphPathCacheValue),
templatePageCache: make(map[string]*PageContent), templatePageCache: make(map[string]*PageContent),
} }
+5 -1
View File
@@ -250,8 +250,12 @@ func (r *Renderer) loadFontSource(family *canvas.FontFamily, source fontSource,
// 入参: dir 目录, patterns 模式列表, bold 是否粗体, italic 是否斜体 // 入参: dir 目录, patterns 模式列表, bold 是否粗体, italic 是否斜体
// 返回: []string 文件列表 // 返回: []string 文件列表
func (r *Renderer) matchFontFiles(dir string, patterns []string, bold, italic bool) []string { func (r *Renderer) matchFontFiles(dir string, patterns []string, bold, italic bool) []string {
candidates, ok := r.fontDirCandidates[dir]
if !ok {
files, _ := filepath.Glob(filepath.Join(dir, "*")) files, _ := filepath.Glob(filepath.Join(dir, "*"))
candidates := fontFileCandidates(files, filepath.Base) candidates = fontFileCandidates(files, filepath.Base)
r.fontDirCandidates[dir] = candidates
}
matches := make([]fontFileMatch, 0, len(candidates)) matches := make([]fontFileMatch, 0, len(candidates))
index := make(map[string]int, len(candidates)) index := make(map[string]int, len(candidates))
for _, matcher := range newFontPatternMatchers(patterns) { for _, matcher := range newFontPatternMatchers(patterns) {
+1
View File
@@ -36,6 +36,7 @@ type Renderer struct {
fontCache map[fontCacheKey]*canvas.FontFamily fontCache map[fontCacheKey]*canvas.FontFamily
fontSourceCache map[string][]fontSource fontSourceCache map[string][]fontSource
fontSourceUsed map[string]fontSource fontSourceUsed map[string]fontSource
fontDirCandidates map[string][]fontFileCandidate
textGlyphPathCache map[textGlyphPathCacheKey]textGlyphPathCacheValue textGlyphPathCache map[textGlyphPathCacheKey]textGlyphPathCacheValue
templatePageCache map[string]*PageContent templatePageCache map[string]*PageContent
fontDirs []string fontDirs []string
+21 -3
View File
@@ -266,8 +266,23 @@ func imageWithTransparentEdge(img image.Image) (image.Image, int) {
if opaque, ok := source.(interface{ Opaque() bool }); ok && opaque.Opaque() { if opaque, ok := source.(interface{ Opaque() bool }); ok && opaque.Opaque() {
return img, 0 return img, 0
} }
src := image.NewNRGBA(image.Rect(0, 0, w, h))
hasZero, hasVisible := false, false hasZero, hasVisible := false, false
src, ok := source.(*image.NRGBA)
if ok {
srcBounds := src.Bounds()
for y := srcBounds.Min.Y; y < srcBounds.Max.Y; y++ {
offset := src.PixOffset(srcBounds.Min.X, y) + 3
for x := 0; x < w; x++ {
if src.Pix[offset] == 0 {
hasZero = true
} else {
hasVisible = true
}
offset += 4
}
}
} else {
src = image.NewNRGBA(image.Rect(0, 0, w, h))
for y := 0; y < h; y++ { for y := 0; y < h; y++ {
for x := 0; x < w; x++ { for x := 0; x < w; x++ {
c := imageNRGBAAt(source, bounds.Min.X+x, bounds.Min.Y+y) c := imageNRGBAAt(source, bounds.Min.X+x, bounds.Min.Y+y)
@@ -279,15 +294,18 @@ func imageWithTransparentEdge(img image.Image) (image.Image, int) {
} }
} }
} }
}
if !hasZero || !hasVisible { if !hasZero || !hasVisible {
return img, 0 return img, 0
} }
srcBounds := src.Bounds()
out := image.NewNRGBA(image.Rect(0, 0, w+2, h+2)) out := image.NewNRGBA(image.Rect(0, 0, w+2, h+2))
for y := 0; y < h; y++ { for y := 0; y < h; y++ {
for x := 0; x < w; x++ { for x := 0; x < w; x++ {
c := src.NRGBAAt(x, y) sx, sy := srcBounds.Min.X+x, srcBounds.Min.Y+y
c := src.NRGBAAt(sx, sy)
if c.A == 0 { if c.A == 0 {
if edge, ok := transparentEdgeColor(src, x, y); ok { if edge, ok := transparentEdgeColor(src, sx, sy); ok {
c = edge c = edge
} }
} }