-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.go
323 lines (299 loc) · 6.92 KB
/
app.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package main
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"sync"
"wordcloud/embedded"
"github.com/elazarl/go-bindata-assetfs"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
type (
Text struct {
Text string `json:"text"`
Size float64 `json:"size"`
Color string `json:"color"`
ColorValue color.Color `json:"-"`
}
NetMsg struct {
Err string `json:"err,omitempty"`
Data []byte `json:"data,omitempty"`
}
NetParams struct {
Content []*Text `json:"content"`
Width int32 `json:"width"`
Height int32 `json:"height"`
Color string `json:"color"`
}
point struct {
x int32
y int32
}
)
func (t *Text) ParseColor() {
// color #232323
if len(t.Color) == 7 {
r, _ := strconv.ParseInt(t.Color[1:3], 10, 8)
g, _ := strconv.ParseInt(t.Color[3:5], 10, 8)
b, _ := strconv.ParseInt(t.Color[5:7], 10, 8)
t.ColorValue = color.RGBA{A: 255, R: uint8(r), G: uint8(g), B: uint8(b)}
} else {
t.ColorValue = color.RGBA{A: 255}
}
}
func (msg *NetMsg) SendTo(w http.ResponseWriter) {
dat, e := json.Marshal(msg)
if e != nil {
log.Println(e.Error())
return
}
w.Write(dat)
if e != nil {
log.Println(e.Error())
}
}
// generate 接收客户端请求解析参数并生成结果
func generate(w http.ResponseWriter, req *http.Request) {
concurrent <- struct{}{}
defer func() {
<-concurrent
}()
log.Println("start")
result := &NetMsg{}
defer req.Body.Close()
defer result.SendTo(w)
payload, _ := ioutil.ReadAll(req.Body)
var params NetParams
if e := json.Unmarshal(payload, ¶ms); e != nil {
log.Println(e)
result.Err = e.Error()
return
}
if params.Height == 0 || params.Width == 0 {
e := errors.New("width and height cannot be 0")
log.Println(e)
result.Err = e.Error()
return
}
bg := image.White
rgba := image.NewRGBA(image.Rect(0, 0, int(params.Width), int(params.Height)))
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
ctx := freetype.NewContext()
ctx.SetSrc(image.Black)
ctx.SetDst(rgba)
ctx.SetDPI(defaultDpi)
ctx.SetClip(rgba.Bounds())
ctx.SetFont(fnt)
bgColor := colorSum(bg.C)
for i, t := range params.Content {
log.Println(t.Color, t.Text, t.Size)
size := int(t.Size)
ctx.SetFontSize(t.Size)
t.ParseColor()
ctx.SetSrc(image.NewUniform(t.ColorValue))
txtSize := measure(defaultDpi, t.Size, t.Text, fnt)
topX, topY := queryIntegralImage(rgba, txtSize.Round(), size, bgColor, qualityNormal)
if topX < 0 || topY < 0 {
log.Printf("no room left, %d of %d worlds finished", i, len(params.Content))
break
}
log.Println(topX, topY)
// baseline start point of the text
p := freetype.Pt(int(topX), int(topY)+int(t.Size*3/4))
_, e := ctx.DrawString(t.Text, p)
if e != nil {
log.Println(e.Error())
}
}
f, e := os.Create("tmp.png")
if e != nil {
return
}
png.Encode(f, rgba)
f.Close()
var b bytes.Buffer
writer := bufio.NewWriter(&b)
if e := png.Encode(writer, rgba); e != nil {
log.Println(e)
result.Err = e.Error()
return
}
if e := writer.Flush(); e != nil {
log.Println(e)
result.Err = e.Error()
return
}
result.Data = b.Bytes()
log.Println("done")
}
// parseFont 用于将字体文件解析为字体
func parseFont(ttfPath string) (*truetype.Font, error) {
//f, e := os.Open(ttfPath)
//if e != nil {
// return nil, e
//}
//defer f.Close()
dat, e := embedded.Asset(ttfPath)
if e != nil {
return nil, e
}
fnt, e := freetype.ParseFont(dat)
if e != nil {
return nil, e
}
return fnt, nil
}
var (
fnt *truetype.Font
concurrent chan struct{}
)
const (
defaultDpi = 72
qualityLow = 20
qualityNormal = 10
qualityHigh = 5
)
//go:generate go-bindata -o embedded/bindata.go -pkg embedded -nomemcopy asset/...
func main() {
fontPath := "asset/wqy-microhei.ttc"
var e error
fnt, e = parseFont(fontPath)
if e != nil {
log.Fatalln(e)
}
concurrent = make(chan struct{}, 1)
var wg sync.WaitGroup
wg.Add(1)
d := flag.Bool("debug", false, "Debug mode use html/js/css files, or use embedded data")
flag.Parse()
mux := http.DefaultServeMux
log.Println("Debug:", *d)
if *d {
// 调试模式下,webUI采用外部文件
mux.Handle("/", http.FileServer(http.Dir(".")))
} else {
// 费调试模式下,使用内嵌的数据
files := &assetfs.AssetFS{
Asset: embedded.Asset,
AssetDir: embedded.AssetDir,
AssetInfo: embedded.AssetInfo,
Prefix: "",
}
mux.Handle("/", http.FileServer(files))
}
go func() {
mux.HandleFunc("/cloud", generate)
e := http.ListenAndServe(":8765", mux)
if e != nil {
log.Println(e)
}
wg.Done()
}()
e = OpenURLWithBrowser("http://localhost:8765/asset")
if e != nil {
log.Println(e)
}
wg.Wait()
}
// measure 用于计算文本宽度
func measure(dpi, size float64, txt string, fnt *truetype.Font) fixed.Int26_6 {
opt := &truetype.Options{
DPI: dpi,
Size: size,
}
face := truetype.NewFace(fnt, opt)
return font.MeasureString(face, txt)
}
// todo
//
// originImg.At(x,y) == dscImg.At(x,y)
//
// colorSame(c1,c2 color.Color) bool{
// return c1.r == c2.r &&
// c1.g == c2.g &&
// c1.b == c2.b &&
// c1.a == c2.a
// }
func colorSum(p color.Color) uint32 {
r, g, b, a := p.RGBA()
return r + g + b + a
}
// queryIntegralImage 查找符合(sizeX, sizeY)的空白区域,并随机取其一
// 返回随机到的空白区域左上角的坐标,(-1,-1)表示未找到符合条件的
func queryIntegralImage(img image.Image, sizeX, sizeY int, bgColor uint32, quality int) (lTopX, lTopY int32) {
if quality < qualityHigh {
quality = qualityHigh
}
size := img.Bounds().Size()
foldX := size.X - sizeX
foldY := size.Y - sizeY
points := make([]point, 0)
// count how many possible locations
for i := 0; i < foldX; i++ {
for j := 0; j < foldY; j++ {
// Rectangle:
//
// i,j i+sizeX,j
//
// i,j+sizeY i+sizeX,j+sizeY
//
blank := true
for x := i + sizeX; x >= i; x -= quality {
for y := j + sizeY; y >= j; y -= quality {
if colorSum(img.At(x, y)) != bgColor {
blank = false
break
}
}
if !blank {
break
}
}
if !blank {
continue
}
points = append(points, point{int32(i), int32(j)})
}
}
if len(points) == 0 {
// no room left
return -1, -1
}
// pick a location at random
goal := rand.Int31n(int32(len(points)))
p := points[goal]
return p.x, p.y
}
var commands = map[string]string{
"windows": "cmd /c start",
"darwin": "open",
"linux": "xdg-open",
}
// OpenURLWithBrowser 调用系统浏览器打开指定URI,
// 目前支持Linux、Darwin、Windows三种平台
func OpenURLWithBrowser(uri string) error {
run, ok := commands[runtime.GOOS]
if !ok {
return errors.New(fmt.Sprintf("opening browser on %s unsupported, pls do it manually", runtime.GOOS))
}
cmd := exec.Command(run, uri)
return cmd.Start()
}