3 # Render SVG files containing multiple images
5 # Copyright (C) 2010 Joel Holdsworth
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 # Parse the parameters
27 my $svgFileName = $ARGV[0];
28 my $icoFileName = $ARGV[1];
29 die "Cannot open SVG file" unless defined($svgFileName);
30 die "Cannot open ICO file" unless defined($icoFileName);
32 my $renderedSVGFileName = "$svgFileName.ico";
33 $icoFileName =~ m/(.*)\.ico/;
38 # Get the programs from the environment variables
39 my $convert = $ENV{"CONVERT"} || "convert";
40 my $rsvg = $ENV{"RSVG"} || "rsvg";
41 my $icotool = $ENV{"ICOTOOL"} || "icotool";
45 unlink $renderedSVGFileName;
46 unlink $_ foreach(@pngFiles);
49 $SIG{"INT"} = "cleanup";
50 $SIG{"HUP"} = "cleanup";
51 $SIG{"TERM"} = "cleanup";
52 $SIG{"__DIE__"} = "cleanup";
54 # run a shell command and die on error
58 system(@args) == 0 or die "@args failed: $?";
63 my($expat, $element, %attr) = @_;
65 # Parse the id for icon format
67 return unless defined($id);
68 return unless $id =~ /icon:(\d*)-(\d*)/;
71 return unless defined($size) and defined($depth);
73 warn "Unexpected icon depth" unless
74 $depth == 4 or $depth == 8 or $depth == 32;
75 my $pngFileName = "$icoName-$size-$depth.png";
77 if($element eq "rect") {
79 # Extract SVG vector images
82 my $width = $attr{'width'};
83 my $height = $attr{'height'};
85 if(defined($x) and defined($x)) {
86 if($x =~ /\d*/ and $y =~ /\d*/) {
87 shell
$convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", $pngFileName;
91 } elsif($element eq "image" ) {
93 # Extract Base64 encoded PNG data to files
94 my $xlinkHref = $attr{'xlink:href'};
95 if(defined($xlinkHref)) {
96 $xlinkHref =~ /data:image\/png
;base64
(.*)/;
97 my $imageEncodedData = $1;
98 if(defined $imageEncodedData) {
99 open(FILE
, '>' . $pngFileName) or die "$!";
100 print FILE decode_base64
($imageEncodedData);
108 push(@pngFiles, $pngFileName);
111 # Render the SVG image
112 shell
$rsvg, $svgFileName, $renderedSVGFileName;
114 # Render the images in the SVG
115 my $parser = new XML
::Parser
(
116 Handlers
=> {Start
=> \
&svg_element_start
});
117 $parser->parsefile("$svgFileName");
119 # Die if no render directives were found
120 die "No render directives found in icon" unless(@pngFiles);
122 # Combine them into an ICO file
123 shell
$icotool, "-c", "-o", $icoFileName, @pngFiles;
125 # Delete the intermediate images
126 unlink $renderedSVGFileName;
127 unlink $_ foreach(@pngFiles);