From e6b7826b486e548c4a38210813560be476fa19d2 Mon Sep 17 00:00:00 2001 From: Jiang-Red <79574799+Jiang-Red@users.noreply.github.com> Date: Tue, 20 Aug 2024 18:11:06 +0000 Subject: [PATCH] new servicepic!!! --- control/rule.go | 87 +-------- control/servicepic.go | 403 +++++++++++++++++++++++------------------- img/text/font.go | 6 +- 3 files changed, 232 insertions(+), 264 deletions(-) diff --git a/control/rule.go b/control/rule.go index 17fe357..ad31e59 100644 --- a/control/rule.go +++ b/control/rule.go @@ -2,26 +2,18 @@ package control import ( - "encoding/base64" - "image" - "image/jpeg" "os" "strconv" "strings" - "sync" - "github.com/sirupsen/logrus" zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/extension" "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/floatbox/binary" - "github.com/FloatTech/floatbox/file" "github.com/FloatTech/imgfactory" "github.com/FloatTech/rendercard" ctrl "github.com/FloatTech/zbpctrl" - - "github.com/FloatTech/zbputils/ctxext" ) const ( @@ -30,7 +22,6 @@ const ( // Md5File ... Md5File = StorageFolder + "stor.spb" dbfile = StorageFolder + "plugins.db" - lnfile = StorageFolder + "lnperpg.txt" ) var ( @@ -79,21 +70,6 @@ func init() { if err != nil { panic(err) } - // 载入用户配置 - if file.IsExist(lnfile) { - data, err := os.ReadFile(lnfile) - if err != nil { - logrus.Warnln("[control] 读取配置文件失败,将使用默认的显示行数:", err) - } else { - mun, err := strconv.Atoi(binary.BytesToString(data)) - if err != nil { - logrus.Warnln("[control] 获取设置的服务列表显示行数错误,将使用默认的显示行数:", err) - } else if mun > 0 { - lnperpg = mun - logrus.Infoln("[control] 获取到当前设置的服务列表显示行数为:", lnperpg) - } - } - } zero.OnCommandGroup([]string{ "响应", "response", "沉默", "silence", }, zero.UserOrGrpAdmin, zero.OnlyToMe).SetBlock(true).SecondPriority().Handle(func(ctx *zero.Ctx) { @@ -474,65 +450,18 @@ func init() { if gid == 0 { gid = -ctx.Event.UserID } - var imgs []image.Image - imgs, err = drawservicesof(gid) + img, err := renderservepicof(gid) if err != nil { ctx.SendChain(message.Text("ERROR: ", err)) return } - if len(imgs) > 1 { - wg := sync.WaitGroup{} - msg := make(message.Message, len(imgs)) - wg.Add(len(imgs)) - for i := 0; i < len(imgs); i++ { - go func(i int) { - defer wg.Done() - msg[i] = ctxext.FakeSenderForwardNode(ctx, message.Image(binary.BytesToString(binary.NewWriterF(func(w *binary.Writer) { - w.WriteString("base64://") - encoder := base64.NewEncoder(base64.StdEncoding, w) - var opt jpeg.Options - opt.Quality = 70 - if err1 := jpeg.Encode(encoder, imgs[i], &opt); err1 != nil { - err = err1 - return - } - _ = encoder.Close() - })))) - }(i) - } - wg.Wait() - if id := ctx.Send(msg); id.ID() == 0 { - ctx.SendChain(message.Text("ERROR: 可能被风控了")) - } - } else { - b64, err := imgfactory.ToBase64(imgs[0]) - if err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - if id := ctx.SendChain(message.Image("base64://" + binary.BytesToString(b64))); id.ID() == 0 { - ctx.SendChain(message.Text("ERROR: 可能被风控了")) - } + b64, err := imgfactory.ToBase64(img) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + if id := ctx.SendChain(message.Image("base64://" + binary.BytesToString(b64))); id.ID() == 0 { + ctx.SendChain(message.Text("ERROR: 可能被风控了")) } }) - - zero.OnCommand("设置服务列表显示行数", zero.SuperUserPermission, zero.OnlyToMe).SetBlock(true).SecondPriority().Handle(func(ctx *zero.Ctx) { - model := extension.CommandModel{} - _ = ctx.Parse(&model) - mun, err := strconv.Atoi(model.Args) - if err != nil { - ctx.SendChain(message.Text("请输入正确的数字")) - return - } - err = os.WriteFile(lnfile, binary.StringToBytes(model.Args), 0644) - if err != nil { - ctx.SendChain(message.Text(err)) - return - } - lnperpg = mun - // 清除缓存 - titlecache = nil - fullpageshadowcache = nil - ctx.SendChain(message.Text("已设置列表单页显示数为 " + strconv.Itoa(lnperpg))) - }) } diff --git a/control/servicepic.go b/control/servicepic.go index 02e6a1e..bd50e37 100644 --- a/control/servicepic.go +++ b/control/servicepic.go @@ -3,21 +3,14 @@ package control import ( "image" "os" - "runtime" - "strings" "sync" - "time" "github.com/FloatTech/floatbox/file" "github.com/FloatTech/floatbox/math" "github.com/FloatTech/gg" - "github.com/FloatTech/ttl" ctrl "github.com/FloatTech/zbpctrl" - "github.com/disintegration/imaging" zero "github.com/wdvxdr1123/ZeroBot" - "github.com/FloatTech/rendercard" - "github.com/FloatTech/zbputils/img/text" ) @@ -30,23 +23,18 @@ const ( type plugininfo struct { name string brief string - banner string status bool } var ( - // 标题缓存 - titlecache image.Image - // 卡片缓存 - cardcache = ttl.NewCache[uint64, image.Image](time.Hour * 12) - // 阴影缓存 - fullpageshadowcache image.Image - // lnperpg 每页行数 - lnperpg = 9 // 字体 GlowSans 数据 glowsd []byte // 字体 Impact 数据 impactd []byte + // 字体 Torus 数据 + torussd []byte + // 字体 Yuruka 数据 + yurukasd []byte ) func init() { @@ -58,6 +46,10 @@ func init() { if err != nil { panic(err) } + _, err = file.GetLazyData(kanbanpath+"zbpuwu.png", Md5File, false) + if err != nil { + panic(err) + } glowsd, err = file.GetLazyData(text.GlowSansFontFile, Md5File, true) if err != nil { panic(err) @@ -66,198 +58,241 @@ func init() { if err != nil { panic(err) } + torussd, err = file.GetLazyData(text.TorusFontFile, Md5File, true) + if err != nil { + panic(err) + } + yurukasd, err = file.GetLazyData(text.YurukaFontFile, Md5File, true) + if err != nil { + panic(err) + } } -func drawservicesof(gid int64) (imgs []image.Image, err error) { +func renderservepicof(gid int64) (img image.Image, err error) { pluginlist := make([]plugininfo, len(priomap)) ForEachByPrio(func(i int, manager *ctrl.Control[*zero.Ctx]) bool { pluginlist[i] = plugininfo{ name: manager.Service, brief: manager.Options.Brief, - banner: manager.Options.Banner, status: manager.IsEnabledIn(gid), } return true }) - // 分页 - if len(pluginlist) < lnperpg*3 { - // 如果单页显示数量超出了总数量 - lnperpg = math.Ceil(len(pluginlist), 3) + logo, err := gg.LoadImage(kanbanpath + "zbpuwu.png") + if err != nil { + return + } + serverlistlogo, err := renderserverlistlogo() + if err != nil { + return + } + max := len(pluginlist) + ln := math.Ceil(max, 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(list) + if err != nil { + return + } + }(i, pluginlist[a:b]) } - if titlecache == nil { - titlecache, err = (&rendercard.Title{ - Line: lnperpg, - LeftTitle: "服务列表", - LeftSubtitle: "service_list", - RightTitle: "FloatTech", - RightSubtitle: "ZeroBot-Plugin", - TitleFontData: glowsd, - TextFontData: impactd, - ImagePath: kanbanpath + "kanban.png", - }).DrawTitle() + go func() { + defer wg.Done() + cardimgs[3], err = renderinfocards(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() } - cardlist := make([]image.Image, len(pluginlist)) - n := runtime.NumCPU() - - if len(pluginlist) <= n { - for k, info := range pluginlist { - banner := "" - switch { - case strings.HasPrefix(info.banner, "http"): - err = file.DownloadTo(info.banner, bannerpath+info.name+".png") - if err != nil { - return - } - banner = bannerpath + info.name + ".png" - case info.banner != "": - banner = info.banner - default: - _, err = file.GetCustomLazyData(bannerurl, bannerpath+info.name+".png") - if err == nil { - banner = bannerpath + info.name + ".png" - } - } - c := &rendercard.Title{ - IsEnabled: info.status, - LeftTitle: info.name, - LeftSubtitle: info.brief, - ImagePath: banner, - TitleFontData: impactd, - TextFontData: glowsd, - } - h := c.Sum64() - card := cardcache.Get(h) - if card == nil { - card, err = c.DrawCard() - if err != nil { - return - } - cardcache.Set(h, card) - } - cardlist[k] = card - } - } else { - batchsize := len(pluginlist) / n - wg := sync.WaitGroup{} - drawcard := func(info []plugininfo, cards []image.Image) { - defer wg.Done() - for k, info := range info { - banner := "" - switch { - case strings.HasPrefix(info.banner, "http"): - err1 := file.DownloadTo(info.banner, bannerpath+info.name+".png") - if err1 != nil { - err = err1 - return - } - banner = bannerpath + info.name + ".png" - case info.banner != "": - banner = info.banner - default: - _, err1 := file.GetCustomLazyData(bannerurl, bannerpath+info.name+".png") - if err1 == nil { - banner = bannerpath + info.name + ".png" - } - } - c := &rendercard.Title{ - IsEnabled: info.status, - LeftTitle: info.name, - LeftSubtitle: info.brief, - ImagePath: banner, - TitleFontData: impactd, - TextFontData: glowsd, - } - h := c.Sum64() - card := cardcache.Get(h) - if card == nil { - var err1 error - card, err1 = c.DrawCard() - if err1 != nil { - err = err1 - return - } - card = rendercard.Fillet(card, 8) - cardcache.Set(h, card) - } - cards[k] = card - } + img = canvas.Image() + return +} + +func renderinfocards(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) } - wg.Add(n) - for i := 0; i < n; i++ { - a := i * batchsize - b := (i + 1) * batchsize - go drawcard(pluginlist[a:b], cardlist[a:b]) + 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 } - if batchsize*n < len(pluginlist) { - wg.Add(1) - d := len(pluginlist) - batchsize*n - go drawcard(pluginlist[d:], cardlist[d:]) + } + + 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 } - wg.Wait() - if err != nil { - return + } + 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 +} - wg := sync.WaitGroup{} - cardnum := lnperpg * 3 - page := math.Ceil(len(pluginlist), cardnum) - imgs = make([]image.Image, page) - x, y := 30+2, 30+300+30+6+4 - if fullpageshadowcache == nil { - fullpageshadow := gg.NewContextForImage(rendercard.Transparency(titlecache, 0)) - fullpageshadow.SetRGBA255(0, 0, 0, 192) - for i := 0; i < cardnum; i++ { - fullpageshadow.DrawRoundedRectangle(float64(x), float64(y), 384-4, 256-4, 0) - fullpageshadow.Fill() - x += 384 + 30 - if (i+1)%3 == 0 { - x = 30 + 2 - y += 256 + 30 - } - } - fullpageshadowcache = imaging.Blur(fullpageshadow.Image(), 7) +func renderserverlistlogo() (img image.Image, err error) { + const w, h = 400, 200 + canvas := gg.NewContext(w, h) + canvas.SetRGBA255(187, 122, 132, 255) + err = canvas.ParseFontFace(yurukasd, 72) + if err != nil { + return } - wg.Add(page) - for l := 0; l < page; l++ { // 页数 - go func(l int, islast bool) { - defer wg.Done() - x, y := 30+2, 30+300+30+6+4 - var shadowimg image.Image - if islast && len(pluginlist)-cardnum*l < cardnum { - shadow := gg.NewContextForImage(rendercard.Transparency(titlecache, 0)) - shadow.SetRGBA255(0, 0, 0, 192) - for i := 0; i < len(pluginlist)-cardnum*l; i++ { - shadow.DrawRoundedRectangle(float64(x), float64(y), 384-4, 256-4, 0) - shadow.Fill() - x += 384 + 30 - if (i+1)%3 == 0 { - x = 30 + 2 - y += 256 + 30 - } - } - shadowimg = imaging.Blur(shadow.Image(), 7) - } else { - shadowimg = fullpageshadowcache - } - one := gg.NewContextForImage(titlecache) - x, y = 30, 30+300+30 - one.DrawImage(shadowimg, 0, 0) - for i := 0; i < math.Min(cardnum, len(pluginlist)-cardnum*l); i++ { - one.DrawImage(cardlist[(cardnum*l)+i], x, y) - x += 384 + 30 - if (i+1)%3 == 0 { - x = 30 - y += 256 + 30 - } - } - imgs[l] = one.Image() - }(l, l == page-1) + 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(yurukasd, 48) + if err != nil { + return } - wg.Wait() + canvas.DrawStringAnchored("服務列表", 23, 120, 0, 1) + + canvas.SetRGBA255(103, 178, 240, 255) + err = canvas.ParseFontFace(yurukasd, 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) + stroked.SetMask(mask) + 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 } diff --git a/img/text/font.go b/img/text/font.go index 16e72df..f4b7f8a 100644 --- a/img/text/font.go +++ b/img/text/font.go @@ -34,5 +34,9 @@ const ( // GNUUnifontFontFile GNU Unifont https://unifoundry.com/unifont/index.html GNUUnifontFontFile = FontPath + "unifont-15.0.06.ttf" // AiralFontFile ... - AiralFontFile = FontPath + "arial.ttf" + ArialFontFile = FontPath + "arial.ttf" + // TorusFontFile ... + TorusFontFile = FontPath + "Torus-Regular.otf" + // YurukaFontFile ... + YurukaFontFile = FontPath + "FOT-Yuruka Std.ttf" )