/
draw.go
86 lines (78 loc) · 1.76 KB
/
draw.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
package shot
import (
"bufio"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"math"
"net/http"
)
var (
size float64 = 26
smallSize float64 = 16
dpi float64 = 144
spacing float64 = 1.5
hinting font.Hinting = font.HintingNone
sas = "screenshot will be available soon"
)
func (s *Service) Draw(siteName string, w http.ResponseWriter) {
// Read the font data.
fontBytes := MustAsset("assets/luxisr.ttf")
f, err := truetype.Parse(fontBytes)
if err != nil {
log.Println(err)
return
}
// Draw the background and the guidelines.
fg, bg := image.Black, image.White
const imgW, imgH = 1200, 700
rgba := image.NewRGBA(image.Rect(0, 0, imgW, imgH))
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
d := &font.Drawer{
Dst: rgba,
Src: fg,
Face: truetype.NewFace(f, &truetype.Options{
Size: size,
DPI: dpi,
Hinting: hinting,
}),
}
y := (imgH / 2) + (int(math.Ceil(size*dpi/72)) / 2)
//dy := int(math.Ceil(size * spacing * dpi / 72))
d.Dot = fixed.Point26_6{
X: (fixed.I(imgW) - d.MeasureString(siteName)) / 2,
Y: fixed.I(y),
}
d.DrawString(siteName)
d = &font.Drawer{
Dst: rgba,
Src: image.NewUniform(color.Gray16{0xaaaa}),
Face: truetype.NewFace(f, &truetype.Options{
Size: smallSize,
DPI: dpi,
Hinting: hinting,
}),
}
dy := int(math.Ceil(smallSize * spacing * dpi / 72))
d.Dot = fixed.Point26_6{
X: (fixed.I(imgW) - d.MeasureString(sas)) / 2,
Y: fixed.I(y + dy),
}
d.DrawString(sas)
b := bufio.NewWriter(w)
err = png.Encode(b, rgba)
if err != nil {
log.Println(err)
return
}
err = b.Flush()
if err != nil {
log.Println(err)
return
}
}