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 $ENV{"SOURCE_DATE_EPOCH"} ||= "0"; # for reproducible builds
45 # Get the programs from the environment variables
46 my $convert = $ENV{"CONVERT"} || "convert";
47 my $rsvg = $ENV{"RSVG"} || "rsvg-convert";
48 my @icotool_args = ($ENV{"ICOTOOL"} || "icotool", "--create",
49 $ext eq "cur" ?
"--cursor" : "--icon", "-o", $outFileName);
54 unlink $renderedSVGFileName;
55 unlink $_ foreach(@pngFiles);
58 $SIG{"INT"} = "cleanup";
59 $SIG{"HUP"} = "cleanup";
60 $SIG{"TERM"} = "cleanup";
61 $SIG{"__DIE__"} = "cleanup";
65 'ico' => 'icon:(\d*)-(\d*)',
66 'cur' => 'cursor:(\d*)-(\d*)',
67 'bmp' => 'bitmap:(\d*)-(\d*)',
70 # run a shell command and die on error
74 system(@args) == 0 or die "@args failed: $?";
77 # add an image to the icon/cursor
80 my ($file, $size, $depth) = @_;
82 if (defined $hotspot[$size])
84 my @coords = @
{$hotspot[$size]};
85 push @icotool_args, "--hotspot-x=$coords[0]", "--hotspot-y=$coords[1]";
87 push @icotool_args, ($size >= 128 && $depth >= 24) ?
"--raw=$file" : $file;
88 push @pngFiles, $file;
91 # Render the SVG image
93 push(@rsvgCmd, $rsvg);
94 push(@rsvgCmd, $svgFileName);
95 push(@rsvgCmd, "-o") if ($rsvg eq "rsvg-convert");
96 push(@rsvgCmd, $renderedSVGFileName);
99 # Render the images in the SVG
100 my $xml = XML
::LibXML
->load_xml( location
=> $svgFileName );
101 my $xc = XML
::LibXML
::XPathContext
->new($xml);
102 $xc->registerNs('x', 'http://www.w3.org/2000/svg');
106 foreach my $element ($xc->findnodes("/x:svg"))
108 next unless $element->{id
} =~ /bitmap:(\d*)-(\d*)/;
112 shell
$convert, $renderedSVGFileName, "+matte", $outFileName;
114 shell
$convert, $renderedSVGFileName, $outFileName;
121 # fetch hotspot rectangles for the various sizes
125 foreach my $element ($xc->findnodes("/x:svg/x:rect"))
127 next unless $element->{id
} =~ /hotspot:(\d*)/;
128 $hotspot[$1] = [ $element->{x
}, $element->{y
} ];
132 # extract rectangles from the rendered svg
134 foreach my $element ($xc->findnodes("/x:svg/*[\@id]"))
136 next unless $element->{id
} =~ /$label{$ext}/;
139 warn "Unexpected depth" unless
140 $depth == 1 or $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
141 my $file = "$outName-$size-$depth.png";
143 my $x = $element->{x
};
144 my $y = $element->{y
};
145 my $width = $element->{width
};
146 my $height = $element->{height
};
147 if ($element->{'xlink:href'})
149 # extract base64-encoded png image
150 (my $data = $element->{'xlink:href'}) =~ s/data:image\/png;base64//;
151 open FILE
, ">$file" or die "$!";
152 print FILE decode_base64
($data);
157 shell
$convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", "-depth", $depth, $file;
159 add_image
( $file, $size, $depth );
162 die "no render directive found in $svgFileName" unless @pngFiles;
166 # Delete the intermediate images