rundll32: Check more heap allocation failure paths for consistency.
[wine] / tools / buildicon
1 #! /usr/bin/perl -w
2 #
3 # Render SVG files containing multiple images
4 #
5 # Copyright (C) 2010 Joel Holdsworth
6 #
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.
11 #
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.
16 #
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
20
21 use strict;
22 use warnings;
23 use XML::Parser;
24 use MIME::Base64;
25
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);
31
32 my $renderedSVGFileName = "$svgFileName.ico";
33 $icoFileName =~ m/(.*)\.ico/;
34 my $icoName = $1;
35
36 my @pngFiles;
37
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";
42
43 sub cleanup()
44 {
45     unlink $renderedSVGFileName;
46     unlink $_ foreach(@pngFiles);
47 }
48
49 $SIG{"INT"} = "cleanup";
50 $SIG{"HUP"} = "cleanup";
51 $SIG{"TERM"} = "cleanup";
52 $SIG{"__DIE__"} = "cleanup";
53
54 # run a shell command and die on error
55 sub shell(@)
56 {
57     my @args = @_;
58     system(@args) == 0 or die "@args failed: $?";
59 }
60
61 sub svg_element_start
62 {
63     my($expat, $element, %attr) = @_;
64
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);
72
73     warn "Unexpected icon depth" unless
74         $depth == 4 or $depth == 8 or $depth == 32;
75     my $pngFileName = "$icoName-$size-$depth.png";
76
77     if($element eq "rect") {
78
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'};
84
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;
88             }
89         }
90
91     } elsif($element eq "image" ) {
92
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;
102             }
103         }
104     } else {
105         return;
106     }
107
108     push(@pngFiles, $pngFileName);
109 }
110
111 # Render the SVG image
112 shell $rsvg, $svgFileName, $renderedSVGFileName;
113
114 # Render the images in the SVG
115 my $parser = new XML::Parser(
116     Handlers => {Start => \&svg_element_start});
117 $parser->parsefile("$svgFileName");
118
119 # Die if no render directives were found
120 die "No render directives found in icon" unless(@pngFiles);
121
122 # Combine them into an ICO file
123 shell $icotool, "-c", "-o", $icoFileName, @pngFiles;
124
125 # Delete the intermediate images
126 unlink $renderedSVGFileName;
127 unlink $_ foreach(@pngFiles);