-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdavatar.go
182 lines (154 loc) · 4.36 KB
/
mdavatar.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
/*
* Material-Design-Avatars
* https://github.com/lincanbin/Material-Design-Avatars
*
* Copyright 2015 Canbin Lin (lincanbin@hotmail.com)
* http://www.94cb.com/
*
* Licensed under the Apache License, Version 2.0:
* http://www.apache.org/licenses/LICENSE-2.0
*
* Create material deisgn avatars for users just like Gmail or Messager in Android.
*/
// Ref: https://blog.logrocket.com/working-with-go-images/
// Ref: https://github.com/zdhxiong/mdclub/tree/master/src/Vendor
package mdavatar
import (
"image"
"image/color"
"image/draw"
"io/ioutil"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"golang.org/x/image/math/fixed"
"github.com/laojianzi/mdavatar/style"
"github.com/laojianzi/mdavatar/util"
)
// Config on mdavatar build
type Config struct {
avatarText string
avatarTextHandle AvatarTextHandler
avatarSize int
padding int
letterFont string
letterFontContent []byte
asianFont string
asianFontContent []byte
enableAsianFontChar bool
colors []color.RGBA
background *image.RGBA
}
// AvatarTextHandler handle for avatar text
// allow custom !!!
// e.g:
// text: laojianzi => L
// text: 老健仔 => L
type AvatarTextHandler func(s string, enableAsianFontChar bool) string
// New return a mdavatar config (*Config)
func New(text string, opts ...Option) *Config {
defaultConfig := *DefaultConfig
config := &defaultConfig
for _, opt := range opts {
opt(config)
}
if config.asianFont != "" {
config.enableAsianFontChar = util.IsFile(config.asianFont)
}
config.avatarText = config.avatarTextHandle(text, config.enableAsianFontChar)
return config
}
// Build generate a image from config
func (config *Config) Build() (*image.RGBA, error) {
var err error
if config.background == nil {
if _, err = config.MakeBackground(); err != nil {
return nil, err
}
}
return config.drawText()
}
// Builds generate a image from config by a build style
func (config *Config) Builds(newStyle style.MDAvatarBuildStyleNewFunc) (*image.RGBA, error) {
img, err := config.Build()
if err != nil {
return nil, err
}
des := image.NewRGBA(img.Rect)
draw.DrawMask(des, des.Bounds(), img, image.Point{}, newStyle(img), image.Point{}, draw.Over)
return des, nil
}
// Background new a background image from Config.avatarSize and Config.colors
func (config *Config) MakeBackground() (*image.RGBA, error) {
bgColorIndex, err := util.MtRand(0, int64(len(config.colors)-1))
if err != nil {
return nil, err
}
bgColor := image.Uniform{C: config.colors[bgColorIndex]}
config.background = image.NewRGBA(image.Rect(0, 0, config.avatarSize, config.avatarSize))
draw.Draw(config.background, config.background.Bounds(), &image.Uniform{C: &bgColor},
image.Point{}, draw.Src)
return config.background, nil
}
func (config *Config) drawText() (*image.RGBA, error) {
var (
fgColor image.Image
fontSize = 128.0
err error
)
fgColor = image.White
fontContent, err := config.fontContent()
if err != nil {
return nil, err
}
fontFace, err := opentype.Parse(fontContent)
if err != nil {
return nil, err
}
face, err := opentype.NewFace(fontFace, &opentype.FaceOptions{
Size: fontSize,
DPI: 72.0,
Hinting: font.HintingFull,
})
if err != nil {
return nil, err
}
fontDrawer := &font.Drawer{
Dst: config.background,
Src: fgColor,
Face: face,
}
textBounds, _ := fontDrawer.BoundString(config.avatarText)
xPosition := (fixed.I(config.background.Rect.Max.X) - fontDrawer.MeasureString(config.avatarText)) / 2
textHeight := textBounds.Max.Y - textBounds.Min.Y
yPosition := fixed.I((config.background.Rect.Max.Y)-textHeight.Ceil())/2 + fixed.I(textHeight.Ceil())
fontDrawer.Dot = fixed.Point26_6{
X: xPosition,
Y: yPosition,
}
fontDrawer.DrawString(config.avatarText)
return config.background, err
}
func (config *Config) fontContent() ([]byte, error) {
var content []byte
var err error
if config.enableAsianFontChar {
if config.asianFont != "" {
content, err = ioutil.ReadFile(config.asianFont)
if err != nil {
return nil, err
}
config.asianFontContent = content
}
content = config.asianFontContent
} else {
if config.letterFont != "" {
content, err = ioutil.ReadFile(config.letterFont)
if err != nil {
return nil, err
}
config.letterFontContent = content
}
content = config.asianFontContent
}
return content, nil
}