2010-04-05 12:15:08 +02:00
|
|
|
#! /usr/bin/perl -w
|
|
|
|
#
|
2010-05-17 00:07:57 +02:00
|
|
|
# Render SVG files containing one or more images into an ICO or BMP.
|
2010-04-05 12:15:08 +02:00
|
|
|
#
|
|
|
|
# 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;
|
2010-05-17 00:07:57 +02:00
|
|
|
use File::Copy;
|
2010-04-05 12:15:08 +02:00
|
|
|
|
|
|
|
# Parse the parameters
|
|
|
|
my $svgFileName = $ARGV[0];
|
2010-05-17 00:07:57 +02:00
|
|
|
my $outFileName = $ARGV[1];
|
|
|
|
|
2010-04-05 12:15:08 +02:00
|
|
|
die "Cannot open SVG file" unless defined($svgFileName);
|
2010-05-17 00:07:57 +02:00
|
|
|
die "Cannot open output file" unless defined($outFileName);
|
2010-04-05 12:15:08 +02:00
|
|
|
|
2010-05-17 00:07:57 +02:00
|
|
|
$outFileName =~ m/(.*)\.(.*)/;
|
|
|
|
my $outName = $1;
|
|
|
|
my $ext = lc($2);
|
2017-07-14 11:58:54 +02:00
|
|
|
die "Only BMP, ICO and CUR outputs are supported" unless $ext eq "bmp" or $ext eq "ico" or $ext eq "cur";
|
2010-04-05 12:15:08 +02:00
|
|
|
|
2010-05-17 00:07:57 +02:00
|
|
|
my $renderedSVGFileName = "$svgFileName.png";
|
2010-04-05 12:15:08 +02:00
|
|
|
my @pngFiles;
|
2017-10-17 16:14:28 +02:00
|
|
|
my @hotspot;
|
2010-04-05 12:15:08 +02:00
|
|
|
|
|
|
|
# Get the programs from the environment variables
|
2010-04-06 13:24:04 +02:00
|
|
|
my $convert = $ENV{"CONVERT"} || "convert";
|
|
|
|
my $rsvg = $ENV{"RSVG"} || "rsvg";
|
2017-07-14 11:58:54 +02:00
|
|
|
my @icotool_args = ($ENV{"ICOTOOL"} || "icotool", "--create",
|
|
|
|
$ext eq "cur" ? "--cursor" : "--icon", "-o", $outFileName);
|
2010-04-06 13:24:04 +02:00
|
|
|
|
2010-05-17 00:07:57 +02:00
|
|
|
# Be ready to abort
|
2010-04-06 13:24:04 +02:00
|
|
|
sub cleanup()
|
|
|
|
{
|
|
|
|
unlink $renderedSVGFileName;
|
|
|
|
unlink $_ foreach(@pngFiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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: $?";
|
|
|
|
}
|
2010-04-05 12:15:08 +02:00
|
|
|
|
|
|
|
sub svg_element_start
|
|
|
|
{
|
|
|
|
my($expat, $element, %attr) = @_;
|
|
|
|
|
2010-05-17 00:07:57 +02:00
|
|
|
# Parse the id for icon/bitmap render directives
|
2010-04-05 12:15:08 +02:00
|
|
|
my $id = $attr{'id'};
|
|
|
|
return unless defined($id);
|
2010-05-17 00:07:57 +02:00
|
|
|
|
|
|
|
my $size = 0;
|
|
|
|
my $depth = 0;
|
2013-03-27 13:44:18 +01:00
|
|
|
my $width = 0;
|
|
|
|
my $height = 0;
|
2010-05-17 00:07:57 +02:00
|
|
|
|
2017-10-17 16:14:28 +02:00
|
|
|
if ($id =~ /hotspot:(\d*)/)
|
2017-07-14 11:58:54 +02:00
|
|
|
{
|
2017-10-17 16:14:28 +02:00
|
|
|
$hotspot[$1] = [ $attr{x}, $attr{y} ];
|
2017-07-14 11:58:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-05-17 00:07:57 +02:00
|
|
|
if($ext eq "ico") {
|
|
|
|
return unless $id =~ /icon:(\d*)-(\d*)/;
|
|
|
|
$size = $1;
|
|
|
|
$depth = $2;
|
2017-07-14 11:58:54 +02:00
|
|
|
} elsif($ext eq "cur") {
|
|
|
|
return unless $id =~ /cursor:(\d*)-(\d*)/;
|
|
|
|
$size = $1;
|
|
|
|
$depth = $2;
|
2010-05-17 00:07:57 +02:00
|
|
|
} elsif($ext eq "bmp") {
|
|
|
|
return unless $id =~ /bitmap:(\d*)-(\d*)/;
|
|
|
|
$size = $1;
|
|
|
|
$depth = $2;
|
|
|
|
}
|
|
|
|
|
2010-04-05 12:15:08 +02:00
|
|
|
return unless defined($size) and defined($depth);
|
|
|
|
|
2017-07-14 11:58:54 +02:00
|
|
|
warn "Unexpected depth" unless
|
|
|
|
$depth == 1 or $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
|
2010-05-17 00:07:57 +02:00
|
|
|
my $pngFileName = "$outName-$size-$depth.png";
|
2010-04-05 12:15:08 +02:00
|
|
|
|
2010-05-17 00:07:57 +02:00
|
|
|
if($element eq "svg") {
|
|
|
|
|
2017-07-14 11:58:54 +02:00
|
|
|
if ($ext eq "bmp") {
|
|
|
|
if ($depth == 24) {
|
|
|
|
shell $convert, $renderedSVGFileName, "+matte", $outFileName;
|
|
|
|
} else {
|
|
|
|
shell $convert, $renderedSVGFileName, $outFileName;
|
|
|
|
}
|
|
|
|
cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2010-05-17 00:07:57 +02:00
|
|
|
# The whole file is tagged
|
2017-07-14 11:58:54 +02:00
|
|
|
$pngFileName = $renderedSVGFileName;
|
2010-05-17 00:07:57 +02:00
|
|
|
|
|
|
|
} elsif($element eq "rect") {
|
2010-04-05 12:15:08 +02:00
|
|
|
|
|
|
|
# Extract SVG vector images
|
|
|
|
my $x = $attr{'x'};
|
|
|
|
my $y = $attr{'y'};
|
2013-03-27 13:44:18 +01:00
|
|
|
$width = $attr{'width'};
|
|
|
|
$height = $attr{'height'};
|
2010-04-05 12:15:08 +02:00
|
|
|
|
2015-01-23 16:14:21 +01:00
|
|
|
if(defined($x) and defined($y)) {
|
2010-04-05 12:15:08 +02:00
|
|
|
if($x =~ /\d*/ and $y =~ /\d*/) {
|
2017-07-14 11:58:54 +02:00
|
|
|
shell $convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", "-depth", $depth, $pngFileName;
|
2010-04-05 12:15:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
2017-10-17 16:14:28 +02:00
|
|
|
if (defined $hotspot[$size])
|
|
|
|
{
|
|
|
|
my @coords = @{$hotspot[$size]};
|
|
|
|
push @icotool_args, "--hotspot-x=$coords[0]", "--hotspot-y=$coords[1]";
|
|
|
|
}
|
|
|
|
|
2013-03-27 13:44:18 +01:00
|
|
|
if ($width >= 128 && $height >= 128)
|
|
|
|
{
|
2017-07-14 11:58:54 +02:00
|
|
|
push @icotool_args, "--raw=$pngFileName";
|
2013-03-27 13:44:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-07-14 11:58:54 +02:00
|
|
|
push @icotool_args, $pngFileName;
|
2013-03-27 13:44:18 +01:00
|
|
|
}
|
2017-07-14 11:58:54 +02:00
|
|
|
push @pngFiles, $pngFileName;
|
2010-04-05 12:15:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Render the SVG image
|
2015-01-23 17:32:36 +01:00
|
|
|
my @rsvgCmd;
|
|
|
|
push(@rsvgCmd, $rsvg);
|
|
|
|
push(@rsvgCmd, $svgFileName);
|
|
|
|
push(@rsvgCmd, "-o") if ($rsvg eq "rsvg-convert");
|
|
|
|
push(@rsvgCmd, $renderedSVGFileName);
|
|
|
|
shell @rsvgCmd;
|
2010-04-05 12:15:08 +02:00
|
|
|
|
|
|
|
# Render the images in the SVG
|
|
|
|
my $parser = new XML::Parser(
|
|
|
|
Handlers => {Start => \&svg_element_start});
|
|
|
|
$parser->parsefile("$svgFileName");
|
|
|
|
|
2017-07-14 11:58:54 +02:00
|
|
|
die "no render directive found in $svgFileName" unless @pngFiles;
|
2010-04-05 12:15:08 +02:00
|
|
|
|
2017-07-14 11:58:54 +02:00
|
|
|
shell @icotool_args;
|
2010-04-05 12:15:08 +02:00
|
|
|
|
|
|
|
# Delete the intermediate images
|
2010-05-17 00:07:57 +02:00
|
|
|
cleanup();
|