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