This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
153 lines (127 loc) · 3.62 KB
/
helper.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
package image
import (
"image"
"image/color"
"net/http"
"strings"
"github.com/disintegration/imaging"
"github.com/fogleman/gg"
)
func (i *Image) getImageFromURL(url string) (image.Image, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, err
}
res, _, err := image.Decode(resp.Body)
if err != nil {
return nil, err
}
return res, nil
}
func (i *Image) drawBackground(dc *gg.Context, url string) error {
if url == "" {
// Draw default background.
i.drawDefaultBackground(dc)
return nil
}
bg, err := i.getImageFromURL(url)
if err != nil {
return err
}
// Resize.
bg = imaging.Fill(bg, i.width, i.height, imaging.Center, imaging.Lanczos)
bg = imaging.Blur(bg, 10)
dc.DrawImage(bg, 0, 0)
return nil
}
func (i *Image) drawDefaultBackground(dc *gg.Context) {
dc.SetColor(color.White)
dc.DrawRectangle(0, 0, float64(dc.Width()), float64(dc.Height()))
dc.Fill()
}
func (i *Image) drawOverlay(dc *gg.Context) {
w := float64(dc.Width()) - (2.0 * i.margin)
h := float64(dc.Height()) - (2.0 * i.margin)
dc.SetColor(color.RGBA{0, 0, 0, 204})
dc.DrawRectangle(i.margin, i.margin, w, h)
dc.Fill()
}
func (i *Image) drawTitle(dc *gg.Context, title, enTitle string) {
dc.SetColor(color.White)
dc.SetFontFace(i.title.face)
title = strings.Join(dc.WordWrap(title, i.title.maxWidth), "\n")
// Resize font.
fontSize := i.title.size
w, h := dc.MeasureMultilineString(title, 1.25)
for h > i.title.maxHeight {
fontSize--
f, _ := gg.LoadFontFace(i.fontPath, fontSize)
dc.SetFontFace(f)
w, h = dc.MeasureMultilineString(title, 1.25)
}
x := 2 * i.margin
y := 2*i.margin + (i.title.maxHeight-h)/2.0
dc.DrawStringWrapped(title, x, y, 0, 0, i.title.maxWidth, 1.25, gg.AlignCenter)
// Line.
dc.DrawLine(
2*i.margin+(i.enTitle.maxWidth-w)/2,
y+h+i.margin,
2*i.margin+(i.enTitle.maxWidth-w)/2+w,
y+h+i.margin,
)
dc.SetRGBA(1, 1, 1, 0.5)
dc.Stroke()
if enTitle == "" {
return
}
// English title.
dc.SetColor(color.RGBA{255, 255, 255, 127})
dc.SetFontFace(i.enTitle.face)
// Resize font.
fontSize2 := i.enTitle.size
w2, _ := dc.MeasureString(enTitle)
for w2 > w {
fontSize2--
f, _ := gg.LoadFontFace(i.fontPath, fontSize2)
dc.SetFontFace(f)
w2, _ = dc.MeasureString(enTitle)
}
x2 := 2 * i.margin
y2 := y + h + i.margin
dc.DrawStringWrapped(enTitle, x2, y2, 0, 0, i.enTitle.maxWidth, 1, gg.AlignCenter)
}
func (i *Image) drawStats(dc *gg.Context, order int, title, value string) {
dc.SetColor(color.RGBA{255, 255, 255, 127})
// Title.
dc.SetFontFace(i.subtitle.face)
x1 := (float64(order) * i.subtitle.maxWidth) + i.margin
y1 := float64(dc.Height()) - i.stats.maxHeight - i.subtitle.maxHeight - (i.margin * 4)
dc.DrawStringWrapped(title, x1, y1, 0, 0, i.subtitle.maxWidth, 1, gg.AlignCenter)
// Line.
dc.DrawLine(
2*i.margin+float64(order)*i.stats.maxWidth,
float64(dc.Height())-i.stats.maxHeight-(i.margin*3),
2*i.margin+float64(order)*i.stats.maxWidth+i.stats.maxWidth-2*i.margin,
float64(dc.Height())-i.stats.maxHeight-(i.margin*3),
)
dc.SetRGBA(1, 1, 1, 0.5)
dc.Stroke()
// Value.
dc.SetColor(color.White)
dc.SetFontFace(i.stats.face)
fontSize := i.stats.size
w, h := dc.MeasureString(value)
for w > i.stats.maxWidth-(2*i.margin) {
fontSize--
f, _ := gg.LoadFontFace(i.fontPath, fontSize)
dc.SetFontFace(f)
w, h = dc.MeasureString(value)
}
x2 := (float64(order) * i.stats.maxWidth) + (2 * i.margin)
y2 := float64(dc.Height()) - i.stats.maxHeight - (i.margin * 3) + (i.stats.maxHeight-h)/2
dc.DrawStringWrapped(value, x2, y2, 0, 0, i.stats.maxWidth-(2*i.margin), 1, gg.AlignCenter)
}