-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.go
More file actions
176 lines (161 loc) · 4.23 KB
/
text.go
File metadata and controls
176 lines (161 loc) · 4.23 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package image
import (
"bufio"
"image"
"image/color"
"image/draw"
"image/png"
"io"
"math"
"net/http"
"os"
"strings"
"sync"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
)
func MergeTextImageServeHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
// figure out the Content-Length of this new combined image somehow
// w.Header().Set("Content-Length", fmt.Sprint(pngImage.ContentLength))
textContent := "coscms.com"
pngBgImgPath := "data/fonts/background.png"
img, err := TextToImage(textContent, "data/fonts/Courier New.ttf", pngBgImgPath)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
b := bufio.NewWriter(w)
png.Encode(b, img)
}
// TextToImage textContent string, fontFile string, pngBgImgPath string, width int, height int
func TextToImage(textContent string, fontFile string, args ...interface{}) (*image.RGBA, error) {
var c, textLayer *image.RGBA
rlen := len(args)
var width, height int
opt := NewTextImageOptions()
opt.Text = textContent
opt.FontFile = fontFile
opt.Width = width
opt.Height = height
var err error
if rlen > 0 {
pngBgImgPath := args[0].(string)
var img image.Image
if len(pngBgImgPath) > 0 {
pngFile, err := os.Open(pngBgImgPath)
if err != nil {
return nil, err
}
defer pngFile.Close()
img, err = png.Decode(pngFile)
if err != nil {
return nil, err
}
width = img.Bounds().Max.X
height = img.Bounds().Max.Y
}
if rlen > 1 {
width = args[1].(int)
}
if rlen > 2 {
height = args[2].(int)
}
opt.Width = width
opt.Height = height
textLayer, err = TextImage(opt)
if err != nil {
return nil, err
}
width = textLayer.Bounds().Max.X
height = textLayer.Bounds().Max.Y
c = image.NewRGBA(image.Rect(0, 0, width, height))
if len(pngBgImgPath) > 0 {
// draw bottom layer from file
draw.Draw(c, c.Bounds(), img, image.Point{0, 0}, draw.Src)
}
} else {
textLayer, err = TextImage(opt)
if err != nil {
return nil, err
}
width = textLayer.Bounds().Max.X
height = textLayer.Bounds().Max.Y
c = image.NewRGBA(image.Rect(0, 0, width, height))
}
// draw text layer on top
draw.Draw(c, c.Bounds(), textLayer, image.Point{0, 0}, draw.Over)
return c, nil
}
func NewTextImageOptions() *TextImageOptions {
return &TextImageOptions{
FontSize: 8,
PosX: 10,
PosY: 10,
Spacing: 1.5,
DPI: 300,
ForegroundColor: color.White,
BackgroundColor: color.Transparent,
}
}
type TextImageOptions struct {
Text string
FontFile string
FontSize float64
Width int
Height int
PosX int
PosY int
Spacing float64
DPI float64
ForegroundColor color.Color // 前景色
BackgroundColor color.Color // 背景色
}
var fontCache = sync.Map{}
func TextImage(opt *TextImageOptions) (*image.RGBA, error) {
lines := strings.Split(opt.Text, "\n")
// read font
var font *truetype.Font
if v, ok := fontCache.Load(opt.FontFile); ok {
font = v.(*truetype.Font)
} else {
fontBytes, err := os.ReadFile(opt.FontFile)
if err != nil {
return nil, err
}
font, err = freetype.ParseFont(fontBytes)
if err != nil {
return nil, err
}
fontCache.Store(opt.FontFile, font)
}
c := freetype.NewContext()
c.SetDPI(opt.DPI)
c.SetFont(font)
c.SetFontSize(opt.FontSize)
// Initialize the context.
fg, bg := image.NewUniform(opt.ForegroundColor), image.NewUniform(opt.BackgroundColor)
if opt.Width <= 0 {
s := float64(c.PointToFixed(opt.FontSize) >> 8)
opt.Width = int(math.Ceil((s-float64(s)/2.5)*float64(len(opt.Text)) + float64(opt.PosX)*2))
}
if opt.Height <= 0 {
opt.Height = len(lines)*int(c.PointToFixed(opt.FontSize*opt.Spacing)>>8) + opt.PosY*2
}
rgba := image.NewRGBA(image.Rect(0, 0, opt.Width, opt.Height))
draw.Draw(rgba, rgba.Bounds(), bg, image.Point{}, draw.Src)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(fg)
// Draw the text
pt := freetype.Pt(opt.PosX, opt.PosY+int(c.PointToFixed(opt.FontSize)>>8))
for _, s := range lines {
_, err := c.DrawString(s, pt)
if err != nil {
return nil, err
}
pt.Y += c.PointToFixed(opt.FontSize * opt.Spacing)
}
return rgba, nil
}