123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- #! /usr/bin/perl -w
- #
- # Render SVG files containing one or more images into an ICO or BMP.
- #
- # Copyright (C) 2010 Joel Holdsworth
- #
- # This library is free software; you can redistribute it and/or
- # modify it under the terms of the GNU Lesser General Public
- # License as published by the Free Software Foundation; either
- # version 2.1 of the License, or (at your option) any later version.
- #
- # This library 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
- # Lesser General Public License for more details.
- #
- # You should have received a copy of the GNU Lesser General Public
- # License along with this library; if not, write to the Free Software
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
- use strict;
- use warnings;
- use XML::LibXML;
- use MIME::Base64;
- use File::Copy;
- # Parse the parameters
- my $svgFileName = $ARGV[0];
- my $outFileName = $ARGV[1];
- die "Cannot open SVG file" unless defined($svgFileName);
- die "Cannot open output file" unless defined($outFileName);
- $outFileName =~ m/(.*)\.(.*)/;
- my $outName = $1;
- my $ext = lc($2);
- die "Only BMP, ICO and CUR outputs are supported" unless $ext eq "bmp" or $ext eq "ico" or $ext eq "cur";
- my $renderedSVGFileName = "$svgFileName.png";
- my @pngFiles;
- my @hotspot;
- $ENV{"SOURCE_DATE_EPOCH"} ||= "0"; # for reproducible builds
- # Get the programs from the environment variables
- my $convert = $ENV{"CONVERT"} || "convert";
- my $rsvg = $ENV{"RSVG"} || "rsvg-convert";
- my @icotool_args = ($ENV{"ICOTOOL"} || "icotool", "--create",
- $ext eq "cur" ? "--cursor" : "--icon", "-o", $outFileName);
- # Be ready to abort
- sub cleanup()
- {
- unlink $renderedSVGFileName;
- unlink $_ foreach(@pngFiles);
- }
- $SIG{"INT"} = "cleanup";
- $SIG{"HUP"} = "cleanup";
- $SIG{"TERM"} = "cleanup";
- $SIG{"__DIE__"} = "cleanup";
- my %label =
- (
- 'ico' => 'icon:(\d*)-(\d*)',
- 'cur' => 'cursor:(\d*)-(\d*)',
- 'bmp' => 'bitmap:(\d*)-(\d*)',
- );
- # run a shell command and die on error
- sub shell(@)
- {
- my @args = @_;
- system(@args) == 0 or die "@args failed: $?";
- }
- # add an image to the icon/cursor
- sub add_image($$$)
- {
- my ($file, $size, $depth) = @_;
-
- if (defined $hotspot[$size])
- {
- my @coords = @{$hotspot[$size]};
- push @icotool_args, "--hotspot-x=$coords[0]", "--hotspot-y=$coords[1]";
- }
- push @icotool_args, ($size >= 128 && $depth >= 24) ? "--raw=$file" : $file;
- push @pngFiles, $file;
- }
- # Render the SVG image
- my @rsvgCmd;
- push(@rsvgCmd, $rsvg);
- push(@rsvgCmd, $svgFileName);
- push(@rsvgCmd, "-o") if ($rsvg eq "rsvg-convert");
- push(@rsvgCmd, $renderedSVGFileName);
- shell @rsvgCmd;
- # Render the images in the SVG
- my $xml = XML::LibXML->load_xml( location => $svgFileName );
- my $xc = XML::LibXML::XPathContext->new($xml);
- $xc->registerNs('x', 'http://www.w3.org/2000/svg');
- if ($ext eq "bmp")
- {
- foreach my $element ($xc->findnodes("/x:svg"))
- {
- next unless $element->{id} =~ /bitmap:(\d*)-(\d*)/;
- my $size = $1;
- my $depth = $2;
- if ($depth == 24) {
- shell $convert, $renderedSVGFileName, "+matte", $outFileName;
- } else {
- shell $convert, $renderedSVGFileName, $outFileName;
- }
- cleanup();
- exit(0);
- }
- }
- # fetch hotspot rectangles for the various sizes
- if ($ext eq "cur")
- {
- foreach my $element ($xc->findnodes("/x:svg/x:rect"))
- {
- next unless $element->{id} =~ /hotspot:(\d*)/;
- $hotspot[$1] = [ $element->{x}, $element->{y} ];
- }
- }
- # extract rectangles from the rendered svg
- foreach my $element ($xc->findnodes("/x:svg/*[\@id]"))
- {
- next unless $element->{id} =~ /$label{$ext}/;
- my $size = $1;
- my $depth = $2;
- warn "Unexpected depth" unless
- $depth == 1 or $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
- my $file = "$outName-$size-$depth.png";
- my $x = $element->{x};
- my $y = $element->{y};
- my $width = $element->{width};
- my $height = $element->{height};
- if ($element->{'xlink:href'})
- {
- # extract base64-encoded png image
- (my $data = $element->{'xlink:href'}) =~ s/data:image\/png;base64//;
- open FILE, ">$file" or die "$!";
- print FILE decode_base64($data);
- close FILE;
- }
- else
- {
- shell $convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", "-depth", $depth, $file;
- }
- add_image( $file, $size, $depth );
- }
- die "no render directive found in $svgFileName" unless @pngFiles;
- shell @icotool_args;
- # Delete the intermediate images
- cleanup();
|