2
2
package font
3
3
4
4
import (
5
+ "bytes"
6
+ "image"
7
+ "image/color"
8
+ "image/gif"
9
+ "math/rand"
10
+ "strings"
11
+
5
12
"github.com/FloatTech/floatbox/binary"
13
+ "github.com/FloatTech/floatbox/file"
14
+ "github.com/FloatTech/gg"
15
+ "github.com/FloatTech/imgfactory"
6
16
ctrl "github.com/FloatTech/zbpctrl"
7
17
"github.com/FloatTech/zbputils/control"
8
18
"github.com/FloatTech/zbputils/ctxext"
@@ -15,10 +25,10 @@ func init() {
15
25
control .Register ("font" , & ctrl.Options [* zero.Ctx ]{
16
26
DisableOnDefault : false ,
17
27
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 ) {
20
30
fnt := ctx .State ["regex_matched" ].([]string )[1 ]
21
- txt := ctx .State ["regex_matched" ].([]string )[2 ]
31
+ txt := ctx .State ["regex_matched" ].([]string )[3 ]
22
32
switch fnt {
23
33
case "用终末体" :
24
34
fnt = text .SyumatuFontFile
@@ -35,11 +45,56 @@ func init() {
35
45
default :
36
46
fnt = text .FontFile
37
47
}
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 )
39
61
if err != nil {
40
62
ctx .SendChain (message .Text ("ERROR: " , err ))
41
63
return
42
64
}
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 ()))
44
99
})
45
100
}
0 commit comments