webservices: Fix reader movement for text field mappings.
[wine.git] / tools / buildimage
blob0a7e466870f0b63a41356d3e81825135d98ddeb2
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::Parser;
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 and ICO outputs are supported" unless $ext eq "bmp" or $ext eq "ico";
39 my $renderedSVGFileName = "$svgFileName.png";
40 my @pngFiles;
41 my @pngFilesRaw;
43 # Get the programs from the environment variables
44 my $convert = $ENV{"CONVERT"} || "convert";
45 my $rsvg = $ENV{"RSVG"} || "rsvg";
46 my $icotool = $ENV{"ICOTOOL"} || "icotool";
48 # Be ready to abort
49 sub cleanup()
51 unlink $renderedSVGFileName;
52 unlink $_ foreach(@pngFiles);
53 unlink $_ foreach(@pngFilesRaw);
56 $SIG{"INT"} = "cleanup";
57 $SIG{"HUP"} = "cleanup";
58 $SIG{"TERM"} = "cleanup";
59 $SIG{"__DIE__"} = "cleanup";
61 # run a shell command and die on error
62 sub shell(@)
64 my @args = @_;
65 system(@args) == 0 or die "@args failed: $?";
68 sub svg_element_start
70 my($expat, $element, %attr) = @_;
72 # Parse the id for icon/bitmap render directives
73 my $id = $attr{'id'};
74 return unless defined($id);
76 my $size = 0;
77 my $depth = 0;
78 my $width = 0;
79 my $height = 0;
81 if($ext eq "ico") {
82 return unless $id =~ /icon:(\d*)-(\d*)/;
83 $size = $1;
84 $depth = $2;
85 } elsif($ext eq "bmp") {
86 return unless $id =~ /bitmap:(\d*)-(\d*)/;
87 $size = $1;
88 $depth = $2;
91 return unless defined($size) and defined($depth);
93 warn "Unexpected icon depth" unless
94 $depth == 4 or $depth == 8 or $depth == 24 or $depth == 32;
95 my $pngFileName = "$outName-$size-$depth.png";
97 if($element eq "svg") {
99 # The whole file is tagged
100 copy($renderedSVGFileName, $pngFileName) or die "File could not be copied";
102 } elsif($element eq "rect") {
104 # Extract SVG vector images
105 my $x = $attr{'x'};
106 my $y = $attr{'y'};
107 $width = $attr{'width'};
108 $height = $attr{'height'};
110 if(defined($x) and defined($y)) {
111 if($x =~ /\d*/ and $y =~ /\d*/) {
112 shell $convert, $renderedSVGFileName, "-crop", "${width}x${height}+$x+$y", $pngFileName;
116 } elsif($element eq "image" ) {
118 # Extract Base64 encoded PNG data to files
119 my $xlinkHref = $attr{'xlink:href'};
120 if(defined($xlinkHref)) {
121 $xlinkHref =~ /data:image\/png;base64(.*)/;
122 my $imageEncodedData = $1;
123 if(defined $imageEncodedData) {
124 open(FILE, '>' . $pngFileName) or die "$!";
125 print FILE decode_base64($imageEncodedData);
126 close FILE;
129 } else {
130 return;
133 if ($width >= 128 && $height >= 128)
135 push(@pngFilesRaw, $pngFileName);
137 else
139 push(@pngFiles, $pngFileName);
143 # Render the SVG image
144 my @rsvgCmd;
145 push(@rsvgCmd, $rsvg);
146 push(@rsvgCmd, $svgFileName);
147 push(@rsvgCmd, "-o") if ($rsvg eq "rsvg-convert");
148 push(@rsvgCmd, $renderedSVGFileName);
149 shell @rsvgCmd;
151 # Render the images in the SVG
152 my $parser = new XML::Parser(
153 Handlers => {Start => \&svg_element_start});
154 $parser->parsefile("$svgFileName");
156 # If no render directives were found, take the full image as-is
157 unless (@pngFiles || @pngFilesRaw) {
158 my $pngFileName = "bmp$renderedSVGFileName";
159 copy($renderedSVGFileName, $pngFileName) or die "File could not be copied";
160 push(@pngFiles, $pngFileName);
163 # Combine the renderings into the output file
164 if($ext eq "ico") {
166 # Place images into the ICO
167 shell $icotool, "-c", "-o", $outFileName, @pngFiles, map { "--raw=$_"; } @pngFilesRaw;
169 } elsif($ext eq "bmp") {
171 # Only the first image becomes the final BMP
172 my $pngFile = $pngFiles[0];
173 $pngFile =~ /.*-\d*-(\d*)\.png/;
174 my $depth = $1;
176 # Convert it into a bmp
177 if($depth == 24) {
178 shell $convert, "png:$pngFile", "+matte", $outFileName;
179 } else {
180 shell $convert, "png:$pngFile", $outFileName;
185 # Delete the intermediate images
186 cleanup();