Skip to content

Commit

Permalink
🎨 ⚡️ 优化 txt2img
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Jan 13, 2022
1 parent 4546c2c commit 85876c8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.DS_Store
2 changes: 1 addition & 1 deletion control/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func init() {
text += msg + "\n\n"
return true
})
data, err := txt2img.RenderToBase64(text, 40, 20)
data, err := txt2img.RenderToBase64(text, txt2img.FontFile, 40, 20)
if err != nil {
log.Errorf("[control] %v", err)
}
Expand Down
61 changes: 31 additions & 30 deletions txt2img/txt2img.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/fogleman/gg"
"github.com/mattn/go-runewidth"
log "github.com/sirupsen/logrus"

"github.com/FloatTech/zbputils/binary"
Expand All @@ -18,13 +17,14 @@ import (
)

const (
whitespace = "\t\n\r\x0b\x0c"
// FontPath 通用字体路径
FontPath = "data/Font/"
// FontFile 苹方字体
FontFile = FontPath + "regular.ttf"
// BoldFontFile 粗体苹方字体
BoldFontFile = FontPath + "regular-bold.ttf"
// SakuraFontFile ...
SakuraFontFile = FontPath + "sakura.ttf"
)

// 加载数据库
Expand All @@ -42,54 +42,56 @@ type TxtCanvas struct {
}

// RenderToBase64 文字转base64
func RenderToBase64(text string, width, fontSize int) (base64Bytes []byte, err error) {
txtc, err := Render(text, width, fontSize)
func RenderToBase64(text, font string, width, fontSize int) (base64Bytes []byte, err error) {
txtc, err := Render(text, font, width, fontSize)
if err != nil {
log.Println("[txt2img]:", err)
log.Println("[txt2img]", err)
return nil, err
}
base64Bytes, err = txtc.ToBase64()
if err != nil {
log.Println("[txt2img]:", err)
log.Println("[txt2img]", err)
return nil, err
}
return
}

// Render 文字转图片
func Render(text string, width, fontSize int) (txtc TxtCanvas, err error) {
// Render 文字转图片 width 是图片宽度
func Render(text, font string, width, fontSize int) (txtc TxtCanvas, err error) {
txtc.Canvas = gg.NewContext(width, fontSize) // fake
if err = txtc.Canvas.LoadFontFace(font, float64(fontSize)); err != nil {
log.Errorln("[txt2img]", err)
return
}

buff := make([]string, 0)
line := ""
count := 0
for _, v := range text {
c := string(v)
if strings.Contains(whitespace, c) {
buff = append(buff, strings.TrimSpace(line))
count = 0
line = ""
continue
}
if count <= width {
line += c
count += runewidth.StringWidth(c)
} else {
buff = append(buff, line)
line = c
count = runewidth.StringWidth(c)
for _, s := range strings.Split(text, "\n") {
line := ""
for _, v := range s {
length, _ := txtc.Canvas.MeasureString(line)
if int(length) <= width {
line += string(v)
} else {
buff = append(buff, line)
line = string(v)
}
}
buff = append(buff, line)
}

txtc.Canvas = gg.NewContext((fontSize+4)*width/2, (len(buff)+2)*fontSize)
_, h := txtc.Canvas.MeasureString("好")
txtc.Canvas = gg.NewContext(width+int(h*2+0.5), int(float64(len(buff)*3+1)/2*h+0.5))
txtc.Canvas.SetRGB(1, 1, 1)
txtc.Canvas.Clear()
txtc.Canvas.SetRGB(0, 0, 0)
if err = txtc.Canvas.LoadFontFace(FontFile, float64(fontSize)); err != nil {
log.Errorln("[txt2img]:", err)
if err = txtc.Canvas.LoadFontFace(font, float64(fontSize)); err != nil {
log.Errorln("[txt2img]", err)
return
}

for i, v := range buff {
if v != "" {
txtc.Canvas.DrawString(v, float64(width/2), float64((i+2)*fontSize))
txtc.Canvas.DrawString(v, float64(width)*0.01, float64((i+1)*3)/2*h)
}
}
return
Expand Down Expand Up @@ -119,7 +121,6 @@ func (txtc TxtCanvas) ToBytes() (data []byte, cl func()) {
}

// WriteTo gg内容写入 Writer
// 使用完 data 后必须调用 cl 放回缓冲区
func (txtc TxtCanvas) WriteTo(f io.Writer) (n int64, err error) {
data, cl := txtc.ToBytes()
defer cl()
Expand Down

0 comments on commit 85876c8

Please sign in to comment.