mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-08-30 12:12:40 +08:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d03448b6c | ||
|
|
9462425a25 |
@@ -3,9 +3,9 @@ module github.com/xiaoqidun/ofdgo
|
|||||||
go 1.25.0
|
go 1.25.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/tdewolff/canvas v0.0.0-20260708151538-b3beae08c578
|
github.com/tdewolff/canvas v0.0.0-20260714230319-248e24504c3b
|
||||||
github.com/tdewolff/font v0.0.0-20260527091451-1663e68cb8a4
|
github.com/tdewolff/font v0.0.0-20260527091451-1663e68cb8a4
|
||||||
github.com/xiaoqidun/jbig2 v0.0.0-20260714021516-ffe617d6fbc8
|
github.com/xiaoqidun/jbig2 v0.0.0-20260728034722-cddd57533c97
|
||||||
golang.org/x/image v0.44.0
|
golang.org/x/image v0.44.0
|
||||||
golang.org/x/text v0.40.0
|
golang.org/x/text v0.40.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -61,8 +61,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
|||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
github.com/tdewolff/canvas v0.0.0-20260708151538-b3beae08c578 h1:OKVthD6KmBTDWsxnPkmDLW35yp+lvoqWsMeteXOZW+4=
|
github.com/tdewolff/canvas v0.0.0-20260714230319-248e24504c3b h1:nfrp/2YyXxBdhIKCdtNWRfy3C89CpD2LVu467JDU66w=
|
||||||
github.com/tdewolff/canvas v0.0.0-20260708151538-b3beae08c578/go.mod h1:Lz12ClttuKvo2jb1UxBfJJWmKV0LmEc8IRo/30R7z+k=
|
github.com/tdewolff/canvas v0.0.0-20260714230319-248e24504c3b/go.mod h1:EUnkUjU6A6BOIzmtICjy/2nGxJa1oXXYLv0kVELAcYE=
|
||||||
github.com/tdewolff/font v0.0.0-20260527091451-1663e68cb8a4 h1:QmSG/RtOlLPJDLJ24XHSDEu4iSgcG+s9sdRg7wTdfpk=
|
github.com/tdewolff/font v0.0.0-20260527091451-1663e68cb8a4 h1:QmSG/RtOlLPJDLJ24XHSDEu4iSgcG+s9sdRg7wTdfpk=
|
||||||
github.com/tdewolff/font v0.0.0-20260527091451-1663e68cb8a4/go.mod h1:tQzLTuJVLs4Pp6Vxdn3PJ7sAe4gIuph3SBm/BmdlmR0=
|
github.com/tdewolff/font v0.0.0-20260527091451-1663e68cb8a4/go.mod h1:tQzLTuJVLs4Pp6Vxdn3PJ7sAe4gIuph3SBm/BmdlmR0=
|
||||||
github.com/tdewolff/minify/v2 v2.24.13 h1:xrcF7gKDnUszseEY9WX9mUlZII2v2Go/QAcAwRASw58=
|
github.com/tdewolff/minify/v2 v2.24.13 h1:xrcF7gKDnUszseEY9WX9mUlZII2v2Go/QAcAwRASw58=
|
||||||
@@ -73,8 +73,8 @@ github.com/tdewolff/test v1.0.12 h1:7F21DqIajswxuche0geHdrUZRCWE4oko4b7bcmkkrxk=
|
|||||||
github.com/tdewolff/test v1.0.12/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
|
github.com/tdewolff/test v1.0.12/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
|
||||||
github.com/wcharczuk/go-chart/v2 v2.1.2 h1:Y17/oYNuXwZg6TFag06qe8sBajwwsuvPiJJXcUcLL6E=
|
github.com/wcharczuk/go-chart/v2 v2.1.2 h1:Y17/oYNuXwZg6TFag06qe8sBajwwsuvPiJJXcUcLL6E=
|
||||||
github.com/wcharczuk/go-chart/v2 v2.1.2/go.mod h1:Zi4hbaqlWpYajnXB2K22IUYVXRXaLfSGNNR7P4ukyyQ=
|
github.com/wcharczuk/go-chart/v2 v2.1.2/go.mod h1:Zi4hbaqlWpYajnXB2K22IUYVXRXaLfSGNNR7P4ukyyQ=
|
||||||
github.com/xiaoqidun/jbig2 v0.0.0-20260714021516-ffe617d6fbc8 h1:HSruvQiN+t72xcn4VQyvB4GRCyX0dnu3+6k4DFnmPvM=
|
github.com/xiaoqidun/jbig2 v0.0.0-20260728034722-cddd57533c97 h1:r9GZyTcO5Hs7y14JTShAeDp8d54jjFyCTVVgPLFz1Yk=
|
||||||
github.com/xiaoqidun/jbig2 v0.0.0-20260714021516-ffe617d6fbc8/go.mod h1:4ieZvd1EYR3zUzaAT4H8vLDb6XO+yjnxuh33So/5iC4=
|
github.com/xiaoqidun/jbig2 v0.0.0-20260728034722-cddd57533c97/go.mod h1:4ieZvd1EYR3zUzaAT4H8vLDb6XO+yjnxuh33So/5iC4=
|
||||||
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
|
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
|
||||||
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
|
|||||||
@@ -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