update from their respective sources
[shellsnippets/shellsnippets.git] / ksh / svg-path-abs2rel
1 # $Id: svg-path-abs2rel 2081 2011-06-09 20:21:16Z tglase $
2 #-
3 # Copyright © 2010
4 #       Thorsten Glaser <t.glaser@tarent.de>
5 # Licenced under the AGPLv3
6
7 eval $(cat)
8 set -A in -- $d
9 out=
10 i=0
11
12 function iscoor {
13         [[ $1 = @([1-9])*([0-9])?(.+([0-9])),@([1-9])*([0-9])?(.+([0-9])) ]]
14 }
15 curx=0
16 cury=0
17 basex=-245.93941
18 basey=-435.44425
19 function fixbc {
20         local x=$(bc)
21         if [[ $x = .* ]]; then
22                 print 0$x
23         elif [[ $x = -.* ]]; then
24                 print -- -0${x#-}
25         else
26                 print -- $x
27         fi
28 }
29 function trans {
30         local px py
31
32         nx=${1%,*}
33         ny=${1#*,}
34         px=$(print -- "$nx+$basex-$curx" | fixbc)
35         py=$(print -- "$ny+$basey-$cury" | fixbc)
36         pos=$px,$py
37 }
38 function transet {
39         trans "$1"
40         curx=$(print -- "$nx+$basex" | fixbc)
41         cury=$(print -- "$ny+$basey" | fixbc)
42 }
43
44 while (( i < ${#in[*]} )); do
45         if (( !i )); then
46                 [[ ${in[0]} = M ]] || exit 1
47                 iscoor "${in[1]}" || exit 1
48                 transet "${in[1]}"
49                 out="M $pos"
50                 i=2
51                 continue
52         fi
53         case ${in[i]} {
54         ([Zz])  out="$out ${in[i++]}" ;;
55         (M)     iscoor "${in[i+1]}" || exit 1
56                 transet "${in[i+1]}"
57                 out="$out m $pos"
58                 let i+=2 ;;
59         (L)     iscoor "${in[i+1]}" || exit 1
60                 transet "${in[i+1]}"
61                 out="$out l $pos"
62                 let i+=2 ;;
63         (C)     out="$out c"
64                 iscoor "${in[i+1]}" || exit 1
65                 iscoor "${in[i+2]}" || exit 1
66                 iscoor "${in[i+3]}" || exit 1
67                 trans "${in[i+1]}"
68                 out="$out $pos"
69                 trans "${in[i+2]}"
70                 out="$out $pos"
71                 transet "${in[i+3]}"
72                 out="$out $pos"
73                 let i+=4 ;;
74         (*)     exit 1 ;;
75         }
76 done
77 print "       d=\"$out\""