#!/usr/bin/perl

###
#    SVG2S60 - Cleans Inkscape SVG files for use with svg2svgt in the S60 SDK
#    Copyright (C) 2007  Ian Dunbar
#    Modified by Brian Smith and Michael Prager
#
#    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 2 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, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
###

###
#
# SVG2S60 - Cleans Inkscape SVG files for use with svg2svgt in the S60 SDK
#
# Usage: svg2s60.pl in.svg > out.svg
#
# Description: This script just splits the style tags used by Inkscape, but not
# properly supported by svg2svgt into it's seperate components. This helps to
# solve color mapping issues in svg2svgt for files that have been edited by
# Inkscape.
#
###

###
# Changelog:
# v1.0 by Ian Dunbar
#    - initial release
# v1.1 by Brian Smith
#    - update to work with the latest inkscape version 0.47
# v1.2 by Michael Prager
#    - fix moveto commands with implicit lineto commands in paths
#    - fix gradients that use xlink:href references
###

my $inputfile = "";
while (<>) {
	$inputfile .= $_;
}

sub fixPathData {
	my $output = shift;
	# do some cleanup to make regex easier, add spacing between numbers and commands and replace , with space
	$output =~ s/(\d)-/$1 -/g;
	$output =~ s/(\d)([a-df-zA-DF-Z])/$1 $2/g;
	$output =~ s/([a-df-zA-DF-Z])(-?\d)/$1 $2/g;
	$output =~ s/([a-df-zA-DF-Z])([a-df-zA-DF-Z])/$1 $2/g;
	$output =~ s/,/ /g;
	# fix moveto commands that have more than two coordinates. Interpret additionl coordinates as lineto commands as defined in the SVG DTD
	$output =~ s/^m\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s*/M $1 $2 l $3 $4 /g;
	$output =~ s/m\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s*/m $1 $2 l $3 $4 /g;
	$output =~ s/M\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s+([-0-9e\.]+)\s*/M $1 $2 L $3 $4 /g;
	return $output;
}

sub fixGradient {
	my $output = shift;
	$output =~ s/^#//;
	# dereference linked gradients
	if ($inputfile =~ /<(linearGradient|radialGradient)([^<>]*?)id=\"$output\"([^<>]*?)>(.*?)<\/\1>/i) {
		$output = $4;
	}
	else {
		$output = "";
	}
	return $output;
}

# remove linebreaks & whitespaces, makes parsing easier
$inputfile =~ s/\n/ /g;
$inputfile =~ s/ +/ /g;

# remove style attribute used by Inkscape, use normal attributes instead
while($inputfile =~ /\s+style=\"(.*?)\"/g) {
	my $input = $1;
	my $output = "";
	my @styles = split /;/,$input;
	foreach $style (@styles) {
		$style =~ s/(.*):(.*)/$1=\"$2\" /;
		if ($style !~ /^-/) { # don't allow attributes that start with "-"
			$output .= $style;
		}
	}
	$inputfile =~ s/\s+style=\"\Q$input\E\"/ $output/g;
}

# fix moveto command in paths if implicit lineto commands are used
$inputfile =~ s/<path([^<>]*?)\s+d=\"(.*?)\"/'<path' . $1 . ' d="' . fixPathData($2) . '"'/eg;

# fix linearGradiants that use references
$inputfile =~ s/<(linearGradient|radialGradient)([^<>]*?)\s*xlink:href="(.*?)"([^<>]*?)\/>/"<$1$2 $4>".fixGradient($3)."<\/$1>"/ieg;

# restore "human readable" file layout
$inputfile =~ s/<(.*?)>/<$1>\n/g;
$inputfile =~ s/\n +/\n/g;

print $inputfile;
