forked from plantuml/plantuml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_sprites.sh
executable file
·175 lines (135 loc) · 5.52 KB
/
create_sprites.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
#!/usr/bin/env bash
#
# (C) Copyright 2017, Anthony Gaudino
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
# Batch creates sprite files for PlantUml
#
# Given a directory can convert all SVG files to PNG, then on the same directory,
# convert all PNG files to PlantUml sprite files.
# To convert SVGs the -s flag must be set.
#
# The generated sprites files formats are based on the ones introduced in
# PlantUml 1.2017.19.
#
# This script assumes InkScape and PlantUml are in PATH.
#
# Help usage message
usage="Batch creates sprite files for PlantUml.
$(basename "$0") [options] prefix
options:
-p directory path to process Default: ./
-w width of PNG from SVG Default: 48
-h height of PNG from SVG Default: 48
-g sprite graylevel Default: 16
-s if set processes SVGs
prefix: a prefix that is added to the sprite name"
# Default arguments values
dir="./" # Directory path to process Default: ./
width=48 # Width of PNG to generate Default: 48
height=48 # Height of PNG to generate Default: 48
graylevel=16 # Number of grayscale colors Default: 16
processsvg=0 # 1 if SVGs should be processed Default: 0
prefix="" # Prefix for sprites names, avoids having
prefixupper="" # two sprites with same name on STDLIB
########################################
#
# Main function
#
########################################
main () {
# Get arguments
while getopts p:w:h:g:s option
do
case "$option" in
p) dir="$OPTARG";;
w) width="$OPTARG";;
h) height="$OPTARG";;
g) graylevel="$OPTARG";;
s) processsvg=1;;
:) echo "$usage"
exit 1
;;
\?) echo "$usage"
exit 1
;;
esac
done
# Get mandatory argument
shift $(($OPTIND-1))
prefix=$( echo $1 | tr '[:upper:]' '[:lower:]')
prefixupper=$(echo $1 | tr '[:lower:]' '[:upper:]')
# Check mandatory argument
if [ -z "$prefix" ]
then
echo "Please specify a prefix!"
echo "$usage"
exit 1
fi
# Change dir to where images are
if [ ! -d "${dir}" ]
then
echo "Please specify a valid directory"
echo "$usage"
exit 1
fi
cd "$dir"
# Processes dir
if [ "$processsvg" -eq 1 ]
then
process_svg
fi
process_png
}
########################################
#
# Convert all SVG files in directory to PNG
#
########################################
process_svg () {
for i in *.svg
do
[ -f "$i" ] || continue
inkscape -z --export-png=`echo $i | sed -e 's/svg$/png/' | sed 's/[^a-zA-Z0-9._]/_/g'` -w $width -h $height -b white $i
done
}
########################################
#
# Generate PlantUml sprite
#
########################################
process_png () {
for i in *.png
do
[ -f "$i" ] || continue
filename=$(echo $i | sed -e 's/.png$//') # Filename without extension
filenameupper=$(echo $filename | tr '[:lower:]' '[:upper:]') # Filename without extension in uppercase
spritename="${prefix}_$filename" # Sprite name is composed by prefix_filename
spritenameupper="${prefixupper}_$filenameupper" # Sprite name in uppercase
spritestereo="$prefixupper $filenameupper" # Sprite stereotype is uppercase prefix followed by uppercase filename
stereowhites=$(echo $spritestereo | sed -e 's/./ /g') # This is just whitespace to make output nicer
#echo "@startuml" >> $filename.puml
echo -e "$(plantuml -encodesprite $graylevel $i | sed '1!b;s/\$/$'${prefix}_'/')\n" >> $filename.puml
echo "!define $spritenameupper(_color) SPRITE_PUT( $stereowhites $spritename, _color)" >> $filename.puml
echo "!define $spritenameupper(_color, _scale) SPRITE_PUT( $stereowhites $spritename, _color, _scale)" >> $filename.puml
echo "!define $spritenameupper(_color, _scale, _alias) SPRITE_ENT( _alias, $spritestereo, $spritename, _color, _scale)" >> $filename.puml
echo "!define $spritenameupper(_color, _scale, _alias, _shape) SPRITE_ENT( _alias, $spritestereo, $spritename, _color, _scale, _shape)" >> $filename.puml
echo "!define $spritenameupper(_color, _scale, _alias, _shape, _label) SPRITE_ENT_L(_alias, $spritestereo, _label, $spritename, _color, _scale, _shape)" >> $filename.puml
echo "skinparam folderBackgroundColor<<$prefixupper $filenameupper>> White" >> $filename.puml
#echo "@enduml" >> $filename.puml
done
}
main "$@"