🔥 drop writer

This commit is contained in:
fumiama 2022-01-13 19:17:59 +08:00
parent f05f09d741
commit 107979c459
2 changed files with 17 additions and 35 deletions

View File

@ -7,14 +7,13 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"image" "image"
"io"
"math/rand" "math/rand"
"os" "os"
"strconv" "strconv"
"sync" "sync"
"time" "time"
"github.com/fogleman/gg" "github.com/fogleman/gg" // 注册了 jpg png gif
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot" zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
@ -24,7 +23,6 @@ import (
"github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/file"
"github.com/FloatTech/zbputils/math" "github.com/FloatTech/zbputils/math"
"github.com/FloatTech/zbputils/process" "github.com/FloatTech/zbputils/process"
"github.com/FloatTech/zbputils/txt2img"
) )
var ( var (
@ -154,17 +152,8 @@ func init() {
} }
dlmu.Lock() dlmu.Lock()
f, err := os.Create(cachefile)
if err != nil {
ctx.SendChain(message.Text("ERROR: ", err))
_ = f.Close()
_ = os.Remove(cachefile)
dlmu.Unlock()
return
}
// 绘制背景 // 绘制背景
err = draw(background, title, text, f) err = draw(background, title, text, cachefile)
_ = f.Close()
dlmu.Unlock() dlmu.Unlock()
if err != nil { if err != nil {
@ -216,23 +205,22 @@ func randtext(seed int64) (string, string, error) {
// @param title 签名 // @param title 签名
// @param text 签文 // @param text 签文
// @return 错误信息 // @return 错误信息
func draw(back image.Image, title, text string, dst io.Writer) error { func draw(back image.Image, title, text, cachefile string) error {
var txtc txt2img.TxtCanvas canvas := gg.NewContext(back.Bounds().Size().Y, back.Bounds().Size().X)
txtc.Canvas = gg.NewContext(back.Bounds().Size().Y, back.Bounds().Size().X) canvas.DrawImage(back, 0, 0)
txtc.Canvas.DrawImage(back, 0, 0)
// 写标题 // 写标题
txtc.Canvas.SetRGB(1, 1, 1) canvas.SetRGB(1, 1, 1)
if err := txtc.Canvas.LoadFontFace(font, 45); err != nil { if err := canvas.LoadFontFace(font, 45); err != nil {
return err return err
} }
sw, _ := txtc.Canvas.MeasureString(title) sw, _ := canvas.MeasureString(title)
txtc.Canvas.DrawString(title, 140-sw/2, 112) canvas.DrawString(title, 140-sw/2, 112)
// 写正文 // 写正文
txtc.Canvas.SetRGB(0, 0, 0) canvas.SetRGB(0, 0, 0)
if err := txtc.Canvas.LoadFontFace(font, 23); err != nil { if err := canvas.LoadFontFace(font, 23); err != nil {
return err return err
} }
tw, th := txtc.Canvas.MeasureString("测") tw, th := canvas.MeasureString("测")
tw, th = tw+10, th+10 tw, th = tw+10, th+10
r := []rune(text) r := []rune(text)
xsum := rowsnum(len(r), 9) xsum := rowsnum(len(r), 9)
@ -242,7 +230,7 @@ func draw(back image.Image, title, text string, dst io.Writer) error {
xnow := rowsnum(i+1, 9) xnow := rowsnum(i+1, 9)
ysum := math.Min(len(r)-(xnow-1)*9, 9) ysum := math.Min(len(r)-(xnow-1)*9, 9)
ynow := i%9 + 1 ynow := i%9 + 1
txtc.Canvas.DrawString(string(o), -offest(xsum, xnow, tw)+115, offest(ysum, ynow, th)+320.0) canvas.DrawString(string(o), -offest(xsum, xnow, tw)+115, offest(ysum, ynow, th)+320.0)
} }
case 2: case 2:
div := rowsnum(len(r), 2) div := rowsnum(len(r), 2)
@ -252,14 +240,13 @@ func draw(back image.Image, title, text string, dst io.Writer) error {
ynow := i%div + 1 ynow := i%div + 1
switch xnow { switch xnow {
case 1: case 1:
txtc.Canvas.DrawString(string(o), -offest(xsum, xnow, tw)+115, offest(9, ynow, th)+320.0) canvas.DrawString(string(o), -offest(xsum, xnow, tw)+115, offest(9, ynow, th)+320.0)
case 2: case 2:
txtc.Canvas.DrawString(string(o), -offest(xsum, xnow, tw)+115, offest(9, ynow+(9-ysum), th)+320.0) canvas.DrawString(string(o), -offest(xsum, xnow, tw)+115, offest(9, ynow+(9-ysum), th)+320.0)
} }
} }
} }
_, err := txtc.WriteTo(dst) return canvas.SavePNG(cachefile)
return err
} }
func offest(total, now int, distance float64) float64 { func offest(total, now int, distance float64) float64 {

View File

@ -3,7 +3,6 @@ package nativesetu
import ( import (
"bytes" "bytes"
"image" "image"
"io"
"io/fs" "io/fs"
"os" "os"
"sync" "sync"
@ -11,7 +10,7 @@ import (
"github.com/corona10/goimagehash" "github.com/corona10/goimagehash"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/wdvxdr1123/ZeroBot/utils/helper" "github.com/wdvxdr1123/ZeroBot/utils/helper"
"golang.org/x/image/webp" _ "golang.org/x/image/webp"
"github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/file"
"github.com/FloatTech/zbputils/process" "github.com/FloatTech/zbputils/process"
@ -105,10 +104,6 @@ func scanclass(root fs.FS, path, clsn string) error {
} }
b := bytes.NewReader(f) b := bytes.NewReader(f)
img, _, e := image.Decode(b) img, _, e := image.Decode(b)
if e != nil {
b.Seek(0, io.SeekStart)
img, e = webp.Decode(b)
}
if e != nil { if e != nil {
return e return e
} }