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, ICO and CUR outputs are supported" unless $ext eq "bmp" or $ext eq "ico" or $ext eq "cur";
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-convert";
46 my @icotool_args = ($ENV{"ICOTOOL"} || "icotool", "--create",
47 $ext eq "cur" ?
"--cursor" : "--icon", "-o", $outFileName);
52 unlink $renderedSVGFileName;
53 unlink $_ foreach(@pngFiles);
56 $SIG{"INT"} = "cleanup";
57 $SIG{"HUP"} = "cleanup";
58 $SIG{"TERM"} = "cleanup";
59 $SIG{"__DIE__"} = "cleanup";
63 'ico' => 'icon:(\d*)-(\d*)',
64 'cur' => 'cursor:(\d*)-(\d*)',
65 'bmp' => 'bitmap:(\d*)-(\d*)',
68 # run a shell command and die on error
72 system(@args) == 0 or die "@args failed: $?";
75 # add an image to the icon/cursor
78 my ($file, $size) = @_;
80 if (defined $hotspot[$size])
82 my @coords = @
{$hotspot[$size]};
83 push @icotool_args, "--hotspot-x=$coords[0]", "--hotspot-y=$coords[1]";
85 push @icotool_args, $size >= 128 ?
"--raw=$file" : $file;
86 push @pngFiles, $file;
89 # Render the SVG image
91 push(@rsvgCmd, $rsvg);
92 push(@rsvgCmd, $svgFileName);
93 push(@rsvgCmd, "-o") if ($rsvg eq "rsvg-convert");
94 push(@rsvgCmd, $renderedSVGFileName);
97 # Render the images in the SVG
98 my $xml = XML
::LibXML
->load_xml( location
=> $svgFileName );
99 my $xc = XML
::LibXML
::XPathContext
->new($xml);
100 $xc->registerNs('x', 'http://www.w3.org/2000/svg');
104 foreach my $element ($xc->findnodes("/x:svg"))
106 next unless $element->{id
} =~ /bitmap:(\d*)-(\d*)/;
110 shell
$convert, $renderedSVGFileName, "+matte", $outFileName;
112 shell
$convert, $renderedSVGFileName, $outFileName;
119 # fetch hotspot rectangles for the various sizes
123 foreach my $element ($xc->findnodes("/x:svg/x:rect"))
125 next unless $element->{id
} =~ /hotspot:(\d*)/;
126 $hotspot[$1] = [ $element->{x
}, $element->{y
} ];
130 # extract rectangles from the rendered svg
132 foreach my $element ($xc->findnodes("/x:svg/*[\@id]"))
134 next unless $element->{id
} =~ /$label{$ext}/;
137 warn "Unexpected depth" unless
138 $depth == 1 or $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
139 my $file = "$outName-$size-$depth.png";
141 my $x = $element->{x
};
142 my $y = $element->{y
};
143 my $width = $element->{width
};
144 my $height = $element->{height
};
145 if ($element->{'xlink:href'})
147 # extract base64-encoded png image
148 (my $data = $element->{'xlink:href'}) =~ s/data:image\/png;base64//;
149 open FILE
, ">$file" or die "$!";
150 print FILE decode_base64
($data);
155 shell
$convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", "-depth", $depth, $file;
157 add_image
( $file, $size );
160 die "no render directive found in $svgFileName" unless @pngFiles;
164 # Delete the intermediate images