midimap: Handle MIDI running status.
[wine.git] / tools / buildimage
blobecc5be31898405de4af0f5cc02d61c518ad945c9
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 $ENV{"SOURCE_DATE_EPOCH"} ||= "0"; # for reproducible builds
45 # Get the programs from the environment variables
46 my $convert = $ENV{"CONVERT"} || "convert";
47 my $rsvg = $ENV{"RSVG"} || "rsvg-convert";
48 my @icotool_args = ($ENV{"ICOTOOL"} || "icotool", "--create",
49 $ext eq "cur" ? "--cursor" : "--icon", "-o", $outFileName);
51 # Be ready to abort
52 sub cleanup()
54 unlink $renderedSVGFileName;
55 unlink $_ foreach(@pngFiles);
58 $SIG{"INT"} = "cleanup";
59 $SIG{"HUP"} = "cleanup";
60 $SIG{"TERM"} = "cleanup";
61 $SIG{"__DIE__"} = "cleanup";
63 my %label =
65 'ico' => 'icon:(\d*)-(\d*)',
66 'cur' => 'cursor:(\d*)-(\d*)',
67 'bmp' => 'bitmap:(\d*)-(\d*)',
70 # run a shell command and die on error
71 sub shell(@)
73 my @args = @_;
74 system(@args) == 0 or die "@args failed: $?";
77 # add an image to the icon/cursor
78 sub add_image($$$)
80 my ($file, $size, $depth) = @_;
82 if (defined $hotspot[$size])
84 my @coords = @{$hotspot[$size]};
85 push @icotool_args, "--hotspot-x=$coords[0]", "--hotspot-y=$coords[1]";
87 push @icotool_args, ($size >= 128 && $depth >= 24) ? "--raw=$file" : $file;
88 push @pngFiles, $file;
91 # Render the SVG image
92 my @rsvgCmd;
93 push(@rsvgCmd, $rsvg);
94 push(@rsvgCmd, $svgFileName);
95 push(@rsvgCmd, "-o") if ($rsvg eq "rsvg-convert");
96 push(@rsvgCmd, $renderedSVGFileName);
97 shell @rsvgCmd;
99 # Render the images in the SVG
100 my $xml = XML::LibXML->load_xml( location => $svgFileName );
101 my $xc = XML::LibXML::XPathContext->new($xml);
102 $xc->registerNs('x', 'http://www.w3.org/2000/svg');
104 if ($ext eq "bmp")
106 foreach my $element ($xc->findnodes("/x:svg"))
108 next unless $element->{id} =~ /bitmap:(\d*)-(\d*)/;
109 my $size = $1;
110 my $depth = $2;
111 if ($depth == 24) {
112 shell $convert, $renderedSVGFileName, "+matte", $outFileName;
113 } else {
114 shell $convert, $renderedSVGFileName, $outFileName;
116 cleanup();
117 exit(0);
121 # fetch hotspot rectangles for the various sizes
123 if ($ext eq "cur")
125 foreach my $element ($xc->findnodes("/x:svg/x:rect"))
127 next unless $element->{id} =~ /hotspot:(\d*)/;
128 $hotspot[$1] = [ $element->{x}, $element->{y} ];
132 # extract rectangles from the rendered svg
134 foreach my $element ($xc->findnodes("/x:svg/*[\@id]"))
136 next unless $element->{id} =~ /$label{$ext}/;
137 my $size = $1;
138 my $depth = $2;
139 warn "Unexpected depth" unless
140 $depth == 1 or $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
141 my $file = "$outName-$size-$depth.png";
143 my $x = $element->{x};
144 my $y = $element->{y};
145 my $width = $element->{width};
146 my $height = $element->{height};
147 if ($element->{'xlink:href'})
149 # extract base64-encoded png image
150 (my $data = $element->{'xlink:href'}) =~ s/data:image\/png;base64//;
151 open FILE, ">$file" or die "$!";
152 print FILE decode_base64($data);
153 close FILE;
155 else
157 shell $convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", "-depth", $depth, $file;
159 add_image( $file, $size, $depth );
162 die "no render directive found in $svgFileName" unless @pngFiles;
164 shell @icotool_args;
166 # Delete the intermediate images
167 cleanup();