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),
|
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),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
||||||
files, _ := filepath.Glob(filepath.Join(dir, "*"))
|
candidates, ok := r.fontDirCandidates[dir]
|
||||||
candidates := fontFileCandidates(files, filepath.Base)
|
if !ok {
|
||||||
|
files, _ := filepath.Glob(filepath.Join(dir, "*"))
|
||||||
|
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) {
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
+29
-11
@@ -266,28 +266,46 @@ 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
|
||||||
for y := 0; y < h; y++ {
|
src, ok := source.(*image.NRGBA)
|
||||||
for x := 0; x < w; x++ {
|
if ok {
|
||||||
c := imageNRGBAAt(source, bounds.Min.X+x, bounds.Min.Y+y)
|
srcBounds := src.Bounds()
|
||||||
src.SetNRGBA(x, y, c)
|
for y := srcBounds.Min.Y; y < srcBounds.Max.Y; y++ {
|
||||||
if c.A == 0 {
|
offset := src.PixOffset(srcBounds.Min.X, y) + 3
|
||||||
hasZero = true
|
for x := 0; x < w; x++ {
|
||||||
} else {
|
if src.Pix[offset] == 0 {
|
||||||
hasVisible = true
|
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)
|
||||||
|
src.SetNRGBA(x, y, c)
|
||||||
|
if c.A == 0 {
|
||||||
|
hasZero = true
|
||||||
|
} else {
|
||||||
|
hasVisible = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user