-
Notifications
You must be signed in to change notification settings - Fork 2
/
avatar_initials.go
199 lines (154 loc) · 5.08 KB
/
avatar_initials.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
package avatar
import (
"errors"
"image"
"image/color"
"image/draw"
"io/ioutil"
"regexp"
"strings"
"unicode"
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
type InitialsOptions struct {
BgColor color.Color
Size int
FontPath string
TextColor color.Color
NInitials int
}
type Initials struct {
source []byte
options *InitialsOptions
originalImage image.Image
squareImage image.Image
circleImage image.Image
}
func (i Initials) Source() []byte {
return i.source
}
func (i Initials) loadOriginalImage() (image.Image, error) {
text := i.source
nInitials := i.options.nInitials()
if nInitials > 0 {
text = getInitials(text, nInitials)
}
size := i.options.size() * 3 // 3 times bigger for better quality
// Draw background img
imgRect := image.Rect(0, 0, size, size)
dst := image.NewRGBA(imgRect)
draw.Draw(
dst,
dst.Bounds(),
image.NewUniform(i.options.bgColor()),
image.ZP,
draw.Src)
ftFont, err := i.options.font()
if err != nil {
return nil, err
}
fontFace := truetype.NewFace(ftFont, &truetype.Options{
Size: getFontSizeThatFits(text, float64(size), ftFont),
})
fd := font.Drawer{
Dst: dst,
Src: image.NewUniform(i.options.textColor()),
Face: fontFace,
}
// Figure out baseline and adv for string in img
txtWidth := fd.MeasureBytes(text)
txtWidthInt := int(txtWidth >> 6)
bounds, _ := fd.BoundBytes(text)
txtHeight := bounds.Max.Y - bounds.Min.Y
txtHeightInt := int(txtHeight >> 6)
advLine := (size / 2) - (txtWidthInt / 2)
baseline := (size / 2) + (txtHeightInt / 2)
fd.Dot = fixed.Point26_6{X: fixed.Int26_6(advLine << 6), Y: fixed.Int26_6(baseline << 6)}
fd.DrawBytes(text)
return dst, nil
}
func (i Initials) originalImg() image.Image {
return i.originalImage
}
// Generates the square avatar
// It returns the avatar image in []byte or an error something went wrong
func (i Initials) Square() ([]byte, error) {
return square(i, i.options)
}
func (i Initials) Circle() ([]byte, error) {
return circle(i, i.options)
}
func (o InitialsOptions) bgColor() color.Color {
return bgColor(o.BgColor)
}
func (o InitialsOptions) size() int {
return size(o.Size)
}
func (o InitialsOptions) nInitials() int {
if o.NInitials == 0 {
return defaultNInitials
}
return o.NInitials
}
func (o InitialsOptions) font() (*truetype.Font, error) {
if strings.TrimSpace(o.FontPath) == "" {
return nil, errors.New("No font path given")
}
fontBytes, err := ioutil.ReadFile(o.FontPath)
if err != nil {
return nil, err
}
return freetype.ParseFont(fontBytes)
}
func (o InitialsOptions) textColor() color.Color {
if o.TextColor == nil {
return defaultTxtColor
}
return o.TextColor
}
func getFontSizeThatFits(text []byte, imgWidth float64, ftFont *truetype.Font) float64 {
fontSize := float64(100)
drawer := font.Drawer{
Face: truetype.NewFace(ftFont, &truetype.Options{
Size: fontSize,
}),
}
tw := float64(drawer.MeasureBytes(text) >> 6)
ratio := fontSize / tw
return ratio * (imgWidth - (40./100)*imgWidth)
}
func getInitials(text []byte, nChars int) []byte {
if len(text) == 0 {
return []byte("")
}
var initials = []byte{}
var previous = byte(' ')
regEmail := regexp.MustCompile("^(((([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7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F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$")
skipFromAt := regEmail.Match(text)
for _, ch := range text[:] {
if skipFromAt == true && rune(ch) == '@' {
break
}
if isSymbol(rune(ch)) {
previous = ch
continue
}
if ((unicode.IsUpper(rune(ch)) && unicode.IsLower(rune(previous))) || (unicode.IsLower(rune(ch)) && len(initials) == 0)) || isSymbol(rune(previous)) {
initials = append(initials, ch)
previous = ch
}
}
for i := len(initials); i < nChars && len(text) > i; i++ {
if isSymbol(rune(text[i])) {
continue
}
initials = append(initials, text[i])
}
return initials
}
func isSymbol(ch rune) bool {
return unicode.IsSymbol(ch) || unicode.IsSpace(ch) || unicode.IsPunct(ch)
}