Avoid potential negative array index access to cached text.
[LibreOffice.git] / basegfx / qa / mkpolygons.pl
blob714031775eea4fa6ced1ef1fce0a53c1186d530c
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
5 # This file is part of the LibreOffice project.
7 # This Source Code Form is subject to the terms of the Mozilla Public
8 # License, v. 2.0. If a copy of the MPL was not distributed with this
9 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 # This file incorporates work covered by the following license notice:
13 # Licensed to the Apache Software Foundation (ASF) under one or more
14 # contributor license agreements. See the NOTICE file distributed
15 # with this work for additional information regarding copyright
16 # ownership. The ASF licenses this file to you under the Apache
17 # License, Version 2.0 (the "License"); you may not use this file
18 # except in compliance with the License. You may obtain a copy of
19 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 use IO::File;
23 use Cwd;
24 use File::Spec;
25 use File::Spec::Functions;
26 use File::Temp;
27 use File::Path;
29 $TempDir = "";
32 # all the XML package generation is a blatant rip from AF's
33 # write-calc-doc.pl
36 # Open a file with the given name.
37 # First it is checked if the temporary directory, in which all files for
38 # the document are gathered, is already present and create it if it is not.
39 # Then create the path to the file inside the temporary directory.
40 # Finally open the file and return a file handle to it.
42 sub open_file
44 my $filename = pop @_;
46 # Create base directory of temporary directory tree if not already
47 # present.
48 if ($TempDir eq "")
50 $TempDir = File::Temp::tempdir (CLEANUP => 1);
53 # Create the path to the file.
54 my $fullname = File::Spec->catfile ($TempDir, $filename);
55 my ($volume,$directories,$file) = File::Spec->splitpath ($fullname);
56 mkpath (File::Spec->catpath ($volume,$directories,""));
58 # Open the file and return a file handle to it.
59 return new IO::File ($fullname, "w");
63 # Zip the files in the directory tree into the given file.
65 sub zip_dirtree
67 my $filename = pop @_;
69 my $cwd = getcwd;
70 my $zip_name = $filename;
72 # We are about to change the directory.
73 # Therefore create an absolute pathname for the zip archive.
75 # First transfer the drive from $cwd to $zip_name. This is a
76 # workaround for a bug in file_name_is_absolute which thinks
77 # the path \bla is an absolute path under DOS.
78 my ($volume,$directories,$file) = File::Spec->splitpath ($zip_name);
79 my ($volume_cwd,$directories_cwd,$file_cwd) = File::Spec->splitpath ($cwd);
80 $volume = $volume_cwd if ($volume eq "");
81 $zip_name = File::Spec->catpath ($volume,$directories,$file);
83 # Add the current working directory to a relative path.
84 if ( ! file_name_is_absolute ($zip_name))
86 $zip_name = File::Spec->catfile ($cwd, $zip_name);
88 # Try everything to clean up the name.
89 $zip_name = File::Spec->rel2abs ($filename);
90 $zip_name = File::Spec->canonpath ($zip_name);
92 # Remove .. directories from the middle of the path.
93 while ($zip_name =~ /\/[^\/][^\.\/][^\/]*\/\.\.\//)
95 $zip_name = $` . "/" . $';
99 # Just in case the zip program gets confused by an existing file with the
100 # same name as the one to be written that file is removed first.
101 if ( -e $filename)
103 if (unlink ($filename) == 0)
105 print "Existing file $filename could not be deleted.\n";
106 print "Please close the application that uses it, then try again.\n";
107 return;
111 # Finally create the zip file. First change into the temporary directory
112 # so that the resulting zip file contains only paths relative to it.
113 print "zipping [$ZipCmd $ZipFlags $zip_name *]\n";
114 chdir ($TempDir);
115 system ("$ZipCmd $ZipFlags $zip_name *");
116 chdir ($cwd);
120 sub writeHeader
122 print $OUT qq~<?xml version="1.0" encoding="UTF-8"?>
124 <office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:smil="urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0" xmlns:anim="urn:oasis:names:tc:opendocument:xmlns:animation:1.0" office:version="1.0">
125 <office:scripts/>
126 <office:automatic-styles>
127 <style:style style:name="dp1" style:family="drawing-page">
128 <style:drawing-page-properties presentation:background-visible="true" presentation:background-objects-visible="true" presentation:display-footer="true" presentation:display-page-number="false" presentation:display-date-time="true"/>
129 </style:style>
130 <style:style style:name="gr1" style:family="graphic" style:parent-style-name="standard">
131 <style:graphic-properties draw:textarea-horizontal-align="center" draw:fill="none" draw:stroke="none" draw:textarea-vertical-align="middle"/>
132 </style:style>
133 <style:style style:name="gr2" style:family="graphic" style:parent-style-name="standard">
134 <style:graphic-properties draw:textarea-horizontal-align="center" draw:textarea-vertical-align="middle"/>
135 </style:style>
136 <style:style style:name="pr1" style:family="presentation" style:parent-style-name="Default-title">
137 <style:graphic-properties draw:fill-color="#ffffff" draw:auto-grow-height="true" fo:min-height="3.508cm"/>
138 </style:style>
139 <style:style style:name="pr2" style:family="presentation" style:parent-style-name="Default-notes">
140 <style:graphic-properties draw:fill-color="#ffffff" draw:auto-grow-height="true" fo:min-height="13.367cm"/>
141 </style:style>
142 <style:style style:name="P1" style:family="paragraph">
143 <style:paragraph-properties fo:margin-left="0cm" fo:margin-right="0cm" fo:text-indent="0cm"/>
144 </style:style>
145 <style:style style:name="P2" style:family="paragraph">
146 <style:paragraph-properties fo:margin-left="0.6cm" fo:margin-right="0cm" fo:text-indent="-0.6cm"/>
147 </style:style>
148 <text:list-style style:name="L1">
149 <text:list-level-style-bullet text:level="1" text:bullet-char="●">
150 <style:text-properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/>
151 </text:list-level-style-bullet>
152 <text:list-level-style-bullet text:level="2" text:bullet-char="●">
153 <style:list-level-properties text:space-before="0.6cm" text:min-label-width="0.6cm"/>
154 <style:text-properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/>
155 </text:list-level-style-bullet>
156 <text:list-level-style-bullet text:level="3" text:bullet-char="●">
157 <style:list-level-properties text:space-before="1.2cm" text:min-label-width="0.6cm"/>
158 <style:text-properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/>
159 </text:list-level-style-bullet>
160 <text:list-level-style-bullet text:level="4" text:bullet-char="●">
161 <style:list-level-properties text:space-before="1.8cm" text:min-label-width="0.6cm"/>
162 <style:text-properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/>
163 </text:list-level-style-bullet>
164 <text:list-level-style-bullet text:level="5" text:bullet-char="●">
165 <style:list-level-properties text:space-before="2.4cm" text:min-label-width="0.6cm"/>
166 <style:text-properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/>
167 </text:list-level-style-bullet>
168 <text:list-level-style-bullet text:level="6" text:bullet-char="●">
169 <style:list-level-properties text:space-before="3cm" text:min-label-width="0.6cm"/>
170 <style:text-properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/>
171 </text:list-level-style-bullet>
172 <text:list-level-style-bullet text:level="7" text:bullet-char="●">
173 <style:list-level-properties text:space-before="3.6cm" text:min-label-width="0.6cm"/>
174 <style:text-properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/>
175 </text:list-level-style-bullet>
176 <text:list-level-style-bullet text:level="8" text:bullet-char="●">
177 <style:list-level-properties text:space-before="4.2cm" text:min-label-width="0.6cm"/>
178 <style:text-properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/>
179 </text:list-level-style-bullet>
180 <text:list-level-style-bullet text:level="9" text:bullet-char="●">
181 <style:list-level-properties text:space-before="4.8cm" text:min-label-width="0.6cm"/>
182 <style:text-properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/>
183 </text:list-level-style-bullet>
184 </text:list-style>
185 </office:automatic-styles>
186 <office:body>
187 <office:presentation>
192 sub writeSlideHeader
194 my $titleText = pop @_;
195 my $slideNum = pop @_;
197 print $OUT " <draw:page draw:name=\"page1\" draw:style-name=\"dp1\" draw:master-page-name=\"Default\">\n";
198 print $OUT " <office:forms form:automatic-focus=\"false\" form:apply-design-mode=\"false\"/>\n";
199 print $OUT " <draw:rect draw:style-name=\"gr1\" draw:text-style-name=\"P1\" draw:id=\"id$slideNum\" draw:layer=\"layout\" svg:width=\"17.5cm\" svg:height=\"6cm\" svg:x=\"5cm\" svg:y=\"4cm\">\n";
200 print $OUT " <text:p text:style-name=\"P2\">Slide: $slideNum</text:p>\n";
201 print $OUT " <text:p text:style-name=\"P2\">Path: $titleText</text:p>\n";
202 print $OUT " </draw:rect>\n";
206 sub writeSlideFooter
208 print $OUT " <presentation:notes draw:style-name=\"dp1\">\n";
209 print $OUT " <draw:page-thumbnail draw:style-name=\"gr1\" draw:layer=\"layout\" svg:width=\"14.851cm\" svg:height=\"11.138cm\" svg:x=\"3.068cm\" svg:y=\"2.257cm\" draw:page-number=\"1\" presentation:class=\"page\"/>\n";
210 print $OUT " <draw:frame presentation:style-name=\"pr3\" draw:layer=\"layout\" svg:width=\"16.79cm\" svg:height=\"13.116cm\" svg:x=\"2.098cm\" svg:y=\"14.109cm\" presentation:class=\"notes\" presentation:placeholder=\"true\">\n";
211 print $OUT " <draw:text-box/>\n";
212 print $OUT " </draw:frame>\n";
213 print $OUT " </presentation:notes>\n";
214 print $OUT " </draw:page>\n";
217 sub writeFooter
219 print $OUT qq~ <presentation:settings presentation:full-screen="false"/>
220 </office:presentation>
221 </office:body>
222 </office:document-content>
227 sub writePath
229 my $pathAry = pop @_;
230 my $path = $pathAry->[1];
231 my $viewBox = $pathAry->[0];
233 print $OUT " <draw:path draw:style-name=\"gr2\" draw:text-style-name=\"P1\" draw:layer=\"layout\" svg:width=\"10cm\" svg:height=\"10cm\" svg:x=\"5cm\" svg:y=\"5cm\" svg:viewBox=\"";
234 print $OUT $viewBox;
235 print $OUT "\" svg:d=\"";
236 print $OUT $path;
237 print $OUT "\">\n";
238 print $OUT " <text:p/>\n";
239 print $OUT " </draw:path>\n";
242 sub writeManifest
244 my $outFile = open_file("META-INF/manifest.xml");
246 print $outFile qq~<?xml version="1.0" encoding="UTF-8"?>
247 <!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">
248 <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
249 <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.presentation" manifest:full-path="/"/>
250 <manifest:file-entry manifest:media-type="text/xml" manifest:full-path="content.xml"/>
251 </manifest:manifest>
254 $outFile->close;
258 # Print usage information.
260 sub usage ()
262 print <<END_OF_USAGE;
263 usage: $0 <option>* [<SvgD-values>]
265 output-file-name defaults to polygons.odp.
267 -h Print this usage information.
268 -o output-file-name
269 END_OF_USAGE
272 # Process the command line.
274 sub process_command_line
276 foreach (@ARGV)
278 if (/^-h/)
280 usage;
281 exit 0;
285 $global_output_name = "polygons.odp";
286 my $j = 0, $noMoreOptions = 0;
287 for (my $i=0; $i<$#ARGV; $i++)
289 if ( !$noMoreOptions and $ARGV[$i] eq "-o")
291 $i++;
292 $global_output_name = $ARGV[$i];
294 elsif ( !$noMoreOptions and $ARGV[$i] eq "--")
296 $noMoreOptions = 1;
298 elsif ( !$noMoreOptions and $ARGV[$i] =~ /^-/)
300 print "Unknown option $ARGV[$i]\n";
301 usage;
302 exit 1;
304 else
306 push(@paths, [$ARGV[$i],$ARGV[$i+1]]);
307 $i++;
311 print "output to $global_output_name\n";
314 # Main
316 $ZipCmd = $ENV{LOG_FILE_ZIP_CMD};
317 $ZipFlags = $ENV{LOG_FILE_ZIP_FLAGS};
318 # Provide default values for the zip command and it's flags.
319 if ( ! defined $ZipCmd)
321 $ZipCmd = "zip" unless defined $ZipCmd;
322 $ZipFlags = "-r -q" unless defined $ZipFlags;
325 process_command_line();
327 writeManifest();
329 $OUT = open_file( "content.xml" );
331 writeHeader();
333 $pathNum=0;
334 foreach $path (@paths)
336 writeSlideHeader($pathNum, $path->[1]);
337 writePath($path);
338 writeSlideFooter();
339 $pathNum++;
342 writeFooter();
344 $OUT->close;
346 zip_dirtree ($global_output_name);