msxml3: Register classes.
[wine/multimedia.git] / tools / buildicon
blob4c6a0888da6638da4796d7af2a0c82239f749ce4
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'};
83 if(defined($x) and defined($x)) {
84 if($x =~ /\d*/ and $y =~ /\d*/) {
85 shell $convert, $renderedSVGFileName, "-crop", "${size}x${size}+$x+$y", $pngFileName;
89 } elsif($element eq "image" ) {
91 # Extract Base64 encoded PNG data to files
92 my $xlinkHref = $attr{'xlink:href'};
93 if(defined($xlinkHref)) {
94 $xlinkHref =~ /data:image\/png;base64(.*)/;
95 my $imageEncodedData = $1;
96 if(defined $imageEncodedData) {
97 open(FILE, '>' . $pngFileName) or die "$!";
98 print FILE decode_base64($imageEncodedData);
99 close FILE;
102 } else {
103 return;
106 push(@pngFiles, $pngFileName);
109 sub resize_image
111 # Use ImageMagick to stretch the image
112 my($size) = @_;
113 my $pngFileName = "$icoName-$size.png";
114 shell $convert, $renderedSVGFileName, "-resize", "${size}x${size}", $pngFileName;
115 push(@pngFiles, $pngFileName);
118 sub fallback_render
120 resize_image(16);
121 resize_image(32);
122 resize_image(48);
125 # Render the SVG image
126 shell 'rsvg', $svgFileName, $renderedSVGFileName;
128 # Render the images in the SVG
129 my $parser = new XML::Parser(
130 Handlers => {Start => \&svg_element_start});
131 $parser->parsefile("$svgFileName");
133 # If no render directives were found, this is an old-style icon
134 # which should be rendered with the old build rule
135 fallback_render unless(@pngFiles);
137 # Combine them into an ICO file
138 shell $icotool, "-c", "-o", $icoFileName, @pngFiles;
140 # Delete the intermediate images
141 unlink $renderedSVGFileName;
142 unlink $_ foreach(@pngFiles);