]> code.delx.au - comingnext/blob - ComingNext_SkinFetcher/gfx/svg2s60.pl
eb85aa4238466a3c6ad423e3ed13aabb36c57ad9
[comingnext] / ComingNext_SkinFetcher / gfx / svg2s60.pl
1 ###
2 # SVG2S60 - Cleans Inkscape SVG files for use with svg2svgt in the S60 SDK
3 # Copyright (C) 2007 Ian Dunbar
4 # Modified by Brian Smith and Michael Prager
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ###
20
21 ###
22 #
23 # SVG2S60 - Cleans Inkscape SVG files for use with svg2svgt in the S60 SDK
24 #
25 # Usage: svg2s60.pl in.svg > out.svg
26 #
27 # Description: This script just splits the style tags used by Inkscape, but not
28 # properly supported by svg2svgt into it's seperate components. This helps to
29 # solve color mapping issues in svg2svgt for files that have been edited by
30 # Inkscape.
31 #
32 ###
33
34 ###
35 # Changelog:
36 # v1.0 by Ian Dunbar
37 # - initial release
38 # v1.1 by Brian Smith
39 # - update to work with the latest inkscape version 0.47
40 # v1.2 by Michael Prager
41 # - fix moveto commands with implicit lineto commands in paths
42 # - fix gradients that use xlink:href references
43 ###
44
45 my $inputfile = "";
46 while (<>) {
47 $inputfile .= $_;
48 }
49
50 sub fixPathData {
51 my $output = shift;
52 # do some cleanup to make regex easier, add spacing between numbers and commands and replace , with space
53 $output =~ s/(\d)-/$1 -/g;
54 $output =~ s/(\d)([a-df-zA-DF-Z])/$1 $2/g;
55 $output =~ s/([a-df-zA-DF-Z])(-?\d)/$1 $2/g;
56 $output =~ s/([a-df-zA-DF-Z])([a-df-zA-DF-Z])/$1 $2/g;
57 $output =~ s/,/ /g;
58 # fix moveto commands that have more than two coordinates. Interpret additionl coordinates as lineto commands as defined in the SVG DTD
59 $output =~ s/^m\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s*/M $1 $2 l $3 $4 /g;
60 $output =~ s/m\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s*/m $1 $2 l $3 $4 /g;
61 $output =~ s/M\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s*/M $1 $2 L $3 $4 /g;
62 return $output;
63 }
64
65 sub fixGradient {
66 my $output = shift;
67 $output =~ s/^#//;
68 # dereference linked gradients
69 if ($inputfile =~ /<(linearGradient|radialGradient)([^<>]*?)id=\"$output\"([^<>]*?)>(.*?)<\/\1>/i) {
70 $output = $4;
71 }
72 else {
73 $output = "";
74 }
75 return $output;
76 }
77
78 # remove linebreaks & whitespaces, makes parsing easier
79 $inputfile =~ s/\n/ /g;
80 $inputfile =~ s/ +/ /g;
81
82 # remove style attribute used by Inkscape, use normal attributes instead
83 while($inputfile =~ /\s+style=\"(.*?)\"/g) {
84 my $input = $1;
85 my $output = "";
86 my @styles = split /;/,$input;
87 foreach $style (@styles) {
88 $style =~ s/(.*):(.*)/$1=\"$2\" /;
89 if ($style !~ /^-/) { # don't allow attributes that start with "-"
90 $output .= $style;
91 }
92 }
93 $inputfile =~ s/\s+style=\"\Q$input\E\"/ $output/g;
94 }
95
96 # fix moveto command in paths if implicit lineto commands are used
97 $inputfile =~ s/<path([^<>]*?)\s+d=\"(.*?)\"/'<path' . $1 . ' d="' . fixPathData($2) . '"'/eg;
98
99 # fix linearGradiants that use references
100 $inputfile =~ s/<(linearGradient|radialGradient)([^<>]*?)\s*xlink:href="(.*?)"([^<>]*?)\/>/"<$1$2 $4>".fixGradient($3)."<\/$1>"/ieg;
101
102 # restore "human readable" file layout
103 $inputfile =~ s/<(.*?)>/<$1>\n/g;
104 $inputfile =~ s/\n +/\n/g;
105
106 print $inputfile;