-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathicons.go
385 lines (331 loc) · 9.19 KB
/
icons.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright (c) 2021 Dean Jackson <deanishe@deanishe.net>
// MIT Licence applies http://opensource.org/licenses/MIT
// Created on 2021-02-05
package main
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"
"path/filepath"
"strings"
aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/util"
"golang.org/x/text/unicode/norm"
)
// icons
var (
iconUpdateAvailable = &aw.Icon{Value: "icons/update-available.png"}
iconUpdateOK = &aw.Icon{Value: "icons/update-ok.png"}
iconColour = &aw.Icon{Value: "icons/colour.png"}
iconConfig = &aw.Icon{Value: "icons/config.png"}
iconTrash = &aw.Icon{Value: "icons/trash.png"}
iconFont = &aw.Icon{Value: "icons/font.png"}
iconFontSelected = &aw.Icon{Value: "icons/font-selected.png"}
iconIssue = &aw.Icon{Value: "icons/issue.png"}
iconDocs = &aw.Icon{Value: "icons/docs.png"}
iconToggleOn = &aw.Icon{Value: "icons/toggle-on.png"}
iconToggleOff = &aw.Icon{Value: "icons/toggle-off.png"}
iconWebsite = &aw.Icon{Value: "icons/website.png"}
iconError = &aw.Icon{Value: "icons/error.png"}
iconWarning = &aw.Icon{Value: "icons/warning.png"}
spinnerIcons = []*aw.Icon{
{Value: "icons/spinner-0.png"},
{Value: "icons/spinner-1.png"},
{Value: "icons/spinner-2.png"},
}
)
// ThemeDir returns the relative path of the font/colour/size directory,
// i.e. "font-name/fgcolour-on-bgcolour/text-size".
func (opts *options) ThemeDir() string {
var (
font = slugify(opts.FontName)
colour = fmt.Sprintf("%s-on-%s", colourName(opts.TextColour), colourName(opts.BackgroundColour))
size = fmt.Sprintf("%0.1f", opts.TextSize)
)
if opts.TextSize == 0 {
size = "default"
}
// if opts.FontName == defaultFontName {
// font = "default"
// }
return filepath.Join(font, colour, size)
}
// IconPath returns the relative path for the icon.
func (cp Codepoint) IconPath() string {
// return cp.Hex[0:2] + "/" + cp.Hex[2:4] + "/" + cp.Name + ".png"
return fmt.Sprintf("%s/U+%04X - %s.png", cp.Hex[0:2], cp.Dec, cp.Name)
}
// colouredIcon returns a version of icon in the given colour. If no colour
// is specified or something goes wrong, icon is simply returned.
func colouredIcon(icon *aw.Icon, colour string) *aw.Icon {
c, err := ParseHexColour(colour)
if err != nil {
log.Printf("[icons] ERR: %s", err)
return icon
}
path := colouredIconCachePath(icon, c)
if util.PathExists(path) {
return &aw.Icon{Value: path}
}
if err = generateColouredIcon(icon.Value, path, c); err != nil {
log.Printf("[icons] ERR: generate icon: %v", err)
return icon
}
rel, _ := filepath.Rel(iconDir, path)
log.Printf("new icon: %s", rel)
return &aw.Icon{Value: path}
}
func colouredIconCachePath(i *aw.Icon, c color.RGBA) string {
name := fmt.Sprintf("%02X%02X%02X%02X.png", c.R, c.G, c.B, c.A)
// dir := filepath.Join(iconDir, hex[0:2], hex[2:4], hex[4:6])
dir := filepath.Join(iconDir, "colours", fmt.Sprintf("%02X/%02X/%02X", c.R, c.G, c.B))
util.MustExist(dir)
return filepath.Join(dir, name)
}
// spinnerIcon returns a spinner icon. It rotates by 15 deg on every
// subsequent call. Use with wf.Reload(0.1) to implement an animated
// spinner.
func spinnerIcon() *aw.Icon {
n := opts.SpinnerCounter
wf.Var("spinner_counter", fmt.Sprintf("%d", n+1))
return spinnerIcons[n%3]
}
// NormaliseHexColour converts a hex colour (#abc, #AABBCC, a1b2c3 etc.) to
// a 6- or 8-character form without preceding hash sign.
func NormaliseHexColour(s string) (string, error) {
s = strings.TrimSpace(s)
if s == "" {
return "", errors.New("empty string")
}
if s[0] == '#' {
s = s[1:]
}
if len(s) == 3 || len(s) == 4 {
var s2 string
for _, c := range s {
s2 += string(c) + string(c)
}
s = s2
}
b, err := hex.DecodeString(s)
if err != nil {
return "", fmt.Errorf("invalid colour: %q", s)
}
if len(b) == 3 || len(b) == 4 {
return strings.ToUpper(s), nil
}
return "", fmt.Errorf("colour hex must be 3, 4, 6 or 8 characters: %s", s)
}
// ParseHexColour parses a CSS hex (e.g. #ffffff) to RGBA.
//
// Input must have 3/6 (RGB) or 4/8 (RGBA) characters. Preceding # is optional.
func ParseHexColour(s string) (color.RGBA, error) {
var (
// default to 100% opaque
c = color.RGBA{A: 0xff}
err error
)
if s, err = NormaliseHexColour(s); err != nil {
return c, err
}
// if s[0] == '#' {
// s = s[1:]
// }
// if len(s) == 3 || len(s) == 4 {
// var s2 string
// for _, c := range s {
// s2 += string(c) + string(c)
// }
// s = s2
// }
b, err := hex.DecodeString(s)
if err != nil {
return c, fmt.Errorf("invalid colour: %q", s)
}
switch len(b) {
case 3:
c.R = b[0]
c.G = b[1]
c.B = b[2]
case 4:
c.R = b[0]
c.G = b[1]
c.B = b[2]
c.A = b[3]
default:
err = fmt.Errorf("invalid colour: %q", s)
}
return c, err
}
func runGenerate() error {
gen := newIconGenerator(iconDir, aw.IconWorkflow)
return gen.Generate()
}
type iconGenerator struct {
Dir string
DefaultIcon *aw.Icon
queue []Codepoint
}
func newIconGenerator(dir string, defaultIcon *aw.Icon) *iconGenerator {
return &iconGenerator{
Dir: dir,
DefaultIcon: defaultIcon,
}
}
// Get returns icon for a Codepoint.
func (gen *iconGenerator) Get(cp Codepoint) *aw.Icon {
path := gen.iconPath(cp)
if util.PathExists(path) {
return &aw.Icon{Value: path}
}
gen.queue = append(gen.queue, cp)
return gen.DefaultIcon
}
// HasQueue returns true if generator's queue isn't empty.
func (gen *iconGenerator) HasQueue() bool {
return len(gen.queue) > 0
}
// Generate processes the queue.
func (gen *iconGenerator) Generate() error {
path := gen.queuePath()
if !util.PathExists(path) {
return nil
}
out, err := util.Run("icongen", path)
if err != nil {
return fmt.Errorf("generate icons: %w", err)
}
log.Print("icongen output:\n" + string(out))
if err := os.Remove(path); err != nil {
return fmt.Errorf("remove queue: %w", err)
}
return nil
}
type queueIcon struct {
IconSize int `json:"icon_size"`
Text string `json:"text"`
Path string `json:"filepath"`
FontName string `json:"font_name,omitempty"`
TextSize float64 `json:"text_size,omitempty"`
ResizeFactor float64 `json:"resize_factor,omitempty"`
TextColour string `json:"text_colour,omitempty"`
BackgroundColour string `json:"background_colour,omitempty"`
}
// Saves saves queue if not empty.
func (gen *iconGenerator) Save() error {
if len(gen.queue) == 0 {
return nil
}
payload := make([]queueIcon, len(gen.queue))
for i, cp := range gen.queue {
payload[i] = queueIcon{
IconSize: opts.IconSize,
Text: string(cp.Dec),
Path: gen.iconPath(cp),
FontName: opts.FontName,
TextSize: opts.TextSize,
ResizeFactor: opts.ResizeFactor,
TextColour: opts.TextColour,
BackgroundColour: opts.BackgroundColour,
}
}
data, err := json.MarshalIndent(payload, "", " ")
if err != nil {
return fmt.Errorf("marshal queue to JSON: %w", err)
}
if err := os.MkdirAll(gen.Dir, 0700); err != nil {
return fmt.Errorf("create icons directory: %w", err)
}
if err := util.WriteFile(gen.queuePath(), data, 0600); err != nil {
return fmt.Errorf("save queue: %w", err)
}
log.Printf("[icons] queued %d icon(s) for generation", len(gen.queue))
gen.queue = []Codepoint{}
return nil
}
// iconPath returns icon path for Codepoint.
func (gen *iconGenerator) iconPath(cp Codepoint) string {
return filepath.Join(gen.Dir, opts.ThemeDir(), cp.IconPath())
}
// queuePath returns path to queue file.
func (gen *iconGenerator) queuePath() string {
return filepath.Join(gen.Dir, "queue.json")
}
func generateColouredIcon(src, dest string, c color.RGBA) error {
// defer util.Timed(time.Now(), "generate icon")
var (
f *os.File
mask image.Image
err error
)
if f, err = os.Open(src); err != nil {
return fmt.Errorf("open file: %w", err)
}
defer f.Close()
if mask, _, err = image.Decode(f); err != nil {
return fmt.Errorf("decode image: %w", err)
}
img := image.NewRGBA(mask.Bounds())
draw.DrawMask(img, img.Bounds(), &image.Uniform{c}, image.Point{}, mask, image.Point{}, draw.Src)
if f, err = os.Create(dest); err != nil {
return fmt.Errorf("create file: %w", err)
}
defer f.Close()
if err = png.Encode(f, img); err != nil {
return fmt.Errorf("write PNG data: %w", err)
}
return nil
}
// return "transparent" if alpha is 0, 6-char hex if alpha is 1,
// and 8-char hex if 0 < alpha < 1.
func colourName(hex string) string {
if hex == "" {
return "transparent"
}
c, _ := ParseHexColour(hex)
if c.A == 0 {
return "transparent"
}
if len(hex) == 6 {
return hex
}
if c.A == 255 {
return hex[0:6]
}
return hex
}
func slugify(s string) string {
s = strings.ToLower(strings.TrimSpace(s))
if s == "" {
return s
}
s = norm.NFD.String(s)
clean := []rune{}
var last rune
for _, r := range s {
if r > 0x7F { // skip anything above ASCII
continue
}
if (r > 0x2F && r < 0x3A) ||
(r > 0x60 && r < 0x7A) {
clean = append(clean, r)
last = r
} else if last != '-' {
clean = append(clean, '-')
last = '-'
}
}
s = string(clean)
s = strings.Trim(s, "-")
if s == "" {
return "-"
}
return s
}