-
Notifications
You must be signed in to change notification settings - Fork 34
/
render.sh
executable file
·282 lines (232 loc) · 9.73 KB
/
render.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
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
#!/bin/bash
set -e -u
# Usage: ./render.sh [png|sprite|css]
# Config
pngdirbuilder=`pwd`"/renders/npmap-builder" # PNGs will be created, possibly overwritten, here
pngdirstandalone=`pwd`"/renders/standalone" # PNGs will be created, possibly overwritten, here
svgdirbuilder=`pwd`"/src/npmap-builder" # SVGs should already be here
svgdirstandalone=`pwd`"/src/standalone" # SVGs should already be here
tilex=15 # how many icons wide the sprites will be
function build_pngs_builder {
# Takes a list of SVG files and renders both 1x and 2x scale PNGs
for svg in $@; do
icon=$(basename $svg .svg)
inkscape \
--export-dpi=90 \
--export-png=${pngdirbuilder}/${icon}.png \
$svg > /dev/null
inkscape \
--export-dpi=180 \
--export-png=${pngdirbuilder}/${icon}@2x.png \
$svg > /dev/null
done
}
function build_pngs_standalone {
# Takes a list of SVG files and renders both 1x and 2x scale PNGs
for svg in $@; do
icon=$(basename $svg .svg)
inkscape \
--export-dpi=90 \
--export-png=${pngdirstandalone}/${icon}.png \
$svg > /dev/null
inkscape \
--export-dpi=180 \
--export-png=${pngdirstandalone}/${icon}@2x.png \
$svg > /dev/null
done
}
function build_sprite {
# Takes a list of PNG files and creates a sprite file
# The `montage` results are weird if the number of images passed in
# do not grid perfectly, so we calculate the correct number of null
# images to add into the command.
outfile=$1
shift # the rest of the arguments should be filenames
count=$(echo $@ | tr ' ' '\n' | wc -l)
remainder=$(($count % $tilex))
rnull=$(for ((i=1; i<=$remainder; i++)); do echo -n 'null: '; done)
montage \
-type TrueColorMatte \
-background transparent \
-geometry +0+0 \
-tile ${tilex}x \
-gravity Northwest \
$@ $rnull \
$outfile
}
function build_css_builder {
# Takes a list of icon names, calculates the correct CSS background-
# positions for 22px icons, and creates an appropriate CSS file.
# Assumes that the icon list matches what was passed to the last run
# of the build_pngs function.
count=0
dx=0
dy=0
maxwidth=$((54*$tilex/3-1))
cat www/npmap-symbol-library-sprite.css.tpl > www/npmap-builder/npmap-symbol-library-sprite.css
for icon in $@; do
count=$(($count + 1))
echo ".npmap-symbol-library-icon.$icon { background-position: ${dx}px ${dy}px; }" \
>> www/npmap-builder/npmap-symbol-library-sprite.css
# Check if we need to add a new row yet,
# and if so, adjust dy and dy accordingly.
# Otherwise just adjust dx.
if [ $(($count * 3 % $tilex)) -eq 0 ]; then
dy=$(($dy - 24))
dx=0
else
dx=$(($dx - 54))
fi
done
# Update sprite dimensions for retina rules.
dim="$(identify -format "%wpx %hpx" www/npmap-builder/images/npmap-symbol-library-sprite.png)"
cat www/npmap-builder/npmap-symbol-library-sprite.css | sed "s/background-size: [0-9]*px [0-9]*px;/background-size: $dim;/" > www/npmap-builder/npmap-symbol-library-sprite.css.tmp
mv www/npmap-builder/npmap-symbol-library-sprite.css.tmp www/npmap-builder/npmap-symbol-library-sprite.css
}
function build_css_standalone {
count=0
dx=0
dy=0
maxwidth=$((54*$tilex/3-1))
cat www/npmap-symbol-library-sprite.css.tpl > www/standalone/npmap-symbol-library-sprite.css
for icon in $@; do
count=$(($count + 1))
echo ".npmap-symbol-library-icon.$icon { background-position: ${dx}px ${dy}px; }" \
>> www/standalone/npmap-symbol-library-sprite.css
# Check if we need to add a new row yet,
# and if so, adjust dy and dy accordingly.
# Otherwise just adjust dx.
if [ $(($count * 3 % $tilex)) -eq 0 ]; then
dy=$(($dy - 22))
dx=0
else
dx=$(($dx - 54))
fi
done
# Update sprite dimensions for retina rules.
dim="$(identify -format "%wpx %hpx" www/standalone/images/npmap-symbol-library-sprite.png)"
cat www/standalone/npmap-symbol-library-sprite.css | sed "s/background-size: [0-9]*px [0-9]*px;/background-size: $dim;/" > www/standalone/npmap-symbol-library-sprite.css.tmp
mv www/standalone/npmap-symbol-library-sprite.css.tmp www/standalone/npmap-symbol-library-sprite.css
}
function build_positions_builder {
# Takes a list of icon names, calculates the positions, outputs json
count=0
dx=0
dy=0
maxwidth=$((54*$tilex/3-1))
file="www/npmap-builder/npmap-symbol-library-sprite.json"
echo "{" > $file;
for icon in $@; do
if [ $count -ne 0 ]; then ## avoid trailing comma
echo "," >> $file
fi
count=$(($count + 1))
echo "\"$icon-24\": { \"x\": $((-1 * dx)), \"y\": $((-1 * $dy)), \"width\": 24, \"height\": 24 }," >> $file
echo "\"$icon-18\": { \"x\": $((-1 * dx + 22)), \"y\": $((-1 * $dy)), \"width\": 18, \"height\": 18 }," >> $file
echo -n "\"$icon-12\": { \"x\": $((-1 * dx + 42)), \"y\": $((-1 * $dy)), \"width\": 12, \"height\": 12 }" >> $file
# Check if we need to add a new row yet,
# and if so, adjust dy and dy accordingly.
# Otherwise just adjust dx.
if [ $(($count * 3 % $tilex)) -eq 0 ]; then
dy=$(($dy - 24))
dx=0
else
dx=$(($dx - 54))
fi
done
echo "}" >> $file
}
function build_positions_standalone {
count=0
dx=0
dy=0
maxwidth=$((72*$tilex/3-1))
file="www/standalone/npmap-symbol-library-sprite.json"
echo "{" > $file;
for icon in $@; do
if [ $count -ne 0 ]; then ## avoid trailing comma
echo "," >> $file
fi
count=$(($count + 1))
echo "\"$icon-30\": { \"x\": $((-1 * dx)), \"y\": $((-1 * $dy)), \"width\": 30, \"height\": 30 }," >> $file
echo "\"$icon-22\": { \"x\": $((-1 * dx + 30)), \"y\": $((-1 * $dy)), \"width\": 22, \"height\": 22 }," >> $file
echo -n "\"$icon-14\": { \"x\": $((-1 * dx + 56)), \"y\": $((-1 * $dy)), \"width\": 14, \"height\": 14 }" >> $file
# Check if we need to add a new row yet,
# and if so, adjust dy and dy accordingly.
# Otherwise just adjust dx.
if [ $(($count * 3 % $tilex)) -eq 0 ]; then
dy=$(($dy - 30))
dx=0
else
dx=$(($dx - 70))
fi
done
echo "}" >> $file
}
# Get a lcst of all the icon names - any icons not in npmap-symbol-library.json
# will not be rendered or included in the sprites.
iconsbuilder=$(grep '"icon":' www/npmap-builder/npmap-symbol-library.json \
| sed 's/.*\:\ "\([-a-z0-9]*\)".*/\1/' \
| tr '\n' ' ')
iconsstandalone=$(grep '"icon":' www/standalone/npmap-symbol-library.json \
| sed 's/.*\:\ "\([-a-z0-9]*\)".*/\1/' \
| tr '\n' ' ')
# Build lists of all the SVG and PNG files from the icons list
svgsbuilder=$(for icon in $iconsbuilder; do echo -n $svgdirbuilder/${icon}-{24,18,12}.svg" "; done)
pngsbuilder=$(for icon in $iconsbuilder; do echo -n $pngdirbuilder/${icon}-{24,18,12}.png" "; done)
pngs2xbuilder=$(for icon in $iconsbuilder; do echo -n $pngdirbuilder/${icon}-{24,18,12}@2x.png" "; done)
svgsstandalone=$(for icon in $iconsstandalone; do echo -n $svgdirstandalone/${icon}-{30,22,14}.svg" "; done)
pngsstandalone=$(for icon in $iconsstandalone; do echo -n $pngdirstandalone/${icon}-{30,22,14}.png" "; done)
pngs2xstandalone=$(for icon in $iconsstandalone; do echo -n $pngdirstandalone/${icon}-{30,22,14}@2x.png" "; done)
case $@ in
png | pngs )
build_pngs $svgsbuilder
build_pngs $svgsstandalone
;;
sprite | sprites )
build_sprite "www/npmap-builder/images/npmap-symbol-library-sprite.png" $pngsbuilder
build_sprite "www/npmap-builder/images/[email protected]" $pngs2xbuilder
build_sprite "www/standalone/images/npmap-symbol-library-sprite.png" $pngsstandalone
build_sprite "www/standalone/images/[email protected]" $pngs2xstandalone
;;
css )
build_css_builder $iconsbuilder
build_css_standalone $iconsstandalone
;;
positions )
build_positions_builder $iconsbuilder
build_positions_standalone $iconsstandalone
;;
debug )
# Prints out all of the icon and file lists for debugging
echo -e "\nIcons (NPMap Builder):"
echo $iconsbuilder
echo -e "\nIcons (Standalone):"
echo $iconsstandalone
echo -e "\nSVGs (NPMap Builder):"
echo $svgsbuilder
echo -e "\nSVGs (Standalone):"
echo $svgsstandalone
echo -e "\nPNGs (NPMap Builder):"
echo $pngsbuilder
echo -e "\nPNGs (Standalone):"
echo $pngsstandalone
echo -e "\nPNGs @2x (NPMap Builder):"
echo $pngs2xbuilder
echo -e "\nPNGs @2x (Standalone):"
echo $pngs2xstandalone
;;
* )
# By default we build the PNGs, sprites, CSS, and position JSON
build_pngs_builder $svgsbuilder
build_sprite "www/npmap-builder/images/npmap-symbol-library-sprite.png" $pngsbuilder
build_sprite "www/npmap-builder/images/[email protected]" $pngs2xbuilder
build_css_builder $iconsbuilder
build_positions_builder $iconsbuilder
build_pngs_standalone $svgsstandalone
build_sprite "www/standalone/images/npmap-symbol-library-sprite.png" $pngsstandalone
build_sprite "www/standalone/images/[email protected]" $pngs2xstandalone
build_css_standalone $iconsstandalone
build_positions_standalone $iconsstandalone
;;
esac