-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-sizes.go
82 lines (67 loc) · 1.79 KB
/
generate-sizes.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"sync"
)
var sizes = []int{16, 18, 22, 24, 32, 42, 48, 64, 84, 96, 128}
func main() {
inputDir := "scalable/apps"
svgFiles, err := filepath.Glob(filepath.Join(inputDir, "*.svg"))
if err != nil {
log.Fatalf("Failed to read SVG files: %v", err)
}
for _, s := range sizes {
sizeDir := fmt.Sprintf("%dx%d/apps", s, s)
os.MkdirAll(sizeDir, os.ModePerm)
cleanDirectory(sizeDir)
}
var wg sync.WaitGroup
for _, icon := range svgFiles {
wg.Add(1)
go func(icon string) {
defer wg.Done()
optimizeAndGenerateIcon(icon)
}(icon)
}
wg.Wait()
fmt.Println("PNG generation completed.")
}
func cleanDirectory(dir string) {
files, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
log.Printf("Failed to clean directory %s: %v", dir, err)
return
}
for _, file := range files {
if err := os.Remove(file); err != nil {
log.Printf("Failed to remove %s: %v", file, err)
}
}
}
func optimizeAndGenerateIcon(icon string) {
optimizedIcon := optimizeSVG(icon)
iconname := filepath.Base(icon[:len(icon)-4])
for _, s := range sizes {
sizeDir := fmt.Sprintf("%dx%d/apps", s, s)
outputFile := filepath.Join(sizeDir, iconname+".png")
fmt.Printf("Generating %s...\n", outputFile)
cmd := exec.Command("rsvg-convert", "-w", fmt.Sprint(s), "-h", fmt.Sprint(s), "-o", outputFile, optimizedIcon)
if err := cmd.Run(); err != nil {
log.Printf("Failed to generate %s: %v", outputFile, err)
}
}
}
func optimizeSVG(icon string) string {
optimizedIcon := icon + ".optimized.svg"
fmt.Printf("Optimizing %s...\n", icon)
cmd := exec.Command("svgo", "-i", icon, "-o", optimizedIcon)
if err := cmd.Run(); err != nil {
log.Printf("Failed to optimize %s: %v", icon, err)
return icon
}
return optimizedIcon
}