user32/tests: Run the tests again on Win95.
[wine/hacks.git] / tools / buildicon
blobeae13dc3a9dfac1c173a809b8b9d0c3c3ee54f5b
1 #! /usr/bin/perl -w
3 # Render SVG files containing multiple images
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::Parser;
24 use MIME::Base64;
26 # Parse the parameters
27 my $svgFileName = $ARGV[0];
28 my $icoFileName = $ARGV[1];
29 die "Cannot open SVG file" unless defined($svgFileName);
30 die "Cannot open ICO file" unless defined($icoFileName);
32 my $renderedSVGFileName = "$svgFileName.ico";
33 $icoFileName =~ m/(.*)\.ico/;
34 my $icoName = $1;
36 my @pngFiles;
38 # Get the programs from the environment variables
39 my $convert = $ENV{"CONVERT"} || "convert";
40 my $rsvg = $ENV{"RSVG"} || "rsvg";
41 my $icotool = $ENV{"ICOTOOL"} || "icotool";
43 sub cleanup()
45 unlink $renderedSVGFileName;
46 unlink $_ foreach(@pngFiles);
49 $SIG{"INT"} = "cleanup";
50 $SIG{"HUP"} = "cleanup";
51 $SIG{"TERM"} = "cleanup";
52 $SIG{"__DIE__"} = "cleanup";
54 # run a shell command and die on error
55 sub shell(@)
57 my @args = @_;
58 system(@args) == 0 or die "@args failed: $?";
61 sub svg_element_start
63 my($expat, $element, %attr) = @_;
65 # Parse the id for icon format
66 my $id = $attr{'id'};
67 return unless defined($id);
68 return unless $id =~ /icon:(\d*)-(\d*)/;
69 my $size = $1;
70 my $depth = $2;
71 return unless defined($size) and defined($depth);
73 warn "Unexpected icon depth" unless
74 $depth == 4 or $depth == 8 or $depth == 32;
75 my $pngFileName = "$icoName-$size-$depth.png";
77 if($element eq "rect") {
79 # Extract SVG vector images
80 my $x = $attr{'x'};
81 my $y = $attr{'y'};
82 my $width = $attr{'width'};
83 my $height = $attr{'height'};
85 if(defined($x) and defined($x)) {
86 if($x =~ /\d*/ and $y =~ /\d*/) {
87 shell $convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", $pngFileName;
91 } elsif($element eq "image" ) {
93 # Extract Base64 encoded PNG data to files
94 my $xlinkHref = $attr{'xlink:href'};
95 if(defined($xlinkHref)) {
96 $xlinkHref =~ /data:image\/png;base64(.*)/;
97 my $imageEncodedData = $1;
98 if(defined $imageEncodedData) {
99 open(FILE, '>' . $pngFileName) or die "$!";
100 print FILE decode_base64($imageEncodedData);
101 close FILE;
104 } else {
105 return;
108 push(@pngFiles, $pngFileName);
111 # Render the SVG image
112 shell $rsvg, $svgFileName, $renderedSVGFileName;
114 # Render the images in the SVG
115 my $parser = new XML::Parser(
116 Handlers => {Start => \&svg_element_start});
117 $parser->parsefile("$svgFileName");
119 # Die if no render directives were found
120 die "No render directives found in icon" unless(@pngFiles);
122 # Combine them into an ICO file
123 shell $icotool, "-c", "-o", $icoFileName, @pngFiles;
125 # Delete the intermediate images
126 unlink $renderedSVGFileName;
127 unlink $_ foreach(@pngFiles);