buildimage 4.82 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 23 24
#
# 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::Parser;
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 37
$outFileName =~ m/(.*)\.(.*)/;
my $outName = $1;
my $ext = lc($2);
die "Only BMP and ICO outputs are supported" unless $ext eq "bmp" or $ext eq "ico";
38

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

# Get the programs from the environment variables
44 45 46 47
my $convert = $ENV{"CONVERT"} || "convert";
my $rsvg = $ENV{"RSVG"} || "rsvg";
my $icotool = $ENV{"ICOTOOL"} || "icotool";

48
# Be ready to abort
49 50 51 52
sub cleanup()
{
    unlink $renderedSVGFileName;
    unlink $_ foreach(@pngFiles);
53
    unlink $_ foreach(@pngFilesRaw);
54 55 56 57 58 59 60 61 62 63 64 65 66
}

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

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

sub svg_element_start
{
    my($expat, $element, %attr) = @_;

72
    # Parse the id for icon/bitmap render directives
73 74
    my $id = $attr{'id'};
    return unless defined($id);
75 76 77

    my $size = 0;
    my $depth = 0;
78 79
    my $width = 0;
    my $height = 0;
80 81 82 83 84 85 86 87 88 89 90

    if($ext eq "ico") {
        return unless $id =~ /icon:(\d*)-(\d*)/;
        $size = $1;
        $depth = $2;
    } elsif($ext eq "bmp") {
        return unless $id =~ /bitmap:(\d*)-(\d*)/;
        $size = $1;
        $depth = $2;
    }

91 92 93
    return unless defined($size) and defined($depth);

    warn "Unexpected icon depth" unless
94 95
        $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
    my $pngFileName = "$outName-$size-$depth.png";
96

97 98 99 100 101 102
    if($element eq "svg") {

        # The whole file is tagged
        copy($renderedSVGFileName, $pngFileName) or die "File could not be copied";

    } elsif($element eq "rect") {
103 104 105 106

        # Extract SVG vector images
        my $x = $attr{'x'};
        my $y = $attr{'y'};
107 108
        $width = $attr{'width'};
        $height = $attr{'height'};
109 110 111

        if(defined($x) and defined($x)) {
            if($x =~ /\d*/ and $y =~ /\d*/) {
112
                shell $convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", $pngFileName;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
            }
        }

    } elsif($element eq "image" ) {

        # Extract Base64 encoded PNG data to files
        my $xlinkHref = $attr{'xlink:href'};
        if(defined($xlinkHref)) {
            $xlinkHref =~ /data:image\/png;base64(.*)/;
            my $imageEncodedData = $1;
            if(defined $imageEncodedData) {
                open(FILE, '>' . $pngFileName) or die "$!";
                print FILE decode_base64($imageEncodedData);
                close FILE;
            }
        }
    } else {
        return;
    }

133 134 135 136 137 138 139 140
    if ($width >= 128 && $height >= 128)
    {
        push(@pngFilesRaw, $pngFileName);
    }
    else
    {
        push(@pngFiles, $pngFileName);
    }
141 142 143
}

# Render the SVG image
144
shell $rsvg, $svgFileName, $renderedSVGFileName;
145 146 147 148 149 150

# Render the images in the SVG
my $parser = new XML::Parser(
    Handlers => {Start => \&svg_element_start});
$parser->parsefile("$svgFileName");

151
# If no render directives were found, take the full image as-is
152
unless (@pngFiles || @pngFilesRaw) {
153 154 155 156 157 158 159 160 161
    my $pngFileName = "bmp$renderedSVGFileName";
    copy($renderedSVGFileName, $pngFileName) or die "File could not be copied";
    push(@pngFiles, $pngFileName);
}

# Combine the renderings into the output file
if($ext eq "ico") {

    # Place images into the ICO
162
    shell $icotool, "-c", "-o", $outFileName, @pngFiles, map { "--raw=$_"; } @pngFilesRaw;
163 164 165 166 167 168 169

} elsif($ext eq "bmp") {

    # Only the first image becomes the final BMP
    my $pngFile = $pngFiles[0];
    $pngFile =~ /.*-\d*-(\d*)\.png/;
    my $depth = $1;
170

171 172 173 174 175 176 177 178
    # Convert it into a bmp
    if($depth == 24) {
        shell $convert, "png:$pngFile", "+matte", $outFileName;
    } else {
        shell $convert, "png:$pngFile", $outFileName;
    }

}
179 180

# Delete the intermediate images
181
cleanup();