3 # Render SVG files containing one or more images into an ICO or BMP.
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
27 # Parse the parameters
28 my $svgFileName = $ARGV[0];
29 my $outFileName = $ARGV[1];
31 die "Cannot open SVG file" unless defined($svgFileName);
32 die "Cannot open output file" unless defined($outFileName);
34 $outFileName =~ m/(.*)\.(.*)/;
37 die "Only BMP and ICO outputs are supported" unless $ext eq "bmp" or $ext eq "ico";
39 my $renderedSVGFileName = "$svgFileName.png";
43 # Get the programs from the environment variables
44 my $convert = $ENV{"CONVERT"} || "convert";
45 my $rsvg = $ENV{"RSVG"} || "rsvg";
46 my $icotool = $ENV{"ICOTOOL"} || "icotool";
51 unlink $renderedSVGFileName;
52 unlink $_ foreach(@pngFiles);
53 unlink $_ foreach(@pngFilesRaw);
56 $SIG{"INT"} = "cleanup";
57 $SIG{"HUP"} = "cleanup";
58 $SIG{"TERM"} = "cleanup";
59 $SIG{"__DIE__"} = "cleanup";
61 # run a shell command and die on error
65 system(@args) == 0 or die "@args failed: $?";
70 my($expat, $element, %attr) = @_;
72 # Parse the id for icon/bitmap render directives
74 return unless defined($id);
82 return unless $id =~ /icon:(\d*)-(\d*)/;
85 } elsif($ext eq "bmp") {
86 return unless $id =~ /bitmap:(\d*)-(\d*)/;
91 return unless defined($size) and defined($depth);
93 warn "Unexpected icon depth" unless
94 $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
95 my $pngFileName = "$outName-$size-$depth.png";
97 if($element eq "svg") {
99 # The whole file is tagged
100 copy
($renderedSVGFileName, $pngFileName) or die "File could not be copied";
102 } elsif($element eq "rect") {
104 # Extract SVG vector images
107 $width = $attr{'width'};
108 $height = $attr{'height'};
110 if(defined($x) and defined($y)) {
111 if($x =~ /\d*/ and $y =~ /\d*/) {
112 shell
$convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", $pngFileName;
116 } elsif($element eq "image" ) {
118 # Extract Base64 encoded PNG data to files
119 my $xlinkHref = $attr{'xlink:href'};
120 if(defined($xlinkHref)) {
121 $xlinkHref =~ /data:image\/png
;base64
(.*)/;
122 my $imageEncodedData = $1;
123 if(defined $imageEncodedData) {
124 open(FILE
, '>' . $pngFileName) or die "$!";
125 print FILE decode_base64
($imageEncodedData);
133 if ($width >= 128 && $height >= 128)
135 push(@pngFilesRaw, $pngFileName);
139 push(@pngFiles, $pngFileName);
143 # Render the SVG image
145 push(@rsvgCmd, $rsvg);
146 push(@rsvgCmd, $svgFileName);
147 push(@rsvgCmd, "-o") if ($rsvg eq "rsvg-convert");
148 push(@rsvgCmd, $renderedSVGFileName);
151 # Render the images in the SVG
152 my $parser = new XML
::Parser
(
153 Handlers
=> {Start
=> \
&svg_element_start
});
154 $parser->parsefile("$svgFileName");
156 # If no render directives were found, take the full image as-is
157 unless (@pngFiles || @pngFilesRaw) {
158 my $pngFileName = "bmp$renderedSVGFileName";
159 copy
($renderedSVGFileName, $pngFileName) or die "File could not be copied";
160 push(@pngFiles, $pngFileName);
163 # Combine the renderings into the output file
166 # Place images into the ICO
167 shell
$icotool, "-c", "-o", $outFileName, @pngFiles, map { "--raw=$_"; } @pngFilesRaw;
169 } elsif($ext eq "bmp") {
171 # Only the first image becomes the final BMP
172 my $pngFile = $pngFiles[0];
173 $pngFile =~ /.*-\d*-(\d*)\.png/;
176 # Convert it into a bmp
178 shell
$convert, "png:$pngFile", "+matte", $outFileName;
180 shell
$convert, "png:$pngFile", $outFileName;
185 # Delete the intermediate images