prntvpt: Add support for PageOutputColor to ticket XML writer.
[wine.git] / tools / buildimage
blob7075bc917cb009233c80da00d2dfce8dd15ddffb
1 #! /usr/bin/perl -w
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
21 use strict;
22 use warnings;
23 use XML::LibXML;
24 use MIME::Base64;
25 use File::Copy;
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/(.*)\.(.*)/;
35 my $outName = $1;
36 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";
39 my $renderedSVGFileName = "$svgFileName.png";
40 my @pngFiles;
41 my @hotspot;
43 # Get the programs from the environment variables
44 my $convert = $ENV{"CONVERT"} || "convert";
45 my $rsvg = $ENV{"RSVG"} || "rsvg-convert";
46 my @icotool_args = ($ENV{"ICOTOOL"} || "icotool", "--create",
47 $ext eq "cur" ? "--cursor" : "--icon", "-o", $outFileName);
49 # Be ready to abort
50 sub cleanup()
52 unlink $renderedSVGFileName;
53 unlink $_ foreach(@pngFiles);
56 $SIG{"INT"} = "cleanup";
57 $SIG{"HUP"} = "cleanup";
58 $SIG{"TERM"} = "cleanup";
59 $SIG{"__DIE__"} = "cleanup";
61 my %label =
63 'ico' => 'icon:(\d*)-(\d*)',
64 'cur' => 'cursor:(\d*)-(\d*)',
65 'bmp' => 'bitmap:(\d*)-(\d*)',
68 # run a shell command and die on error
69 sub shell(@)
71 my @args = @_;
72 system(@args) == 0 or die "@args failed: $?";
75 # add an image to the icon/cursor
76 sub add_image($$)
78 my ($file, $size) = @_;
80 if (defined $hotspot[$size])
82 my @coords = @{$hotspot[$size]};
83 push @icotool_args, "--hotspot-x=$coords[0]", "--hotspot-y=$coords[1]";
85 push @icotool_args, $size >= 128 ? "--raw=$file" : $file;
86 push @pngFiles, $file;
89 # Render the SVG image
90 my @rsvgCmd;
91 push(@rsvgCmd, $rsvg);
92 push(@rsvgCmd, $svgFileName);
93 push(@rsvgCmd, "-o") if ($rsvg eq "rsvg-convert");
94 push(@rsvgCmd, $renderedSVGFileName);
95 shell @rsvgCmd;
97 # Render the images in the SVG
98 my $xml = XML::LibXML->load_xml( location => $svgFileName );
99 my $xc = XML::LibXML::XPathContext->new($xml);
100 $xc->registerNs('x', 'http://www.w3.org/2000/svg');
102 if ($ext eq "bmp")
104 foreach my $element ($xc->findnodes("/x:svg"))
106 next unless $element->{id} =~ /bitmap:(\d*)-(\d*)/;
107 my $size = $1;
108 my $depth = $2;
109 if ($depth == 24) {
110 shell $convert, $renderedSVGFileName, "+matte", $outFileName;
111 } else {
112 shell $convert, $renderedSVGFileName, $outFileName;
114 cleanup();
115 exit(0);
119 # fetch hotspot rectangles for the various sizes
121 if ($ext eq "cur")
123 foreach my $element ($xc->findnodes("/x:svg/x:rect"))
125 next unless $element->{id} =~ /hotspot:(\d*)/;
126 $hotspot[$1] = [ $element->{x}, $element->{y} ];
130 # extract rectangles from the rendered svg
132 foreach my $element ($xc->findnodes("/x:svg/*[\@id]"))
134 next unless $element->{id} =~ /$label{$ext}/;
135 my $size = $1;
136 my $depth = $2;
137 warn "Unexpected depth" unless
138 $depth == 1 or $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
139 my $file = "$outName-$size-$depth.png";
141 my $x = $element->{x};
142 my $y = $element->{y};
143 my $width = $element->{width};
144 my $height = $element->{height};
145 if ($element->{'xlink:href'})
147 # extract base64-encoded png image
148 (my $data = $element->{'xlink:href'}) =~ s/data:image\/png;base64//;
149 open FILE, ">$file" or die "$!";
150 print FILE decode_base64($data);
151 close FILE;
153 else
155 shell $convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", "-depth", $depth, $file;
157 add_image( $file, $size );
160 die "no render directive found in $svgFileName" unless @pngFiles;
162 shell @icotool_args;
164 # Delete the intermediate images
165 cleanup();