| 1 |
/* |
| 2 |
|
| 3 |
Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 |
contributor license agreements. See the NOTICE file distributed with |
| 5 |
this work for additional information regarding copyright ownership. |
| 6 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 |
(the "License"); you may not use this file except in compliance with |
| 8 |
the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
|
| 18 |
*/ |
| 19 |
package org.apache.tools.ant.taskdefs.optional; |
| 20 |
|
| 21 |
// -- Ant classes ------------------------------------------------------------ |
| 22 |
import org.apache.tools.ant.Project; |
| 23 |
import org.apache.tools.ant.BuildException; |
| 24 |
import org.apache.tools.ant.DirectoryScanner; |
| 25 |
import org.apache.tools.ant.taskdefs.MatchingTask; |
| 26 |
import org.apache.tools.ant.types.EnumeratedAttribute; |
| 27 |
import org.apache.tools.ant.types.FileSet; |
| 28 |
import org.apache.tools.ant.util.JAXPUtils; |
| 29 |
|
| 30 |
// -- Batik classes ---------------------------------------------------------- |
| 31 |
import org.apache.batik.apps.rasterizer.SVGConverter; |
| 32 |
import org.apache.batik.apps.rasterizer.DestinationType; |
| 33 |
import org.apache.batik.apps.rasterizer.SVGConverterException; |
| 34 |
import org.apache.batik.util.XMLResourceDescriptor; |
| 35 |
|
| 36 |
// -- SAX classes ------------------------------------------------------------ |
| 37 |
import org.xml.sax.XMLReader; |
| 38 |
|
| 39 |
// -- Java SDK classes ------------------------------------------------------- |
| 40 |
import java.awt.geom.Rectangle2D; |
| 41 |
import java.awt.Color; |
| 42 |
import java.io.File; |
| 43 |
import java.util.StringTokenizer; |
| 44 |
import java.util.Vector; |
| 45 |
import java.util.ArrayList; |
| 46 |
import java.util.List; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* This Ant task can be used to convert SVG images to raster images. |
| 52 |
* |
| 53 |
* <p>Possible result raster image formats are PNG, JPEG, TIFF, and PDF. |
| 54 |
* Batik {@link SVGConverter} is used to execute the conversion. You need |
| 55 |
* <em>Batik</em> to produce the first three raster image types and |
| 56 |
* <em>FOP</em> to produce PDF.</p> |
| 57 |
* |
| 58 |
* @see SVGConverter SVGConverter |
| 59 |
* @see org.apache.batik.apps.rasterizer.Main Main |
| 60 |
* |
| 61 |
* @author <a href="mailto:[email protected]">Henri Ruini</a> |
| 62 |
* @version $Id$ |
| 63 |
*/ |
| 64 |
public class RasterizerTask extends MatchingTask { |
| 65 |
|
| 66 |
// -- Constants ---------------------------------------------------------- |
| 67 |
/** |
| 68 |
* Default quality value for JPEGs. This value is used when |
| 69 |
* the user doesn't set the quality. |
| 70 |
*/ |
| 71 |
private static final float DEFAULT_QUALITY = 0.99f; |
| 72 |
/** |
| 73 |
* Magic string indicating that any JAXP conforming XML parser can |
| 74 |
* be used. |
| 75 |
*/ |
| 76 |
private static final String JAXP_PARSER = "jaxp"; |
| 77 |
|
| 78 |
// -- Variables ---------------------------------------------------------- |
| 79 |
/** Result image type. The default is PNG. */ |
| 80 |
protected DestinationType resultType = DestinationType.PNG; |
| 81 |
|
| 82 |
/** Output image height. */ |
| 83 |
protected float height = Float.NaN; |
| 84 |
/** Output image width. */ |
| 85 |
protected float width = Float.NaN; |
| 86 |
/** Maximum output image height. */ |
| 87 |
protected float maxHeight = Float.NaN; |
| 88 |
/** Maximum output image width. */ |
| 89 |
protected float maxWidth = Float.NaN; |
| 90 |
/** Output image quality. */ |
| 91 |
protected float quality = Float.NaN; |
| 92 |
/** Output Area of Interest (AOI) area. */ |
| 93 |
protected String area = null; |
| 94 |
/** Output background color. */ |
| 95 |
protected String background = null; |
| 96 |
/** Media type of CSS file used to produce output images. */ |
| 97 |
protected String mediaType = null; |
| 98 |
/** Output pixel size - dots per inch. */ |
| 99 |
protected float dpi = Float.NaN; |
| 100 |
/** Output image language. */ |
| 101 |
protected String language = null; |
| 102 |
/** XML parser class currently in use. */ |
| 103 |
protected String readerClassName = XMLResourceDescriptor.getXMLParserClassName(); |
| 104 |
|
| 105 |
|
| 106 |
/** Source image path. */ |
| 107 |
protected File srcFile = null; |
| 108 |
/** Destination image path. */ |
| 109 |
protected File destFile = null; |
| 110 |
/** Source directory of images. */ |
| 111 |
protected File srcDir = null; |
| 112 |
/** Destination directory for output images. */ |
| 113 |
protected File destDir = null; |
| 114 |
/** Contents of <code>fileset</code> elements. */ |
| 115 |
protected Vector filesets = new Vector(); |
| 116 |
|
| 117 |
/** Converter object used to convert SVG images to raster images. */ |
| 118 |
protected SVGConverter converter; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
// -- Constructors ------------------------------------------------------- |
| 123 |
/** |
| 124 |
* Initializes a new rasterizer task. |
| 125 |
*/ |
| 126 |
public RasterizerTask() { |
| 127 |
converter = new SVGConverter(new RasterizerTaskSVGConverterController(this)); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
// -- Methods required by Ant -------------------------------------------- |
| 133 |
/** |
| 134 |
* Gets <code>result</code> attribute value. |
| 135 |
* |
| 136 |
* <p>See the documentation for valid values.</p> |
| 137 |
* |
| 138 |
* @param type Attribute value. |
| 139 |
*/ |
| 140 |
public void setResult(ValidImageTypes type) { |
| 141 |
this.resultType = getResultType(type.getValue()); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Gets <code>height</code> attribute value. |
| 146 |
* |
| 147 |
* <p>The attribute is optional.</p> |
| 148 |
* |
| 149 |
* @param height Attribute value. |
| 150 |
*/ |
| 151 |
public void setHeight(float height) { |
| 152 |
this.height = height; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Gets <code>width</code> attribute value. |
| 157 |
* |
| 158 |
* <p>The attribute is optional.</p> |
| 159 |
* |
| 160 |
* @param width Attribute value. |
| 161 |
*/ |
| 162 |
public void setWidth(float width) { |
| 163 |
this.width = width; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Gets <code>maxheight</code> attribute value. |
| 168 |
* |
| 169 |
* <p>The attribute is optional.</p> |
| 170 |
* |
| 171 |
* @param height Attribute value. |
| 172 |
*/ |
| 173 |
public void setMaxheight(float height) { |
| 174 |
this.maxHeight = height; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Gets <code>maxwidth</code> attribute value. |
| 179 |
* |
| 180 |
* <p>The attribute is optional.</p> |
| 181 |
* |
| 182 |
* @param width Attribute value. |
| 183 |
*/ |
| 184 |
public void setMaxwidth(float width) { |
| 185 |
this.maxWidth = width; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Gets <code>quality</code> attribute value. |
| 190 |
* |
| 191 |
* <p>The value have to be a float between 0 and 1 excluded. |
| 192 |
* The attribute is optional.</p> |
| 193 |
* |
| 194 |
* @param quality Attribute value. |
| 195 |
*/ |
| 196 |
public void setQuality(float quality) { |
| 197 |
this.quality = quality; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Gets <code>area</code> attribute value. |
| 202 |
* |
| 203 |
* <p>The value have to be four integers separated with whitespaces or |
| 204 |
* commas. |
| 205 |
* The attribute is optional.</p> |
| 206 |
* |
| 207 |
* @param area Attribute value. |
| 208 |
*/ |
| 209 |
public void setArea(String area) { |
| 210 |
this.area = area; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Gets <code>bg</code> attribute value. |
| 215 |
* |
| 216 |
* <p>The value have to be three or four integers separated with |
| 217 |
* whitespaces or commas. |
| 218 |
* The attribute is optional.</p> |
| 219 |
* |
| 220 |
* @param bg Attribute value. |
| 221 |
*/ |
| 222 |
public void setBg(String bg) { |
| 223 |
this.background = bg; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Gets <code>media</code> attribute value. |
| 228 |
* |
| 229 |
* <p>The value have to a media type defined in CSS2 specifications. |
| 230 |
* Only visual media group is supported.</p> |
| 231 |
* |
| 232 |
* @param media Attribute value. |
| 233 |
*/ |
| 234 |
public void setMedia(ValidMediaTypes media) { |
| 235 |
this.mediaType = media.getValue(); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Gets <code>dpi</code> attribute value. |
| 240 |
* |
| 241 |
* <p>The value have to be a positive float. |
| 242 |
* The attribute is optional.</p> |
| 243 |
* |
| 244 |
* @param dpi Attribute value. |
| 245 |
*/ |
| 246 |
public void setDpi(float dpi) { |
| 247 |
this.dpi = dpi; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Gets <code>lang</code> attribute value. |
| 252 |
* |
| 253 |
* <p>See SVG specification for valid values. |
| 254 |
* The attribute is optional.</p> |
| 255 |
* |
| 256 |
* @param lang Attribute value. |
| 257 |
*/ |
| 258 |
public void setLang(String language) { |
| 259 |
this.language = language; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Sets classname of an XML parser. |
| 264 |
* The attribute is optional. |
| 265 |
* |
| 266 |
* @param value Java classname of an XML parser. |
| 267 |
*/ |
| 268 |
public void setClassname(String value) { |
| 269 |
this.readerClassName = value; |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Gets <code>src</code> attribute value. |
| 274 |
* |
| 275 |
* <p>One of the following have to have a value: this attribute, |
| 276 |
* <code>srcdir</code> attribute or <code>fileset</code> element.</p> |
| 277 |
* |
| 278 |
* @param file Attribute value. |
| 279 |
*/ |
| 280 |
public void setSrc(File file) { |
| 281 |
this.srcFile = file; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Gets <code>dest</code> attribute value. |
| 286 |
* |
| 287 |
* <p>The attribute have to have a value when |
| 288 |
* <code>src</code> attribute has a value.</p> |
| 289 |
* |
| 290 |
* @param file Attribute value. |
| 291 |
*/ |
| 292 |
public void setDest(File file) { |
| 293 |
this.destFile = file; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Gets <code>srcdir</code> attribute value. |
| 298 |
* |
| 299 |
* <p>If <code>srcfile</code> attribute doesn't have a value then |
| 300 |
* either this attribute have to have a value or the element have |
| 301 |
* to contain <code>fileset</code> elements.</p> |
| 302 |
* |
| 303 |
* @param dir Attribute value. |
| 304 |
*/ |
| 305 |
public void setSrcdir(File dir) { |
| 306 |
this.srcDir = dir; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Gets <code>destdir</code> attribute value. |
| 311 |
* |
| 312 |
* <p>This attribute have to have a value if either |
| 313 |
* <code>srcdir</code> attribute or <code>fileset</code> elements |
| 314 |
* exists.</p> |
| 315 |
* |
| 316 |
* @param dir Attribute value. |
| 317 |
*/ |
| 318 |
public void setDestdir(File dir) { |
| 319 |
this.destDir = dir; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Reads <code>fileset</code> elements. |
| 324 |
* |
| 325 |
* <p><code>fileset</code> elements can be used when there are many files |
| 326 |
* to be rasterized.</p> |
| 327 |
* |
| 328 |
* @param set <code>fileset</code> elements |
| 329 |
*/ |
| 330 |
public void addFileset(FileSet set) { |
| 331 |
filesets.addElement(set); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Validates and sets input values and files, then starts the conversion |
| 336 |
* process. |
| 337 |
* |
| 338 |
* <p>See Ant documentation to find out more about the meaning of |
| 339 |
* <code>execute</code> in Ant task.</p> |
| 340 |
* |
| 341 |
* @throws BuildException Parameters are not set correctly or the conversion fails. |
| 342 |
*/ |
| 343 |
public void execute() throws BuildException { |
| 344 |
|
| 345 |
String[] sources; // Array of input files. |
| 346 |
|
| 347 |
// Store default XML parser information and set user class. |
| 348 |
String defaultParser = XMLResourceDescriptor.getXMLParserClassName(); |
| 349 |
// Throws BuildException. |
| 350 |
XMLResourceDescriptor.setXMLParserClassName(getParserClassName(readerClassName)); |
| 351 |
|
| 352 |
try { |
| 353 |
// Check file and directory values. |
| 354 |
if(this.srcFile != null) { |
| 355 |
if(this.destFile == null) { |
| 356 |
throw new BuildException("dest attribute is not set."); |
| 357 |
} |
| 358 |
} else { |
| 359 |
if((this.srcDir == null) && (filesets.size() == 0)) { |
| 360 |
throw new BuildException("No input files! Either srcdir or fileset have to be set."); |
| 361 |
} |
| 362 |
if(this.destDir == null) { |
| 363 |
throw new BuildException("destdir attribute is not set!"); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
// Throws BuildException. |
| 368 |
setRasterizingParameters(); |
| 369 |
|
| 370 |
// Get and set source(s). |
| 371 |
sources = getSourceFiles(); |
| 372 |
converter.setSources(sources); |
| 373 |
|
| 374 |
// Set destination. |
| 375 |
if(this.srcFile != null) { |
| 376 |
converter.setDst(this.destFile); |
| 377 |
} else { |
| 378 |
converter.setDst(this.destDir); |
| 379 |
} |
| 380 |
|
| 381 |
// Input filenames are stored in the converter and |
| 382 |
// everything is ready for the conversion. |
| 383 |
|
| 384 |
log("Rasterizing " + sources.length + |
| 385 |
(sources.length == 1 ? " image " : " images ") + |
| 386 |
"from SVG to " + this.resultType.toString() + "."); |
| 387 |
|
| 388 |
try { |
| 389 |
converter.execute(); |
| 390 |
} catch(SVGConverterException sce) { |
| 391 |
throw new BuildException(sce.getMessage()); |
| 392 |
} |
| 393 |
} finally { |
| 394 |
// Restore default XML parser for the next execute. |
| 395 |
XMLResourceDescriptor.setXMLParserClassName(defaultParser); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
// -- Internal methods --------------------------------------------------- |
| 402 |
/** |
| 403 |
* Checks and sets parameter values to the converter. |
| 404 |
* |
| 405 |
* <p>Some invalid values are just swallowed and default values are |
| 406 |
* used instead. This is done when the invalid value cannot |
| 407 |
* be correctly differentiated from the default value and the default |
| 408 |
* value doesn't cause any harm. <code>BuildException</code> is thrown |
| 409 |
* if the invalid value is clearly recognized.</p> |
| 410 |
* |
| 411 |
* @throws BuildException Invalid parameter value. |
| 412 |
*/ |
| 413 |
protected void setRasterizingParameters() |
| 414 |
throws BuildException { |
| 415 |
if(this.resultType != null) { |
| 416 |
converter.setDestinationType(this.resultType); |
| 417 |
} else { |
| 418 |
throw new BuildException("Unknown value in result parameter."); |
| 419 |
} |
| 420 |
// Set size values. |
| 421 |
if(!Float.isNaN(this.width)) { |
| 422 |
if(this.width < 0) { |
| 423 |
throw new BuildException("Value of width parameter must positive."); |
| 424 |
} |
| 425 |
converter.setWidth(this.width); |
| 426 |
} |
| 427 |
if(!Float.isNaN(this.height)) { |
| 428 |
if(this.height < 0) { |
| 429 |
throw new BuildException("Value of height parameter must positive."); |
| 430 |
} |
| 431 |
converter.setHeight(this.height); |
| 432 |
} |
| 433 |
// Set maximum size values. |
| 434 |
if(!Float.isNaN(this.maxWidth)) { |
| 435 |
if(this.maxWidth < 0) { |
| 436 |
throw new BuildException("Value of maxwidth parameter must positive."); |
| 437 |
} |
| 438 |
converter.setMaxWidth(this.maxWidth); |
| 439 |
} |
| 440 |
if(!Float.isNaN(this.maxHeight)) { |
| 441 |
if(this.maxHeight < 0) { |
| 442 |
throw new BuildException("Value of maxheight parameter must positive."); |
| 443 |
} |
| 444 |
converter.setMaxHeight(this.maxHeight); |
| 445 |
} |
| 446 |
// The quality is just swallowed if the result type is not correct. |
| 447 |
if(allowedToSetQuality(resultType)) { |
| 448 |
if(!Float.isNaN(this.quality)) { |
| 449 |
// Throws BuildException. |
| 450 |
converter.setQuality(getQuality(this.quality)); |
| 451 |
} else { |
| 452 |
// Set something to quiet irritating error |
| 453 |
// from JPEGTranscoder. |
| 454 |
converter.setQuality(DEFAULT_QUALITY); |
| 455 |
} |
| 456 |
} |
| 457 |
if(this.area != null) { |
| 458 |
// Throws BuildException. |
| 459 |
converter.setArea(getAreaOfInterest(this.area)); |
| 460 |
} |
| 461 |
if(this.background != null) { |
| 462 |
// Throws BuildException. |
| 463 |
converter.setBackgroundColor(getBackgroundColor(this.background)); |
| 464 |
} |
| 465 |
if(this.mediaType != null) { |
| 466 |
// Ant takes care of the correct media type values. |
| 467 |
converter.setMediaType(this.mediaType); |
| 468 |
} |
| 469 |
if(!Float.isNaN(this.dpi)) { |
| 470 |
if(this.dpi < 0) { |
| 471 |
throw new BuildException("Value of dpi parameter must positive."); |
| 472 |
} |
| 473 |
// The calculation is the same as 2.54/dpi*10 where |
| 474 |
converter.setPixelUnitToMillimeter(25.4f/this.dpi); |
| 475 |
} |
| 476 |
if(this.language != null) { |
| 477 |
converter.setLanguage(this.language); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Gets source files from the task parameters and child elements, |
| 483 |
* combines those to a one list, and returns the list. |
| 484 |
* |
| 485 |
* @return Array of source filename strings. |
| 486 |
*/ |
| 487 |
protected String[] getSourceFiles() { |
| 488 |
|
| 489 |
List inputFiles = new ArrayList(); // Input files in temp list. |
| 490 |
|
| 491 |
if(this.srcFile != null) { |
| 492 |
// Only one source and destination file have been set. |
| 493 |
inputFiles.add(this.srcFile.getAbsolutePath()); |
| 494 |
} else { |
| 495 |
// Unknown number of files have to be converted. destdir |
| 496 |
// attribute and either srcdir attribute or fileset element |
| 497 |
// have been set. |
| 498 |
|
| 499 |
// Read source files from the child patterns. |
| 500 |
// The value of srcdir attribute overrides the dir attribute in |
| 501 |
// fileset element. |
| 502 |
if(this.srcDir != null) { |
| 503 |
// fileset is declared in the super class. |
| 504 |
// Scan to get all the files in srcdir directory that |
| 505 |
// should be in input files. |
| 506 |
fileset.setDir(this.srcDir); |
| 507 |
DirectoryScanner ds = fileset.getDirectoryScanner(project); |
| 508 |
String[] includedFiles = ds.getIncludedFiles(); |
| 509 |
// Add file and its path to the input file vector. |
| 510 |
for (int j = 0 ; j < includedFiles.length ; j++) { |
| 511 |
File newFile = new File(srcDir.getPath(), includedFiles[j]); |
| 512 |
inputFiles.add(newFile.getAbsolutePath()); |
| 513 |
} |
| 514 |
} |
| 515 |
// Read source files from child filesets. |
| 516 |
for (int i = 0 ; i < filesets.size() ; i++) { |
| 517 |
// Scan to get all the files in this fileset that |
| 518 |
// should be in input files. |
| 519 |
FileSet fs = (FileSet) filesets.elementAt(i); |
| 520 |
DirectoryScanner ds = fs.getDirectoryScanner(project); |
| 521 |
String[] includedFiles = ds.getIncludedFiles(); |
| 522 |
// Add file and its path to the input file vector. |
| 523 |
for (int j = 0 ; j < includedFiles.length ; j++) { |
| 524 |
File newFile = new File(fs.getDir(project).getPath(), includedFiles[j]); |
| 525 |
inputFiles.add(newFile.getAbsolutePath()); |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
// Convert List to array and return the array. |
| 531 |
return (String[])inputFiles.toArray(new String[0]); |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* Returns the correct result image type object. |
| 536 |
* |
| 537 |
* @param type Result image type as a string. |
| 538 |
* |
| 539 |
* @return Result image type as an object or <code>null</code> if the parameter doesn't have corresponding object. |
| 540 |
*/ |
| 541 |
protected DestinationType getResultType(String type) { |
| 542 |
if(type.equals(DestinationType.PNG_STR)) { |
| 543 |
return DestinationType.PNG; |
| 544 |
} else if(type.equals(DestinationType.JPEG_STR)) { |
| 545 |
return DestinationType.JPEG; |
| 546 |
} else if(type.equals(DestinationType.TIFF_STR)) { |
| 547 |
return DestinationType.TIFF; |
| 548 |
} else if(type.equals(DestinationType.PDF_STR)) { |
| 549 |
return DestinationType.PDF; |
| 550 |
} |
| 551 |
return null; |
| 552 |
} |
| 553 |
|
| 554 |
/** |
| 555 |
* Checks if the quality value can be set. Only result image type |
| 556 |
* is checked. |
| 557 |
* |
| 558 |
* @param type Result image type. |
| 559 |
* |
| 560 |
* @return <code>true</code> if the quality can be set and <code>false</code> otherwise. |
| 561 |
*/ |
| 562 |
protected boolean allowedToSetQuality(DestinationType type) { |
| 563 |
if(!type.toString().equals(DestinationType.JPEG_STR)) { |
| 564 |
return false; |
| 565 |
} |
| 566 |
return true; |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* Returns a valid quality value. |
| 571 |
* |
| 572 |
* @param quality Input quality value to be tested. |
| 573 |
* |
| 574 |
* @return Valid quality value. |
| 575 |
* |
| 576 |
* @throws BuildException Input quality value is not valid. |
| 577 |
*/ |
| 578 |
protected float getQuality(float quality) |
| 579 |
throws BuildException { |
| 580 |
if((quality <= 0) || (quality >= 1)) { |
| 581 |
throw new BuildException("quality parameter value have to be between 0 and 1."); |
| 582 |
} |
| 583 |
return quality; |
| 584 |
} |
| 585 |
|
| 586 |
/** |
| 587 |
* Returns a valid Area of Interest (AOI) as a Rectangle2D object. |
| 588 |
* |
| 589 |
* @param area AOI input area. |
| 590 |
* |
| 591 |
* @return A valid AOI rectangle. |
| 592 |
* |
| 593 |
* @throws BuildException AOI area is invalid. |
| 594 |
*/ |
| 595 |
protected Rectangle2D getAreaOfInterest(String area) |
| 596 |
throws BuildException { |
| 597 |
|
| 598 |
float x; // Upper left x point value of the area. |
| 599 |
float y; // Upper left y point value of the area. |
| 600 |
float width; // Area width value. |
| 601 |
float height; // Area height value. |
| 602 |
String token; // A token from the input string. |
| 603 |
StringTokenizer tokenizer = new StringTokenizer(area, ", \t\n\r\f"); |
| 604 |
// Input string tokenizer. |
| 605 |
|
| 606 |
if(tokenizer.countTokens() != 4) { |
| 607 |
throw new BuildException("There must be four numbers in the area parameter: x, y, width, and height."); |
| 608 |
} |
| 609 |
try { |
| 610 |
x = Float.parseFloat(tokenizer.nextToken()); |
| 611 |
y = Float.parseFloat(tokenizer.nextToken()); |
| 612 |
width = Float.parseFloat(tokenizer.nextToken()); |
| 613 |
height = Float.parseFloat(tokenizer.nextToken()); |
| 614 |
} catch(NumberFormatException nfe) { |
| 615 |
throw new BuildException("Invalid area parameter value: " + nfe.toString()); |
| 616 |
} |
| 617 |
|
| 618 |
// Negative values are not allowed. |
| 619 |
if((x < 0) || (y < 0) || (width < 0) || (height < 0)) { |
| 620 |
throw new BuildException("Negative values are not allowed in area parameter."); |
| 621 |
} |
| 622 |
|
| 623 |
return new Rectangle2D.Float(x, y, width, height); |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* Returns a valid background color object. |
| 628 |
* |
| 629 |
* @param argb String containing color channel values. |
| 630 |
* |
| 631 |
* @return A valid background color. |
| 632 |
* |
| 633 |
* @throws BuildException Input value is invalid. |
| 634 |
*/ |
| 635 |
protected Color getBackgroundColor(String argb) |
| 636 |
throws BuildException { |
| 637 |
|
| 638 |
int a; // Value of the alpha channel. |
| 639 |
int r; // Value of the red channel. |
| 640 |
int g; // Value of the green channel. |
| 641 |
int b; // Value of the blue channel. |
| 642 |
String token; // A token from the input string. |
| 643 |
StringTokenizer tokenizer = new StringTokenizer(argb, ", \t\n\r\f"); |
| 644 |
// Input string tokenizer. |
| 645 |
|
| 646 |
try { |
| 647 |
if(tokenizer.countTokens() == 3) { |
| 648 |
// Default alpha channel is opaque. |
| 649 |
a = 255; |
| 650 |
} else if(tokenizer.countTokens() == 4) { |
| 651 |
a = Integer.parseInt(tokenizer.nextToken()); |
| 652 |
} else { |
| 653 |
throw new BuildException("There must be either three or four numbers in bg parameter: (alpha,) red, green, and blue."); |
| 654 |
} |
| 655 |
r = Integer.parseInt(tokenizer.nextToken()); |
| 656 |
g = Integer.parseInt(tokenizer.nextToken()); |
| 657 |
b = Integer.parseInt(tokenizer.nextToken()); |
| 658 |
} catch(NumberFormatException nfe) { |
| 659 |
throw new BuildException("Invalid bg parameter value: " + nfe.toString()); |
| 660 |
} |
| 661 |
|
| 662 |
// Check that the values are valid. |
| 663 |
if((a < 0) ||(a > 255) || (r < 0) ||(r > 255) || |
| 664 |
(g < 0) ||(g > 255) || (b < 0) ||(b > 255)) { |
| 665 |
throw new BuildException("bg parameter value is invalid. Numbers have to be between 0 and 255."); |
| 666 |
} |
| 667 |
|
| 668 |
return new Color(r, g, b, a); |
| 669 |
} |
| 670 |
|
| 671 |
/** |
| 672 |
* Returns name of an XML parser. |
| 673 |
* Magic string {@link #JAXP_PARSER} is also accepted. |
| 674 |
* |
| 675 |
* @param className Name of the XML parser class or a magic string. |
| 676 |
* |
| 677 |
* @return Name of an XML parser. |
| 678 |
* |
| 679 |
* @throws BuildException Unable to get the name of JAXP parser. |
| 680 |
*/ |
| 681 |
private String getParserClassName(final String className) { |
| 682 |
String name = className; |
| 683 |
if ((className == null) || className.equals(JAXP_PARSER)) { |
| 684 |
// Set first JAXP parser. |
| 685 |
// Throws BuildException. |
| 686 |
XMLReader reader = JAXPUtils.getXMLReader(); |
| 687 |
name = reader.getClass().getName(); |
| 688 |
} |
| 689 |
|
| 690 |
log("Using class '" + name + "' to parse SVG documents.", Project.MSG_VERBOSE); |
| 691 |
return name; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
// ----------------------------------------------------------------------- |
| 697 |
// Inner classes |
| 698 |
// ----------------------------------------------------------------------- |
| 699 |
|
| 700 |
/** |
| 701 |
* Defines the valid attribute values for <code>result</code> parameter. |
| 702 |
* |
| 703 |
* <p>See the Ant documentation for more information.</p> |
| 704 |
* |
| 705 |
* @author <a href="mailto:[email protected]">Henri Ruini</a> |
| 706 |
* @version $Id$ |
| 707 |
*/ |
| 708 |
public static class ValidImageTypes extends EnumeratedAttribute { |
| 709 |
|
| 710 |
/** |
| 711 |
* Defines valid image types. |
| 712 |
* |
| 713 |
* @return Array of valid values as strings. |
| 714 |
*/ |
| 715 |
public String[] getValues() { |
| 716 |
return new String[] |
| 717 |
{DestinationType.PNG_STR, |
| 718 |
DestinationType.JPEG_STR, |
| 719 |
DestinationType.TIFF_STR, |
| 720 |
DestinationType.PDF_STR}; |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
/** |
| 725 |
* Defines the valid attribute values for a media parameter. |
| 726 |
* |
| 727 |
* <p>See the Ant documentation for more information.</p> |
| 728 |
* |
| 729 |
* @author <a href="mailto:[email protected]">Henri Ruini</a> |
| 730 |
* @version $Id$ |
| 731 |
*/ |
| 732 |
public static class ValidMediaTypes extends EnumeratedAttribute { |
| 733 |
|
| 734 |
/** |
| 735 |
* Defines valid media types. |
| 736 |
* |
| 737 |
* <p>The types are defined in CSS2 specification. |
| 738 |
* Only visual media group is supported.</p> |
| 739 |
* |
| 740 |
* @return Array of valid values as strings. |
| 741 |
*/ |
| 742 |
public String[] getValues() { |
| 743 |
return new String[] {"all", "handheld", "print", |
| 744 |
"projection", "screen", "tty", "tv"}; |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
} |