-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbadge.go
291 lines (225 loc) · 14.7 KB
/
badge.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Package badge provides methods for generating SVG badges
package badge
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
)
// ////////////////////////////////////////////////////////////////////////////////// //
const (
COLOR_BLUE = "#007ec6"
COLOR_BRIGHTGREEN = "#4c1"
COLOR_GREEN = "#97ca00"
COLOR_GREY = "#555"
COLOR_LIGHTGREY = "#9f9f9f"
COLOR_ORANGE = "#fe7d37"
COLOR_RED = "#e05d44"
COLOR_YELLOW = "#dfb317"
COLOR_YELLOWGREEN = "#a4a61d"
COLOR_SUCCESS = "#4c1"
COLOR_IMPORTANT = "#fe7d37"
COLOR_CRITICAL = "#e05d44"
COLOR_INFORMATIONAL = "#007ec6"
COLOR_INACTIVE = "#9f9f9f"
)
const (
DEFAULT_OFFSET = 9 // default font offset
DEFAULT_SPACING = 0 // default letter spacing
)
// ////////////////////////////////////////////////////////////////////////////////// //
const _TEMPLATE_PLASTIC = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{WIDTH}" height="18" role="img" aria-label="{LABEL}: {MESSAGE}"><title>{LABEL}: {MESSAGE}</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="r"><rect width="{WIDTH}" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="{LABEL_WIDTH}" height="18" fill="#555"/><rect x="{LABEL_WIDTH}" width="{MESSAGE_WIDTH}" height="18" fill="{COLOR}"/><rect width="{WIDTH}" height="18" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="{FONT},Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="{FONT_SIZE}"><text aria-hidden="true" x="{LABEL_X}" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="{LABEL_LENGTH}">{LABEL}</text><text x="{LABEL_X}" y="130" transform="scale(.1)" fill="#fff" textLength="{LABEL_LENGTH}">{LABEL}</text><text aria-hidden="true" x="{MESSAGE_X}" y="140" fill="{MESSAGE_SHADOW}" fill-opacity=".3" transform="scale(.1)" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text><text x="{MESSAGE_X}" y="130" transform="scale(.1)" fill="{MESSAGE_COLOR}" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text></g></svg>`
const _TEMPLATE_FLAT = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{WIDTH}" height="20" role="img" aria-label="{LABEL}: {MESSAGE}"><title>{LABEL}: {MESSAGE}</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="{WIDTH}" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="{LABEL_WIDTH}" height="20" fill="#555"/><rect x="{LABEL_WIDTH}" width="{MESSAGE_WIDTH}" height="20" fill="{COLOR}"/><rect width="{WIDTH}" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="{FONT},Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="{FONT_SIZE}"><text aria-hidden="true" x="{LABEL_X}" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="{LABEL_LENGTH}">{LABEL}</text><text x="{LABEL_X}" y="140" transform="scale(.1)" fill="#fff" textLength="{LABEL_LENGTH}">{LABEL}</text><text aria-hidden="true" x="{MESSAGE_X}" y="150" fill="{MESSAGE_SHADOW}" fill-opacity=".3" transform="scale(.1)" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text><text x="{MESSAGE_X}" y="140" transform="scale(.1)" fill="{MESSAGE_COLOR}" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text></g></svg>`
const _TEMPLATE_FLAT_SQUARE = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{WIDTH}" height="20" role="img" aria-label="{LABEL}: {MESSAGE}"><title>{LABEL}: {MESSAGE}</title><g shape-rendering="crispEdges"><rect width="{LABEL_WIDTH}" height="20" fill="#555"/><rect x="{LABEL_WIDTH}" width="{MESSAGE_WIDTH}" height="20" fill="{COLOR}"/></g><g fill="#fff" text-anchor="middle" font-family="{FONT},Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="{FONT_SIZE}"><text x="{LABEL_X}" y="140" transform="scale(.1)" fill="#fff" textLength="{LABEL_LENGTH}">{LABEL}</text><text x="{MESSAGE_X}" y="140" transform="scale(.1)" fill="{MESSAGE_COLOR}" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text></g></svg>`
const _TEMPLATE_FLAT_SIMPLE = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{WIDTH}" height="20" role="img" aria-label="{MESSAGE}"><title>{MESSAGE}</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="{WIDTH}" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="0" height="20" fill="{COLOR}"/><rect x="0" width="{WIDTH}" height="20" fill="{COLOR}"/><rect width="{WIDTH}" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="{FONT},Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="{FONT_SIZE}"><text aria-hidden="true" x="{MESSAGE_X}" y="150" fill="{MESSAGE_SHADOW}" fill-opacity=".3" transform="scale(.1)" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text><text x="{MESSAGE_X}" y="140" transform="scale(.1)" fill="{MESSAGE_COLOR}" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text></g></svg>`
const _TEMPLATE_PLASTIC_SIMPLE = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{WIDTH}" height="18" role="img" aria-label="{MESSAGE}"><title>TESTTEST</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="r"><rect width="{WIDTH}" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="0" height="18" fill="{COLOR}"/><rect x="0" width="{WIDTH}" height="18" fill="{COLOR}"/><rect width="{WIDTH}" height="18" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="{FONT},Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="{FONT_SIZE}"><text aria-hidden="true" x="{MESSAGE_X}" y="140" fill="{MESSAGE_SHADOW}" fill-opacity=".3" transform="scale(.1)" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text><text x="{MESSAGE_X}" y="130" transform="scale(.1)" fill="{MESSAGE_COLOR}" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text></g></svg>`
const _TEMPLATE_FLAT_SQUARE_SIMPLE = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{WIDTH}" height="20" role="img" aria-label="{MESSAGE}"><title>{MESSAGE}</title><g shape-rendering="crispEdges"><rect width="0" height="20" fill="{COLOR}"/><rect x="0" width="{WIDTH}" height="20" fill="{COLOR}"/></g><g fill="#fff" text-anchor="middle" font-family="{FONT},Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="{FONT_SIZE}"><text x="{MESSAGE_X}" y="140" transform="scale(.1)" fill="{MESSAGE_COLOR}" textLength="{MESSAGE_LENGTH}">{MESSAGE}</text></g></svg>`
// ////////////////////////////////////////////////////////////////////////////////// //
// Generator is badge generator
type Generator struct {
Offset int // Text offset
Spacing float64 // Letter spacing
fontSize int
fontName string
drawer *font.Drawer
}
// ////////////////////////////////////////////////////////////////////////////////// //
// NewGenerator creates new badge generator using given path to font file
func NewGenerator(fontFile string, fontSize int) (*Generator, error) {
data, err := os.ReadFile(fontFile)
if err != nil {
return nil, fmt.Errorf("Can't read font data: %w", err)
}
return NewGeneratorFromBytes(data, fontSize)
}
// NewGeneratorFromReader creates a new card generator using a reader that provides font
// data
func NewGeneratorFromReader(r io.Reader, fontSize int) (*Generator, error) {
fontData, err := io.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("Can't read font data: %w", err)
}
return NewGeneratorFromBytes(fontData, fontSize)
}
// NewGeneratorFromBytes creates a new card generator using given font data
func NewGeneratorFromBytes(fontData []byte, fontSize int) (*Generator, error) {
fontTTF, err := truetype.Parse(fontData)
if err != nil {
return nil, err
}
return &Generator{
Offset: DEFAULT_OFFSET,
Spacing: DEFAULT_SPACING,
fontSize: fontSize,
fontName: fontTTF.Name(truetype.NameIDFontFullName),
drawer: &font.Drawer{
Face: truetype.NewFace(fontTTF, &truetype.Options{
Size: float64(fontSize),
DPI: 72,
Hinting: font.HintingFull,
}),
},
}, nil
}
// ////////////////////////////////////////////////////////////////////////////////// //
// GeneratePlastic generates SVG badge in plastic style
func (g *Generator) GeneratePlastic(label, message, color string) []byte {
if label == "" {
return g.generateBadgeSimple(_TEMPLATE_PLASTIC_SIMPLE, message, color)
}
return g.generateBadge(_TEMPLATE_PLASTIC, label, message, color)
}
// GenerateFlat generates SVG badge in flat style
func (g *Generator) GenerateFlat(label, message, color string) []byte {
if label == "" {
return g.generateBadgeSimple(_TEMPLATE_FLAT_SIMPLE, message, color)
}
return g.generateBadge(_TEMPLATE_FLAT, label, message, color)
}
// GenerateFlatSquare generates SVG badge in flat-square style
func (g *Generator) GenerateFlatSquare(label, message, color string) []byte {
if label == "" {
return g.generateBadgeSimple(_TEMPLATE_FLAT_SQUARE_SIMPLE, message, color)
}
return g.generateBadge(_TEMPLATE_FLAT_SQUARE, label, message, color)
}
// GeneratePlasticSimple generates SVG simple badge in plastic style
func (g *Generator) GeneratePlasticSimple(message, color string) []byte {
return g.generateBadgeSimple(_TEMPLATE_PLASTIC_SIMPLE, message, color)
}
// GenerateFlatSimple generates SVG simple badge in flat style
func (g *Generator) GenerateFlatSimple(message, color string) []byte {
return g.generateBadgeSimple(_TEMPLATE_FLAT_SIMPLE, message, color)
}
// GenerateFlatSquareSimple generates SVG simple badge in flat-square style
func (g *Generator) GenerateFlatSquareSimple(message, color string) []byte {
return g.generateBadgeSimple(_TEMPLATE_FLAT_SQUARE_SIMPLE, message, color)
}
// ////////////////////////////////////////////////////////////////////////////////// //
// generateBadge generates badge with given template
func (g *Generator) generateBadge(template, label, message, color string) []byte {
c := parseColor(color)
gF := float64(g.Offset)
lW := float64(g.drawer.MeasureString(label)>>6) + gF
mW := float64(g.drawer.MeasureString(message)>>6) + gF
fW := lW + mW
lX := (lW/2 + 1) * 10
mX := (lW + (mW / 2) - 1) * 10
lL := (lW - gF) * (10.0 + g.Spacing - 0.5)
mL := (mW - gF) * (10.0 + g.Spacing - 0.5)
fS := g.fontSize * 10
mC, mS := getMessageColors(c)
badge := strings.ReplaceAll(template, "{LABEL}", label)
badge = strings.ReplaceAll(badge, "{MESSAGE}", message)
badge = strings.ReplaceAll(badge, "{COLOR}", formatColor(c))
badge = strings.ReplaceAll(badge, "{WIDTH}", formatFloat(fW))
badge = strings.ReplaceAll(badge, "{LABEL_WIDTH}", formatFloat(lW))
badge = strings.ReplaceAll(badge, "{MESSAGE_WIDTH}", formatFloat(mW))
badge = strings.ReplaceAll(badge, "{LABEL_X}", formatFloat(lX))
badge = strings.ReplaceAll(badge, "{MESSAGE_X}", formatFloat(mX))
badge = strings.ReplaceAll(badge, "{LABEL_LENGTH}", formatFloat(lL))
badge = strings.ReplaceAll(badge, "{MESSAGE_LENGTH}", formatFloat(mL))
badge = strings.ReplaceAll(badge, "{MESSAGE_COLOR}", mC)
badge = strings.ReplaceAll(badge, "{MESSAGE_SHADOW}", mS)
badge = strings.ReplaceAll(badge, "{FONT}", g.fontName)
badge = strings.ReplaceAll(badge, "{FONT_SIZE}", strconv.Itoa(fS))
return []byte(badge)
}
// generateBadgeSimple generates badge with given template
func (g *Generator) generateBadgeSimple(template, message, color string) []byte {
c := parseColor(color)
gF := float64(g.Offset)
fW := float64(g.drawer.MeasureString(message)>>6) + gF
mX := (fW / 2) * 10
mL := (fW - gF) * (10.0 + g.Spacing)
fS := g.fontSize * 10
mC, mS := getMessageColors(c)
badge := strings.ReplaceAll(template, "{MESSAGE}", message)
badge = strings.ReplaceAll(badge, "{COLOR}", formatColor(c))
badge = strings.ReplaceAll(badge, "{WIDTH}", formatFloat(fW))
badge = strings.ReplaceAll(badge, "{MESSAGE_X}", formatFloat(mX))
badge = strings.ReplaceAll(badge, "{MESSAGE_LENGTH}", formatFloat(mL))
badge = strings.ReplaceAll(badge, "{MESSAGE_COLOR}", mC)
badge = strings.ReplaceAll(badge, "{MESSAGE_SHADOW}", mS)
badge = strings.ReplaceAll(badge, "{FONT}", g.fontName)
badge = strings.ReplaceAll(badge, "{FONT_SIZE}", strconv.Itoa(fS))
return []byte(badge)
}
// ////////////////////////////////////////////////////////////////////////////////// //
// parseColor parses hex color
func parseColor(c string) int64 {
if strings.HasPrefix(c, "#") {
c = strings.TrimLeft(c, "#")
}
// Shorthand hex color
if len(c) == 3 {
c = strings.Repeat(string(c[0]), 2) +
strings.Repeat(string(c[1]), 2) +
strings.Repeat(string(c[2]), 2)
}
i, _ := strconv.ParseInt(c, 16, 32)
return i
}
// formatColor formats color
func formatColor(c int64) string {
k := fmt.Sprintf("%06x", c)
if k[0] == k[1] && k[2] == k[3] && k[4] == k[5] {
k = k[0:1] + k[2:3] + k[4:5]
}
return "#" + k
}
// formatFloat formats float values
func formatFloat(v float64) string {
return strconv.FormatFloat(v, 'f', 0, 64)
}
// getMessageColors returns message text and shadow colors based on color of badge
func getMessageColors(c int64) (string, string) {
if c == 0 || calcLuminance(c) < 0.65 {
return "#fff", "#010101"
}
return "#333", "#ccc"
}
// calcLuminance calculates relative luminance
func calcLuminance(color int64) float64 {
r := calcLumColor(float64(color>>16&0xFF) / 255)
g := calcLumColor(float64(color>>8&0xFF) / 255)
b := calcLumColor(float64(color&0xFF) / 255)
return 0.2126*r + 0.7152*g + 0.0722*b
}
// calcLumColor calculates luminance for one color
func calcLumColor(c float64) float64 {
if c <= 0.03928 {
return c / 12.92
}
return math.Pow(((c + 0.055) / 1.055), 2.4)
}