-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen-icon.sh
executable file
·157 lines (131 loc) · 3.45 KB
/
gen-icon.sh
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
#!/usr/bin/env bash
app_name="$(basename "${BASH_SOURCE[0]}")"
USAGE=$(cat << EOF
Usage:
$app_name
Generate PNG images from SVG files.
Options:
-s, --sizes=<SIZE_LIST>
Which sizes to render SVG files formatted as comma separated integer
values. Original size from the SVG file is used when this option is
missing or 0 is used.
-a, --apple=[SIZE,PADDING]
Apple touch icon size to generate. "180,25" is used when no size or
padding argument is given. Padding is automatically calculated as 1/7
of size if only size is given.
-h, --help
Show this help information and exit.
EOF
)
getopt -q -T
if [[ $? -ne 4 ]]; then
echo "$app_name: This script requires an enhanced getopt version." >&2
exit 1
fi
opts=$(getopt -o 'hs:a::' -l 'help,sizes:,apple::' -n "$app_name" -- "$@") || exit $?
eval "opts=($opts)"
args=()
apple=0
sizes=0
for ((i = 0; i < ${#opts[@]}; i++)); do
opt="${opts["$i"]}"
case "$opt" in
-s|--sizes)
((i++))
readarray -t -d, png_sizes < <(printf "%s" "${opts["$i"]}")
sizes=1
;;
-a|--apple)
((i++))
readarray -t -d, apple_sizes < <(printf "%s" "${opts["$i"]}")
apple=1
;;
-h|--help)
echo "$USAGE"
exit 0
;;
--)
args+=("${opts[@]:((i+1))}")
break
;;
esac
done
if (( apple != 0 )); then
apple_touch_icon_size="${apple_sizes[0]:-180}"
apple_touch_icon_padding="${apple_sizes[1]:-((apple_touch_icon_size / 7))}" # 25
apple_touch_icon_content_size="$((apple_touch_icon_size - apple_touch_icon_padding))"
fi
set -- "${args[@]}"
command -v resvg > /dev/null; resvg=$?
if [[ resvg -ne 0 ]]; then
echo "Cannot find 'resvg' command. Make sure resvg is installed and resvg reachable from the current working directory. See: https://github.com/RazrFalcon/resvg" >&2
exit 1
fi
command -v convert > /dev/null; magick=$?
if [[ magick -ne 0 ]]; then
echo "Cannot find 'convert' command. Make sure ImageMagick is installed and reachable from the current working directory. See: https://www.imagemagick.org" >&2
exit 1
fi
status=0
export_png() {
if [[ $2 -nt $3 ]]; then
if (( $1 == 0 )); then
resvg "$2" "$3"
else
resvg -w "$1" -h "$1" "$2" "$3"
fi
fi
}
if (( apple == 0 && sizes == 0 )); then
png_sizes=(0) # Original size of SVG
fi
readarray -d '' -t png_sizes < <(for s in "${png_sizes[@]}"; do printf '%s\0' "$s"; done | sort -r -u -n -z)
for f in "$@"; do
name="${f%.*}"
pngs=()
declare -A size2job
declare -A job2png
for i in "${png_sizes[@]}"; do
if (( i == 0 )); then
png="$name.g.png"
else
png="$name-$i.g.png"
fi
export_png "$i" "$f" "$png" &
size2job["$i"]=$!
job2png[$!]="$png"
done
if (( apple != 0 )); then
apple_icon="$(dirname "$name")/apple-touch-icon.g.png"
export_png "$apple_touch_icon_content_size" "$f" "$apple_icon" &
apple_job=$!
fi
for i in "${png_sizes[@]}"; do
if (( i != 0 && i < 256 )); then
j="${size2job["$i"]}"
if wait -n "$j"; then
pngs+=("${job2png["$j"]}")
unset job2png["$j"]
else
status=1
fi
fi
done
if (( ${#pngs[@]} > 0 )); then
ico="$name.g.ico"
convert -background none "${pngs[@]}" "$ico"
fi
if (( apple != 0 )); then
if wait -n "$apple_job"; then
convert "$apple_icon" -define png:exclude-chunks=date,time -background white -gravity center -extent "${apple_touch_icon_size}x${apple_touch_icon_size}" "$apple_icon"
else
status=1
fi
fi
done
for j in "${!job2png[@]}"; do
if ! wait "$j"; then
status=1
fi
done
exit "$status"