Skip to content

Commit df4c043

Browse files
authored
抖动文字 (#647)
1 parent 864f92a commit df4c043

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ print("run[CQ:image,file="+j["img"]+"]")
719719

720720
`import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/font"`
721721

722-
- [x] (用[终末体|终末变体|紫罗兰体|樱酥体|Consolas体|苹方体])渲染文字xxx
722+
- [x] (用[终末体|终末变体|紫罗兰体|樱酥体|Consolas体|苹方体])渲染(抖动)文字xxx
723723
</details>
724724
<details>
725725
<summary>每日运势</summary>

plugin/font/main.go

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
package font
33

44
import (
5+
"bytes"
6+
"image"
7+
"image/color"
8+
"image/gif"
9+
"math/rand"
10+
"strings"
11+
512
"github.com/FloatTech/floatbox/binary"
13+
"github.com/FloatTech/floatbox/file"
14+
"github.com/FloatTech/gg"
15+
"github.com/FloatTech/imgfactory"
616
ctrl "github.com/FloatTech/zbpctrl"
717
"github.com/FloatTech/zbputils/control"
818
"github.com/FloatTech/zbputils/ctxext"
@@ -15,10 +25,10 @@ func init() {
1525
control.Register("font", &ctrl.Options[*zero.Ctx]{
1626
DisableOnDefault: false,
1727
Brief: "渲染任意文字到图片",
18-
Help: "- (用[字体])渲染文字xxx\n可选字体: [终末体|终末变体|紫罗兰体|樱酥体|Consolas体|苹方体]",
19-
}).OnRegex(`^(用.+)?渲染文字([\s\S]+)$`).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
28+
Help: "- (用[字体])渲染(抖动)文字xxx\n可选字体: [终末体|终末变体|紫罗兰体|樱酥体|Consolas体|苹方体]",
29+
}).OnRegex(`^(用.+)?渲染(抖动)?文字([\s\S]+)$`).SetBlock(true).Limit(ctxext.LimitByUser).Handle(func(ctx *zero.Ctx) {
2030
fnt := ctx.State["regex_matched"].([]string)[1]
21-
txt := ctx.State["regex_matched"].([]string)[2]
31+
txt := ctx.State["regex_matched"].([]string)[3]
2232
switch fnt {
2333
case "用终末体":
2434
fnt = text.SyumatuFontFile
@@ -35,11 +45,56 @@ func init() {
3545
default:
3646
fnt = text.FontFile
3747
}
38-
b, err := text.RenderToBase64(txt, fnt, 400, 20)
48+
if ctx.State["regex_matched"].([]string)[2] == "" {
49+
b, err := text.RenderToBase64(txt, fnt, 400, 20)
50+
if err != nil {
51+
ctx.SendChain(message.Text("ERROR: ", err))
52+
return
53+
}
54+
ctx.SendChain(message.Image("base64://" + binary.BytesToString(b)))
55+
return
56+
}
57+
nilx, nily := 1.0, 8.0
58+
s := []*image.NRGBA{}
59+
strlist := strings.Split(txt, "\n")
60+
data, err := file.GetLazyData(fnt, control.Md5File, true)
3961
if err != nil {
4062
ctx.SendChain(message.Text("ERROR: ", err))
4163
return
4264
}
43-
ctx.SendChain(message.Image("base64://" + binary.BytesToString(b)))
65+
//获得画布预计
66+
testcov := gg.NewContext(1, 1)
67+
if err = testcov.ParseFontFace(data, 30); err != nil {
68+
ctx.SendChain(message.Text("ERROR: ", err))
69+
return
70+
}
71+
//取最长段
72+
txt = ""
73+
for _, v := range strlist {
74+
if len([]rune(v)) > len([]rune(txt)) {
75+
txt = v
76+
}
77+
}
78+
w, h := testcov.MeasureString(txt)
79+
for i := 0; i < 10; i++ {
80+
cov := gg.NewContext(int(w+float64(len([]rune(txt)))*nilx)+40, int(h+nily)*len(strlist)+30)
81+
cov.SetRGB(1, 1, 1)
82+
cov.Clear()
83+
if err = cov.ParseFontFace(data, 30); err != nil {
84+
ctx.SendChain(message.Text("ERROR: ", err))
85+
return
86+
}
87+
cov.SetColor(color.NRGBA{R: 0, G: 0, B: 0, A: 127})
88+
for k, v := range strlist {
89+
for kk, vv := range []rune(v) {
90+
x, y := cov.MeasureString(string([]rune(v)[:kk]))
91+
cov.DrawString(string(vv), x+float64(rand.Intn(5))+10+nilx, y+float64(rand.Intn(5))+15+float64(k)*(y+nily))
92+
}
93+
}
94+
s = append(s, imgfactory.Size(cov.Image(), 0, 0).Image())
95+
}
96+
var buf bytes.Buffer
97+
_ = gif.EncodeAll(&buf, imgfactory.MergeGif(5, s))
98+
ctx.SendChain(message.ImageBytes(buf.Bytes()))
4499
})
45100
}

0 commit comments

Comments
 (0)