-
Notifications
You must be signed in to change notification settings - Fork 1
/
avatarbuilder.go
181 lines (149 loc) · 4.17 KB
/
avatarbuilder.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
package avatarbuilderMod
/*
We would like to express our gratitude to ShiningRush (https://github.com/ShiningRush) for the font. We borrowed their code and modified it to create an SVG without saving it to disk.
Mod https://github.com/ShiningRush/avatarbuilder
*/
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"os"
"strconv"
"github.com/golang/freetype"
"golang.org/x/image/font"
)
type FontCenterCalculator interface {
// CalculateCenterLocation used to calculate center location in different font style
CalculateCenterLocation(string, *AvatarBuilder) (int, int)
}
type AvatarBuilder struct {
W int
H int
fontFile string
fontsize float64
bg color.Color
fg color.Color
ctx *freetype.Context
calc FontCenterCalculator
}
func NewAvatarBuilder(fontFile string, calc FontCenterCalculator) *AvatarBuilder {
ab := &AvatarBuilder{}
ab.fontFile = fontFile
ab.bg, ab.fg = color.White, color.Black
ab.W, ab.H = 200, 200
ab.fontsize = 95
ab.calc = calc
return ab
}
func (ab *AvatarBuilder) SetFrontgroundColor(c color.Color) {
ab.fg = c
}
func (ab *AvatarBuilder) SetBackgroundColor(c color.Color) {
ab.bg = c
}
func (ab *AvatarBuilder) SetFrontgroundColorHex(hex uint32) {
ab.fg = ab.hexToRGBA(hex)
}
func (ab *AvatarBuilder) SetBackgroundColorHex(hex uint32) {
ab.bg = ab.hexToRGBA(hex)
}
func (ab *AvatarBuilder) SetFontSize(size float64) {
ab.fontsize = size
}
func (ab *AvatarBuilder) SetAvatarSize(w int, h int) {
ab.W = w
ab.H = h
}
func (ab *AvatarBuilder) GenerateImageAndSavePNG(s string, outname string) error {
bs, err := ab.GenerateImage(s)
if err != nil {
return err
}
// Save that RGBA image to disk.
outFile, err := os.Create(outname)
if err != nil {
return fmt.Errorf("create file: %w", err)
}
defer outFile.Close()
b := bufio.NewWriter(outFile)
if _, err := b.Write(bs); err != nil {
return fmt.Errorf("write bytes to file: %w", err)
}
if err = b.Flush(); err != nil {
return fmt.Errorf("flush image: %w", err)
}
return nil
}
func (ab *AvatarBuilder) GenerateImageAndSave(s string) (string, error) {
bs, err := ab.GenerateImage(s)
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
buf.WriteString(`<svg xmlns="http://www.w3.org/2000/svg" width="` + strconv.Itoa(ab.W) + `" height="` + strconv.Itoa(ab.H) + `">`)
buf.WriteString(`<image width="` + strconv.Itoa(ab.W) + `" height="` + strconv.Itoa(ab.H) + `" xlink:href="data:image/png;base64,` + base64.StdEncoding.EncodeToString(bs) + `" xmlns:xlink="http://www.w3.org/1999/xlink" />`)
buf.WriteString(`</svg>`)
return buf.String(), nil
}
func (ab *AvatarBuilder) GenerateImage(s string) ([]byte, error) {
rgba := ab.buildColorImage()
if ab.ctx == nil {
if err := ab.buildDrawContext(rgba); err != nil {
return nil, err
}
}
x, y := ab.calc.CalculateCenterLocation(s, ab)
pt := freetype.Pt(x, y)
if _, err := ab.ctx.DrawString(s, pt); err != nil {
return nil, fmt.Errorf("draw string: %w", err)
}
buf := &bytes.Buffer{}
if err := png.Encode(buf, rgba); err != nil {
return nil, fmt.Errorf("png encode: %w", err)
}
return buf.Bytes(), nil
}
func (ab *AvatarBuilder) buildColorImage() *image.RGBA {
bg := image.NewUniform(ab.bg)
rgba := image.NewRGBA(image.Rect(0, 0, ab.W, ab.H))
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
return rgba
}
func (ab *AvatarBuilder) hexToRGBA(h uint32) *color.RGBA {
rgba := &color.RGBA{
R: uint8(h >> 16),
G: uint8((h & 0x00ff00) >> 8),
B: uint8(h & 0x0000ff),
A: 255,
}
return rgba
}
func (ab *AvatarBuilder) buildDrawContext(rgba *image.RGBA) error {
// Read the font data.
fontBytes, err := os.ReadFile(ab.fontFile)
if err != nil {
return fmt.Errorf("error when open font file: %w", err)
}
f, err := freetype.ParseFont(fontBytes)
if err != nil {
return fmt.Errorf("error when parse font file: %w", err)
}
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(f)
c.SetFontSize(ab.fontsize)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(image.NewUniform(ab.fg))
c.SetHinting(font.HintingNone)
ab.ctx = c
return nil
}
func (ab *AvatarBuilder) GetFontWidth() int {
return int(ab.ctx.PointToFixed(ab.fontsize) >> 6)
}