buildimage 4.46 KB
Newer Older
1 2
#! /usr/bin/perl -w
#
3
# Render SVG files containing one or more images into an ICO or BMP.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# 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;
23
use XML::LibXML;
24
use MIME::Base64;
25
use File::Copy;
26 27 28

# Parse the parameters
my $svgFileName = $ARGV[0];
29 30
my $outFileName = $ARGV[1];

31
die "Cannot open SVG file" unless defined($svgFileName);
32
die "Cannot open output file" unless defined($outFileName);
33

34 35 36
$outFileName =~ m/(.*)\.(.*)/;
my $outName = $1;
my $ext = lc($2);
37
die "Only BMP, ICO and CUR outputs are supported" unless $ext eq "bmp" or $ext eq "ico" or $ext eq "cur";
38

39
my $renderedSVGFileName = "$svgFileName.png";
40
my @pngFiles;
41
my @hotspot;
42 43

# Get the programs from the environment variables
44
my $convert = $ENV{"CONVERT"} || "convert";
45
my $rsvg = $ENV{"RSVG"} || "rsvg-convert";
46 47
my @icotool_args = ($ENV{"ICOTOOL"} || "icotool", "--create",
                    $ext eq "cur" ? "--cursor" : "--icon", "-o", $outFileName);
48

49
# Be ready to abort
50 51 52 53 54 55 56 57 58 59 60
sub cleanup()
{
    unlink $renderedSVGFileName;
    unlink $_ foreach(@pngFiles);
}

$SIG{"INT"} = "cleanup";
$SIG{"HUP"} = "cleanup";
$SIG{"TERM"} = "cleanup";
$SIG{"__DIE__"} = "cleanup";

61 62 63 64 65 66 67
my %label =
(
 'ico' => 'icon:(\d*)-(\d*)',
 'cur' => 'cursor:(\d*)-(\d*)',
 'bmp' => 'bitmap:(\d*)-(\d*)',
);

68 69 70 71 72 73
# run a shell command and die on error
sub shell(@)
{
    my @args = @_;
    system(@args) == 0 or die "@args failed: $?";
}
74

75 76
# add an image to the icon/cursor
sub add_image($$)
77
{
78 79 80 81 82 83 84 85 86 87
    my ($file, $size) = @_;
    
    if (defined $hotspot[$size])
    {
        my @coords = @{$hotspot[$size]};
        push @icotool_args, "--hotspot-x=$coords[0]", "--hotspot-y=$coords[1]";
    }
    push @icotool_args, $size >= 128 ? "--raw=$file" : $file;
    push @pngFiles, $file;
}
88

89 90 91 92 93 94 95
# 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;
96

97 98 99 100
# 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');
101

102 103 104
if ($ext eq "bmp")
{
    foreach my $element ($xc->findnodes("/x:svg"))
105
    {
106 107 108 109 110 111 112 113 114 115
        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);
116
    }
117
}
118

119 120 121 122 123 124 125 126
# 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} ];
127
    }
128
}
129

130
# extract rectangles from the rendered svg
131

132 133 134 135 136
foreach my $element ($xc->findnodes("/x:svg/*[\@id]"))
{
    next unless $element->{id} =~ /$label{$ext}/;
    my $size = $1;
    my $depth = $2;
137 138
    warn "Unexpected depth" unless
        $depth == 1 or $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
139
    my $file = "$outName-$size-$depth.png";
140

141 142 143 144 145
    my $x = $element->{x};
    my $y = $element->{y};
    my $width = $element->{width};
    my $height = $element->{height};
    if ($element->{'xlink:href'})
146
    {
147 148 149 150 151
        # 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;
152 153 154
    }
    else
    {
155
        shell $convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", "-depth", $depth, $file;
156
    }
157
    add_image( $file, $size );
158 159
}

160
die "no render directive found in $svgFileName" unless @pngFiles;
161

162
shell @icotool_args;
163 164

# Delete the intermediate images
165
cleanup();