1 18 package org.apache.tools.ant.taskdefs.optional; 19 20 import org.apache.tools.ant.Project; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.Task; 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 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.transcoder.image.JPEGTranscoder; 35 import org.apache.batik.util.XMLResourceDescriptor; 36 37 import org.xml.sax.XMLReader ; 39 40 import java.awt.geom.Rectangle2D ; 42 import java.awt.Color ; 43 import java.io.File ; 44 import java.util.StringTokenizer ; 45 import java.util.Vector ; 46 import java.util.Iterator ; 47 import java.util.ArrayList ; 48 import java.util.List ; 49 50 51 52 66 public class RasterizerTask extends MatchingTask { 67 68 73 private static final float DEFAULT_QUALITY = 0.99f; 74 78 private static final String JAXP_PARSER = "jaxp"; 79 80 82 protected DestinationType resultType = DestinationType.PNG; 83 84 85 protected float height = Float.NaN; 86 87 protected float width = Float.NaN; 88 89 protected float maxHeight = Float.NaN; 90 91 protected float maxWidth = Float.NaN; 92 93 protected float quality = Float.NaN; 94 95 protected String area = null; 96 97 protected String background = null; 98 99 protected String mediaType = null; 100 101 protected float dpi = Float.NaN; 102 103 protected String language = null; 104 105 protected String readerClassName = XMLResourceDescriptor.getXMLParserClassName(); 106 107 108 109 protected File srcFile = null; 110 111 protected File destFile = null; 112 113 protected File srcDir = null; 114 115 protected File destDir = null; 116 117 protected Vector filesets = new Vector (); 118 119 120 protected SVGConverter converter; 121 122 123 124 128 public RasterizerTask() { 129 converter = new SVGConverter(new RasterizerTaskSVGConverterController(this)); 130 } 131 132 133 134 142 public void setResult(ValidImageTypes type) { 143 this.resultType = getResultType(type.getValue()); 144 } 145 146 153 public void setHeight(float height) { 154 this.height = height; 155 } 156 157 164 public void setWidth(float width) { 165 this.width = width; 166 } 167 168 175 public void setMaxheight(float height) { 176 this.maxHeight = height; 177 } 178 179 186 public void setMaxwidth(float width) { 187 this.maxWidth = width; 188 } 189 190 198 public void setQuality(float quality) { 199 this.quality = quality; 200 } 201 202 211 public void setArea(String area) { 212 this.area = area; 213 } 214 215 224 public void setBg(String bg) { 225 this.background = bg; 226 } 227 228 236 public void setMedia(ValidMediaTypes media) { 237 this.mediaType = media.getValue(); 238 } 239 240 248 public void setDpi(float dpi) { 249 this.dpi = dpi; 250 } 251 252 260 public void setLang(String language) { 261 this.language = language; 262 } 263 264 270 public void setClassname(String value) { 271 this.readerClassName = value; 272 } 273 274 282 public void setSrc(File file) { 283 this.srcFile = file; 284 } 285 286 294 public void setDest(File file) { 295 this.destFile = file; 296 } 297 298 307 public void setSrcdir(File dir) { 308 this.srcDir = dir; 309 } 310 311 320 public void setDestdir(File dir) { 321 this.destDir = dir; 322 } 323 324 332 public void addFileset(FileSet set) { 333 filesets.addElement(set); 334 } 335 336 345 public void execute() throws BuildException { 346 347 String [] sources; 349 String defaultParser = XMLResourceDescriptor.getXMLParserClassName(); 351 XMLResourceDescriptor.setXMLParserClassName(getParserClassName(readerClassName)); 353 354 try { 355 if(this.srcFile != null) { 357 if(this.destFile == null) { 358 throw new BuildException("dest attribute is not set."); 359 } 360 } else { 361 if((this.srcDir == null) && (filesets.size() == 0)) { 362 throw new BuildException("No input files! Either srcdir or fileset have to be set."); 363 } 364 if(this.destDir == null) { 365 throw new BuildException("destdir attribute is not set!"); 366 } 367 } 368 369 setRasterizingParameters(); 371 372 sources = getSourceFiles(); 374 converter.setSources(sources); 375 376 if(this.srcFile != null) { 378 converter.setDst(this.destFile); 379 } else { 380 converter.setDst(this.destDir); 381 } 382 383 386 log("Rasterizing " + sources.length + 387 (sources.length == 1 ? " image " : " images ") + 388 "from SVG to " + this.resultType.toString() + "."); 389 390 try { 391 converter.execute(); 392 } catch(SVGConverterException sce) { 393 throw new BuildException(sce.getMessage()); 394 } 395 } finally { 396 XMLResourceDescriptor.setXMLParserClassName(defaultParser); 398 } 399 } 400 401 402 403 415 protected void setRasterizingParameters() 416 throws BuildException { 417 if(this.resultType != null) { 418 converter.setDestinationType(this.resultType); 419 } else { 420 throw new BuildException("Unknown value in result parameter."); 421 } 422 if(!Float.isNaN(this.width)) { 424 if(this.width < 0) { 425 throw new BuildException("Value of width parameter must positive."); 426 } 427 converter.setWidth(this.width); 428 } 429 if(!Float.isNaN(this.height)) { 430 if(this.height < 0) { 431 throw new BuildException("Value of height parameter must positive."); 432 } 433 converter.setHeight(this.height); 434 } 435 if(!Float.isNaN(this.maxWidth)) { 437 if(this.maxWidth < 0) { 438 throw new BuildException("Value of maxwidth parameter must positive."); 439 } 440 converter.setMaxWidth(this.maxWidth); 441 } 442 if(!Float.isNaN(this.maxHeight)) { 443 if(this.maxHeight < 0) { 444 throw new BuildException("Value of maxheight parameter must positive."); 445 } 446 converter.setMaxHeight(this.maxHeight); 447 } 448 if(allowedToSetQuality(resultType)) { 450 if(!Float.isNaN(this.quality)) { 451 converter.setQuality(getQuality(this.quality)); 453 } else { 454 converter.setQuality(DEFAULT_QUALITY); 457 } 458 } 459 if(this.area != null) { 460 converter.setArea(getAreaOfInterest(this.area)); 462 } 463 if(this.background != null) { 464 converter.setBackgroundColor(getBackgroundColor(this.background)); 466 } 467 if(this.mediaType != null) { 468 converter.setMediaType(this.mediaType); 470 } 471 if(!Float.isNaN(this.dpi)) { 472 if(this.dpi < 0) { 473 throw new BuildException("Value of dpi parameter must positive."); 474 } 475 converter.setPixelUnitToMillimeter(25.4f/this.dpi); 477 } 478 if(this.language != null) { 479 converter.setLanguage(this.language); 480 } 481 } 482 483 489 protected String [] getSourceFiles() { 490 491 List inputFiles = new ArrayList (); 493 if(this.srcFile != null) { 494 inputFiles.add(this.srcFile.getAbsolutePath()); 496 } else { 497 501 if(this.srcDir != null) { 505 fileset.setDir(this.srcDir); 509 DirectoryScanner ds = fileset.getDirectoryScanner(project); 510 String [] includedFiles = ds.getIncludedFiles(); 511 for (int j = 0 ; j < includedFiles.length ; j++) { 513 File newFile = new File (srcDir.getPath(), includedFiles[j]); 514 inputFiles.add(newFile.getAbsolutePath()); 515 } 516 } 517 for (int i = 0 ; i < filesets.size() ; i++) { 519 FileSet fs = (FileSet) filesets.elementAt(i); 522 DirectoryScanner ds = fs.getDirectoryScanner(project); 523 String [] includedFiles = ds.getIncludedFiles(); 524 for (int j = 0 ; j < includedFiles.length ; j++) { 526 File newFile = new File (fs.getDir(project).getPath(), includedFiles[j]); 527 inputFiles.add(newFile.getAbsolutePath()); 528 } 529 } 530 } 531 532 return (String [])inputFiles.toArray(new String [0]); 534 } 535 536 543 protected DestinationType getResultType(String type) { 544 if(type.equals(DestinationType.PNG_STR)) { 545 return DestinationType.PNG; 546 } else if(type.equals(DestinationType.JPEG_STR)) { 547 return DestinationType.JPEG; 548 } else if(type.equals(DestinationType.TIFF_STR)) { 549 return DestinationType.TIFF; 550 } else if(type.equals(DestinationType.PDF_STR)) { 551 return DestinationType.PDF; 552 } 553 return null; 554 } 555 556 564 protected boolean allowedToSetQuality(DestinationType type) { 565 if(!type.toString().equals(DestinationType.JPEG_STR)) { 566 return false; 567 } 568 return true; 569 } 570 571 580 protected float getQuality(float quality) 581 throws BuildException { 582 if((quality <= 0) || (quality >= 1)) { 583 throw new BuildException("quality parameter value have to be between 0 and 1."); 584 } 585 return quality; 586 } 587 588 597 protected Rectangle2D getAreaOfInterest(String area) 598 throws BuildException { 599 600 float x; float y; float width; float height; String token; StringTokenizer tokenizer = new StringTokenizer (area, ", \t\n\r\f"); 606 608 if(tokenizer.countTokens() != 4) { 609 throw new BuildException("There must be four numbers in the area parameter: x, y, width, and height."); 610 } 611 try { 612 x = Float.parseFloat(tokenizer.nextToken()); 613 y = Float.parseFloat(tokenizer.nextToken()); 614 width = Float.parseFloat(tokenizer.nextToken()); 615 height = Float.parseFloat(tokenizer.nextToken()); 616 } catch(NumberFormatException nfe) { 617 throw new BuildException("Invalid area parameter value: " + nfe.toString()); 618 } 619 620 if((x < 0) || (y < 0) || (width < 0) || (height < 0)) { 622 throw new BuildException("Negative values are not allowed in area parameter."); 623 } 624 625 return new Rectangle2D.Float (x, y, width, height); 626 } 627 628 637 protected Color getBackgroundColor(String argb) 638 throws BuildException { 639 640 int a; int r; int g; int b; String token; StringTokenizer tokenizer = new StringTokenizer (argb, ", \t\n\r\f"); 646 648 try { 649 if(tokenizer.countTokens() == 3) { 650 a = 255; 652 } else if(tokenizer.countTokens() == 4) { 653 a = Integer.parseInt(tokenizer.nextToken()); 654 } else { 655 throw new BuildException("There must be either three or four numbers in bg parameter: (alpha,) red, green, and blue."); 656 } 657 r = Integer.parseInt(tokenizer.nextToken()); 658 g = Integer.parseInt(tokenizer.nextToken()); 659 b = Integer.parseInt(tokenizer.nextToken()); 660 } catch(NumberFormatException nfe) { 661 throw new BuildException("Invalid bg parameter value: " + nfe.toString()); 662 } 663 664 if((a < 0) ||(a > 255) || (r < 0) ||(r > 255) || 666 (g < 0) ||(g > 255) || (b < 0) ||(b > 255)) { 667 throw new BuildException("bg parameter value is invalid. Numbers have to be between 0 and 255."); 668 } 669 670 return new Color (r, g, b, a); 671 } 672 673 683 private String getParserClassName(final String className) { 684 String name = className; 685 if(className.equals(JAXP_PARSER)) { 686 XMLReader reader = JAXPUtils.getXMLReader(); 689 name = reader.getClass().getName(); 690 } 691 692 log("Using class '" + name + "' to parse SVG documents.", Project.MSG_VERBOSE); 693 return name; 694 } 695 696 697 698 702 710 public static class ValidImageTypes extends EnumeratedAttribute { 711 712 717 public String [] getValues() { 718 return new String [] 719 {DestinationType.PNG_STR, 720 DestinationType.JPEG_STR, 721 DestinationType.TIFF_STR, 722 DestinationType.PDF_STR}; 723 } 724 } 725 726 734 public static class ValidMediaTypes extends EnumeratedAttribute { 735 736 744 public String [] getValues() { 745 return new String [] {"all", "handheld", "print", 746 "projection", "screen", "tty", "tv"}; 747 } 748 } 749 750 } 751 | Popular Tags |