Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zbputils/contorl/serverpic -> rendercard #5

Merged
merged 4 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ linters:
fast: false
enable:
- bodyclose
- deadcode
- depguard
#- deadcode
#- depguard
- dogsled
- errcheck
- exportloopref
#- exportloopref
- exhaustive
#- funlen
#- goconst
Expand All @@ -39,13 +39,13 @@ linters:
- nolintlint
- rowserrcheck
- staticcheck
- structcheck
#- structcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
#- varcheck
- whitespace
- prealloc
- predeclared
Expand All @@ -62,7 +62,7 @@ run:
tests: false
skip-dirs:
- order
go: '1.19'
go: '1.20'

# output configuration options
output:
Expand Down
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/FloatTech/rendercard

go 1.19
go 1.20

require (
github.com/FloatTech/gg v1.1.2
github.com/FloatTech/floatbox v0.0.0-20240505082030-226ec6713e14
github.com/FloatTech/gg v1.1.3
github.com/disintegration/imaging v1.6.2
github.com/stretchr/testify v1.8.1
)
Expand All @@ -13,6 +14,7 @@ require (
github.com/fumiama/imgsz v0.0.2 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/image v0.3.0 // indirect
golang.org/x/image v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
35 changes: 8 additions & 27 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/FloatTech/gg v1.1.2 h1:YolgOYg3uDHc1+g0bLtt6QuRA/pvLn+b9IBCIhOOX88=
github.com/FloatTech/gg v1.1.2/go.mod h1:uzPzAeT35egARdRuu+1oyjU3CmTwCceoq3Vvje7LpcI=
github.com/FloatTech/floatbox v0.0.0-20240505082030-226ec6713e14 h1:8O0Iq9MnKsKowltY9txhOqcJdmGTjxHPQ4gEYzbJc9A=
github.com/FloatTech/floatbox v0.0.0-20240505082030-226ec6713e14/go.mod h1:OzGLhvmtz1TKIdGaJDd8pQumvD36UqK+dWsiCISmzQQ=
github.com/FloatTech/gg v1.1.3 h1:+GlL02lTKsxJQr4WCuNwVxC1/eBZrCvypCIBtxuOFb4=
github.com/FloatTech/gg v1.1.3/go.mod h1:/9oLP54CMfq4r+71XL26uaFTJ1uL1boAyX67680/1HE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -18,33 +20,12 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.3.0 h1:HTDXbdK9bjfSWkPzDJIw89W8CAtfFGduujWs33NLLsg=
golang.org/x/image v0.3.0/go.mod h1:fXd9211C/0VTlYuAcOhW8dY/RtEJqODXOWBDpmYBf+A=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/image v0.15.0 h1:kOELfmgrmJlw4Cdb7g/QGuB3CvDrXbqEIww/pNtNBm8=
golang.org/x/image v0.15.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
67 changes: 67 additions & 0 deletions logo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package rendercard

import (
"image"

"github.com/FloatTech/gg"
)

// RenderServerListLogo ...
func RenderServerListLogo(fontdata []byte) (img image.Image, err error) {
const w, h = 400, 200
canvas := gg.NewContext(w, h)
canvas.SetRGBA255(187, 122, 132, 255)
err = canvas.ParseFontFace(fontdata, 72)
if err != nil {
return
}
canvas.DrawStringAnchored("Server", 22, 45, 0, 1)
canvas.SetRGBA255(246, 166, 171, 255)

canvas.DrawStringAnchored("List", 219, 112, 0, 1)

canvas.SetRGBA255(135, 145, 173, 255)
err = canvas.ParseFontFace(fontdata, 48)
if err != nil {
return
}
canvas.DrawStringAnchored("服務列表", 23, 120, 0, 1)

canvas.SetRGBA255(103, 178, 240, 255)
err = canvas.ParseFontFace(fontdata, 36)
if err != nil {
return
}

canvas.DrawStringAnchored("サーバー", 82, 25, 0, 1)
canvas.DrawStringAnchored("リスト", 280, 83, 0, 1)

mask := canvas.AsMask()

stroked := gg.NewContext(w, h)
err = stroked.SetMask(mask)
if err != nil {
return
}
stroked.SetRGBA255(255, 255, 255, 255)
stroked.DrawRectangle(0, 0, float64(stroked.W()), float64(stroked.H()))
stroked.Fill()

strokedimg := stroked.Image()
coloredimg := canvas.Image()

canvas = gg.NewContext(w, h)

canvas.DrawImage(strokedimg, -3, 0)
canvas.DrawImage(strokedimg, 3, 0)
canvas.DrawImage(strokedimg, 0, -3)
canvas.DrawImage(strokedimg, 0, 3)
canvas.DrawImage(strokedimg, -3, -3)
canvas.DrawImage(strokedimg, 3, 3)
canvas.DrawImage(strokedimg, 3, -3)
canvas.DrawImage(strokedimg, -3, 3)
canvas.DrawImage(coloredimg, 0, 0)

img = canvas.Image()
return
}
177 changes: 177 additions & 0 deletions serverpic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package rendercard

import (
"image"
"sync"

"github.com/FloatTech/floatbox/math"

"github.com/FloatTech/gg"
)

type plugininfo struct {
Name string
Brief string
Status bool
}

// RenderServerPic ...
func RenderServerPic(pluginlist []*plugininfo, torussd, glowsd []byte, zbplogopath string, serverlistlogo image.Image) (img image.Image, err error) {
logo, err := gg.LoadImage(zbplogopath)
if err != nil {
return
}
listnum := len(pluginlist)
ln := math.Ceil(listnum, 3)
w := (290+24)*3 + 24
h := serverlistlogo.Bounds().Dy() + ln*(80+16) + serverlistlogo.Bounds().Dy()/3
canvas := gg.NewContext(w, h)

canvas.SetRGBA255(235, 235, 235, 255)
canvas.Clear()

canvas.SetRGBA255(135, 144, 173, 255)
canvas.NewSubPath()
canvas.MoveTo(0, 0)
canvas.LineTo(float64(canvas.W()), 140)
canvas.LineTo(float64(canvas.W()), 0)
canvas.ClosePath()
canvas.Fill()

canvas.NewSubPath()
canvas.MoveTo(float64(canvas.W()), float64(canvas.H()))
canvas.LineTo(0, float64(canvas.H()))
canvas.LineTo(0, float64(canvas.H())-140)
canvas.ClosePath()
canvas.Fill()

canvas.SetRGBA255(247, 177, 170, 255)
canvas.NewSubPath()
canvas.MoveTo(0, 0)
canvas.LineTo(float64(canvas.W()), 0)
canvas.LineTo(float64(canvas.W()), 70)
canvas.LineTo(0, 270)
canvas.ClosePath()
canvas.Fill()

canvas.NewSubPath()
canvas.MoveTo(float64(canvas.W()), float64(canvas.H()))
canvas.LineTo(0, float64(canvas.H()))
canvas.LineTo(0, float64(canvas.H())-70)
canvas.LineTo(float64(canvas.W()), float64(canvas.H())-270)
canvas.ClosePath()
canvas.Fill()

canvas.SetRGBA255(186, 113, 132, 255)
canvas.NewSubPath()
canvas.MoveTo(0, 0)
canvas.LineTo(float64(canvas.W()), 0)
canvas.LineTo(float64(canvas.W()), 35)
canvas.LineTo(0, 160)
canvas.ClosePath()
canvas.Fill()

canvas.NewSubPath()
canvas.MoveTo(float64(canvas.W()), float64(canvas.H()))
canvas.LineTo(0, float64(canvas.H()))
canvas.LineTo(0, float64(canvas.H())-35)
canvas.LineTo(float64(canvas.W()), float64(canvas.H())-160)
canvas.ClosePath()
canvas.Fill()

canvas.ScaleAbout(0.5, 0.5, float64(canvas.W()/4), 0)
canvas.DrawImageAnchored(logo, canvas.W()/4, 0, 0.5, 0)
canvas.Identity()

canvas.DrawImageAnchored(serverlistlogo, canvas.W()/4*3, 0, 0.5, 0)

cardimgs := make([]image.Image, 4)

wg := sync.WaitGroup{}
cardsnum := math.Ceil(ln, 4) * 3
wg.Add(4)
for i := 0; i < 3; i++ {
a := i * cardsnum
b := (i + 1) * cardsnum
go func(i int, list []*plugininfo) {
defer wg.Done()
cardimgs[i], err = renderinfocards(torussd, glowsd, list)
if err != nil {
return
}
}(i, pluginlist[a:b])
}
go func() {
defer wg.Done()
cardimgs[3], err = renderinfocards(torussd, glowsd, pluginlist[cardsnum*3:])
if err != nil {
return
}
}()
wg.Wait()
spacing := 0
for i := 0; i < len(cardimgs); i++ {
canvas.DrawImage(cardimgs[i], 0, serverlistlogo.Bounds().Dy()+spacing)
spacing += cardimgs[i].Bounds().Dy()
}

img = canvas.Image()
return
}

func renderinfocards(torussd, glowsd []byte, plugininfos []*plugininfo) (img image.Image, err error) {
w := (290+24)*3 + 24
cardnum := len(plugininfos)
h := math.Ceil(cardnum, 3) * (80 + 16)
cardw, cardh := 290.0, 80.0
spacingw, spacingh := 24.0, 16.0
canvas := gg.NewContext(w, h)
beginw, beginh := 24.0, 0.0
for i := 0; i < cardnum; i++ {
canvas.SetRGBA255(204, 51, 51, 255)
if plugininfos[i].Status {
canvas.SetRGBA255(136, 178, 0, 255)
}
canvas.DrawRoundedRectangle(beginw, beginh, cardw/2, cardh, 16)
canvas.Fill()

canvas.SetRGBA255(34, 26, 33, 255)
canvas.DrawRoundedRectangle(beginw+10, beginh, cardw-10, cardh, 16)
canvas.Fill()
beginw += cardw + spacingw
if (i+1)%3 == 0 {
beginw = spacingw
beginh += cardh + spacingh
}
}

err = canvas.ParseFontFace(torussd, 36)
if err != nil {
return
}
canvas.SetRGBA255(235, 235, 235, 255)
beginw, beginh = 24.0, 0.0
for i := 0; i < cardnum; i++ {
canvas.DrawStringAnchored(plugininfos[i].Name, beginw+14, beginh+canvas.FontHeight()/2+4, 0, 0.5)
beginw += cardw + spacingw
if (i+1)%3 == 0 {
beginw = spacingw
beginh += cardh + spacingh
}
}
err = canvas.ParseFontFace(glowsd, 16)
if err != nil {
return
}
beginw, beginh = 24.0, 0.0
for i := 0; i < cardnum; i++ {
canvas.DrawStringAnchored(plugininfos[i].Brief, beginw+14, beginh+cardh-canvas.FontHeight()-4, 0, 0.5)
beginw += cardw + spacingw
if (i+1)%3 == 0 {
beginw = spacingw
beginh += cardh + spacingh
}
}
img = canvas.Image()
return
}
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func Truncate(fontdata []byte, texts []string, maxW, fontsize float64) (newtexts
width, _ := one.MeasureString(tmp.String())
if width > maxW {
break
} else {
res = append(res, t)
}
res = append(res, t)

}
newlinetext := string(res)
newtexts = append(newtexts, newlinetext)
Expand Down
Loading