-
Notifications
You must be signed in to change notification settings - Fork 2
/
card.go
108 lines (104 loc) · 2.59 KB
/
card.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Package rendercard 渲染卡片
package rendercard
import (
"errors"
"image"
"math/rand"
"github.com/FloatTech/gg"
)
var (
// ErrNilTextFont ...
ErrNilTextFont = errors.New("nil TextFont")
// ErrNilTitleFont ...
ErrNilTitleFont = errors.New("nil TitleFont")
)
// DrawTextCard 绘制文字卡片
func (c *Card) DrawTextCard() (imgForCard image.Image, err error) {
width := c.Width
if width == 0 {
width = 600
}
if c.TextFontData == nil {
return nil, ErrNilTextFont
}
// 正文数据
texts, err := Truncate(c.TextFontData, c.Text, float64(width)-80, 38)
if err != nil {
return
}
textHigh := float64(len(texts)+1) * 38 * 72 / 96 * 1.5
// 计算图片高度
imgHigh := c.Height
if imgHigh == 0 {
if c.CanTitleShown {
imgHigh = 30 + 100 + int(textHigh) + 20
} else {
imgHigh = 20 + int(textHigh) + 20
}
}
// 创建画布
canvas := gg.NewContext(width, imgHigh)
// 随机背景色
if c.BackgroundImage == "" {
canvas.DrawRectangle(0, 0, float64(width), float64(imgHigh))
canvas.SetRGBA255(rand.Intn(45)+165, rand.Intn(45)+165, rand.Intn(45)+165, 255)
canvas.Fill()
} else {
banner, err := gg.LoadImage(c.BackgroundImage)
if err == nil {
if float64(banner.Bounds().Dy())/float64(banner.Bounds().Dx()) < float64(canvas.H())/float64(canvas.W()) {
sc := float64(canvas.H()) / float64(banner.Bounds().Dy())
canvas.ScaleAbout(sc, sc, float64(canvas.W())/2, float64(canvas.H())/2)
canvas.DrawImageAnchored(banner, canvas.W()/2, canvas.H()/2, 0.5, 0.5)
} else {
sc := float64(canvas.W()) / float64(banner.Bounds().Dx())
canvas.Scale(sc, sc)
canvas.DrawImage(banner, 0, 0)
}
canvas.Identity()
}
}
y := 0.0
// 标题
if c.CanTitleShown {
if c.TitleFontData == nil {
return nil, ErrNilTitleFont
}
err = canvas.ParseFontFace(c.TitleFontData, 103)
if err != nil {
return
}
canvas.SetRGB(0, 0, 0)
titleDx := 10.0
widthOfTilte, titleDy := canvas.MeasureString(c.Title)
switch c.TitleAlign {
case NilAlign:
case AlignLeft:
case AlignCenter:
titleDx = (float64(width) - widthOfTilte) / 2
case AlignRight:
titleDx = float64(width) - widthOfTilte
}
canvas.DrawString(c.Title, titleDx, titleDy+10)
// 画横线
canvas.DrawRoundedRectangle(10, 115, 580, 10, 2.5)
canvas.SetRGB(0, 0, 0)
canvas.Fill()
// 内容
y = 130 + 38*72/96*1.5
} else {
// 内容
y = 20 + 38*72/96*1.5
}
err = canvas.ParseFontFace(c.TextFontData, 38)
if err != nil {
return
}
for _, s := range texts {
canvas.DrawStringAnchored(s, 10, y, 0, 0.5)
y += canvas.FontHeight() * 1.5
}
// 制图
imgForCard = canvas.Image()
return
}