mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-08-30 12:12:40 +08:00
refactor(重构代码): 专注于性能提升
This commit is contained in:
@@ -76,6 +76,7 @@ func NewRenderer(reader *Reader, opts ...RendererOption) *Renderer {
|
||||
fontCache: make(map[fontCacheKey]*canvas.FontFamily),
|
||||
fontSourceCache: make(map[string][]fontSource),
|
||||
fontSourceUsed: make(map[string]fontSource),
|
||||
fontDirCandidates: make(map[string][]fontFileCandidate),
|
||||
textGlyphPathCache: make(map[textGlyphPathCacheKey]textGlyphPathCacheValue),
|
||||
templatePageCache: make(map[string]*PageContent),
|
||||
}
|
||||
|
||||
@@ -250,8 +250,12 @@ func (r *Renderer) loadFontSource(family *canvas.FontFamily, source fontSource,
|
||||
// 入参: dir 目录, patterns 模式列表, bold 是否粗体, italic 是否斜体
|
||||
// 返回: []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, "*"))
|
||||
candidates := fontFileCandidates(files, filepath.Base)
|
||||
candidates = fontFileCandidates(files, filepath.Base)
|
||||
r.fontDirCandidates[dir] = candidates
|
||||
}
|
||||
matches := make([]fontFileMatch, 0, len(candidates))
|
||||
index := make(map[string]int, len(candidates))
|
||||
for _, matcher := range newFontPatternMatchers(patterns) {
|
||||
|
||||
@@ -36,6 +36,7 @@ type Renderer struct {
|
||||
fontCache map[fontCacheKey]*canvas.FontFamily
|
||||
fontSourceCache map[string][]fontSource
|
||||
fontSourceUsed map[string]fontSource
|
||||
fontDirCandidates map[string][]fontFileCandidate
|
||||
textGlyphPathCache map[textGlyphPathCacheKey]textGlyphPathCacheValue
|
||||
templatePageCache map[string]*PageContent
|
||||
fontDirs []string
|
||||
|
||||
+21
-3
@@ -266,8 +266,23 @@ func imageWithTransparentEdge(img image.Image) (image.Image, int) {
|
||||
if opaque, ok := source.(interface{ Opaque() bool }); ok && opaque.Opaque() {
|
||||
return img, 0
|
||||
}
|
||||
src := image.NewNRGBA(image.Rect(0, 0, w, h))
|
||||
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 x := 0; x < w; x++ {
|
||||
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 {
|
||||
return img, 0
|
||||
}
|
||||
srcBounds := src.Bounds()
|
||||
out := image.NewNRGBA(image.Rect(0, 0, w+2, h+2))
|
||||
for y := 0; y < h; y++ {
|
||||
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 edge, ok := transparentEdgeColor(src, x, y); ok {
|
||||
if edge, ok := transparentEdgeColor(src, sx, sy); ok {
|
||||
c = edge
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user