1d3182a3baf5bd7dc8f484d78fbbe353b6b6b969
[nit.git] / examples / mnit_dino / tools / svg-to-pngs
1 #!/bin/bash
2
3 # This file is part of NIT ( http://www.nitlanguage.org ).
4 #
5 # Copyright 2012-2013 Alexis Laferrière <alexis.laf@xymus.net>
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18
19 # This script extracts pngs from a single svg for all objects with ids
20 # beginning by 0. Requires Inkscape.
21
22 # there are some trick used do to inconsistency in inkscape input and output values
23 # the document must be resized to fit all objects
24
25 dir=`dirname $0`
26 log=$dir/`basename $0 .sh`.log
27
28 echo $0 executed at `date` > $log
29
30 if [ $# -ne 2 ]
31 then
32         echo "Usage: $0 source.svg destination/folder/"
33         exit
34 fi
35
36 sourceFile="$1"
37 destinationFolder="$2"
38 if [ ! -f $sourceFile ]
39 then
40         echo "Source file \"$sourceFile\" not found."
41         exit
42 fi
43
44 if [ ! -d $destinationFolder ]
45 then
46         echo "Destination folder \"$destinationFolder\" not found, creating it."
47         mkdir -p $destinationFolder
48 fi
49
50
51 query=`inkscape --query-all $sourceFile`
52 min_x=10000
53 min_y=10000
54 for line in $query; do
55         ld=(${line//,/ })
56
57         x=${ld[1]}
58         y=${ld[2]}
59         w=${ld[3]}
60         h=${ld[4]}
61         # remove e notation
62         if echo $y | grep e &> /dev/null; then
63                 y=0
64         fi
65         if echo $x | grep e &> /dev/null; then
66                 x=0
67         fi
68
69         echo "$x < $min_x" | bc &> /dev/null
70         if [ $? ]; then
71                 min_x=$x
72         fi
73
74         y=`echo $y-$h|bc`
75         echo "$y < $min_y" | bc &> /dev/null
76         if [ $? ]; then
77                 min_y=$y
78         fi
79 done
80
81 full_h=`inkscape "-f$sourceFile" -C -H 2>> $log`
82
83 for line in $query; do #`inkscape --query-all $sourceFile | grep ^0`; do
84         if echo $line | grep ^0 &> /dev/null; then
85                 ld=(${line//,/ })
86                 id=${ld[0]} #${line:0: $[`expr index "$line" ,` - 1] }
87                 name=${line:1: $[`expr index "$line" ,` - 2] }
88
89                 x=${ld[1]}
90                 y=${ld[2]}
91                 w=${ld[3]}
92                 h=${ld[4]}
93
94                 # remove e notation
95                 if echo $y | grep e &> /dev/null; then
96                         y=0
97                 fi
98                 if echo $x | grep e &> /dev/null; then
99                         x=0
100                 fi
101
102                 # corrected dim for pow(2)
103                 i=`bc <<!
104                         i=1
105                         while (i<$w || i<$h){
106                                 i=i*2
107                         }
108                         print i
109 !`
110                 echo "found $i for $w $h" &>> $log
111                 cw=$i
112                 ch=$i
113                 dw=`echo \($cw-$w\)/2|bc`
114                 dh=`echo \($ch-$h\)/2|bc`
115                 cx=`echo $x + \($cw-$w\)/2|bc`
116                 cy=`echo $y + \($ch-$h\)/2|bc`
117
118                 # corrects value of y.
119                 y=`echo $full_h - $y | bc`
120
121                 echo "x=$x y=$y w=$w h=$h" &>> $log
122                 echo "cx=$cx cy=$cy cw=$cw ch=$ch" &>> $log
123                 i=`echo "$i * 2" | bc`
124                 inkscape -z -C "-f$sourceFile" "-i$id" -j "-a`echo $x-$dw|bc`:`echo $y-$h-$dh|bc`:`echo $x+$w+$dw|bc`:`echo $y+$dh|bc`" "-e$destinationFolder/$name.png" -b#000000 -y0.0 -d180 -w$i -h$i &>> $log
125         fi
126 done