1 18 19 package org.apache.batik.transcoder.print; 20 21 import java.awt.Graphics ; 22 import java.awt.Graphics2D ; 23 import java.awt.RenderingHints ; 24 import java.awt.Shape ; 25 import java.awt.geom.AffineTransform ; 26 import java.awt.geom.Rectangle2D ; 27 import java.awt.print.PageFormat ; 28 import java.awt.print.Paper ; 29 import java.awt.print.Printable ; 30 import java.awt.print.PrinterException ; 31 import java.awt.print.PrinterJob ; 32 import java.io.File ; 33 import java.util.StringTokenizer ; 34 import java.util.Vector ; 35 36 import org.apache.batik.bridge.BridgeContext; 37 import org.apache.batik.ext.awt.RenderingHintsKeyExt; 38 import org.apache.batik.transcoder.SVGAbstractTranscoder; 39 import org.apache.batik.transcoder.XMLAbstractTranscoder; 40 import org.apache.batik.transcoder.Transcoder; 41 import org.apache.batik.transcoder.TranscoderException; 42 import org.apache.batik.transcoder.TranscoderInput; 43 import org.apache.batik.transcoder.TranscoderOutput; 44 import org.apache.batik.transcoder.TranscodingHints; 45 import org.apache.batik.transcoder.keys.BooleanKey; 46 import org.apache.batik.transcoder.keys.LengthKey; 47 import org.apache.batik.transcoder.keys.StringKey; 48 49 import org.w3c.dom.Document ; 50 51 79 public class PrintTranscoder extends SVGAbstractTranscoder 80 implements Printable { 81 82 public static final String KEY_AOI_STR = "aoi"; 83 public static final String KEY_HEIGHT_STR = "height"; 84 public static final String KEY_LANGUAGE_STR = "language"; 85 public static final String KEY_MARGIN_BOTTOM_STR = "marginBottom"; 86 public static final String KEY_MARGIN_LEFT_STR = "marginLeft"; 87 public static final String KEY_MARGIN_RIGHT_STR = "marginRight"; 88 public static final String KEY_MARGIN_TOP_STR = "marginTop"; 89 public static final String KEY_PAGE_HEIGHT_STR = "pageHeight"; 90 public static final String KEY_PAGE_ORIENTATION_STR = "pageOrientation"; 91 public static final String KEY_PAGE_WIDTH_STR = "pageWidth"; 92 public static final String KEY_PIXEL_TO_MM_STR = "pixelToMm"; 93 public static final String KEY_SCALE_TO_PAGE_STR = "scaleToPage"; 94 public static final String KEY_SHOW_PAGE_DIALOG_STR = "showPageDialog"; 95 public static final String KEY_SHOW_PRINTER_DIALOG_STR = "showPrinterDialog"; 96 public static final String KEY_USER_STYLESHEET_URI_STR = "userStylesheet"; 97 public static final String KEY_WIDTH_STR = "width"; 98 public static final String KEY_XML_PARSER_CLASSNAME_STR = "xmlParserClassName"; 99 public static final String VALUE_MEDIA_PRINT = "print"; 100 public static final String VALUE_PAGE_ORIENTATION_LANDSCAPE = "landscape"; 101 public static final String VALUE_PAGE_ORIENTATION_PORTRAIT = "portrait"; 102 public static final String VALUE_PAGE_ORIENTATION_REVERSE_LANDSCAPE = "reverseLandscape"; 103 104 108 private Vector inputs = new Vector (); 109 110 114 private Vector printedInputs = null; 115 116 119 private int curIndex = -1; 120 121 126 private BridgeContext theCtx; 127 128 131 public PrintTranscoder() { 132 super(); 133 134 hints.put(KEY_MEDIA, 135 VALUE_MEDIA_PRINT); 136 } 137 138 public void transcode(TranscoderInput in, 139 TranscoderOutput out){ 140 if(in != null){ 141 inputs.addElement(in); 142 } 143 } 144 145 153 protected void transcode(Document document, 154 String uri, 155 TranscoderOutput output) 156 throws TranscoderException { 157 super.transcode(document, uri, output); 158 159 theCtx = ctx; 163 ctx = null; 164 } 165 168 public void print() throws PrinterException { 169 PrinterJob printerJob = 174 PrinterJob.getPrinterJob(); 175 176 PageFormat pageFormat = 177 printerJob.defaultPage(); 178 179 Paper paper = pageFormat.getPaper(); 183 184 Float pageWidth = (Float )hints.get(KEY_PAGE_WIDTH); 185 Float pageHeight = (Float )hints.get(KEY_PAGE_HEIGHT); 186 if(pageWidth != null){ 187 paper.setSize(pageWidth.floatValue(), 188 paper.getHeight()); 189 } 190 if(pageHeight != null){ 191 paper.setSize(paper.getWidth(), 192 pageHeight.floatValue()); 193 } 194 195 float x=0, y=0; 196 float width=(float)paper.getWidth(), height=(float)paper.getHeight(); 197 198 Float leftMargin = (Float )hints.get(KEY_MARGIN_LEFT); 199 Float topMargin = (Float )hints.get(KEY_MARGIN_TOP); 200 Float rightMargin = (Float )hints.get(KEY_MARGIN_RIGHT); 201 Float bottomMargin = (Float )hints.get(KEY_MARGIN_BOTTOM); 202 203 if(leftMargin != null){ 204 x = leftMargin.floatValue(); 205 width -= leftMargin.floatValue(); 206 } 207 if(topMargin != null){ 208 y = topMargin.floatValue(); 209 height -= topMargin.floatValue(); 210 } 211 if(rightMargin != null){ 212 width -= rightMargin.floatValue(); 213 } 214 if(bottomMargin != null){ 215 height -= bottomMargin.floatValue(); 216 } 217 218 paper.setImageableArea(x, y, width, height); 219 220 String pageOrientation = (String )hints.get(KEY_PAGE_ORIENTATION); 221 if(VALUE_PAGE_ORIENTATION_PORTRAIT.equalsIgnoreCase(pageOrientation)){ 222 pageFormat.setOrientation(PageFormat.PORTRAIT); 223 } 224 else if(VALUE_PAGE_ORIENTATION_LANDSCAPE.equalsIgnoreCase(pageOrientation)){ 225 pageFormat.setOrientation(PageFormat.LANDSCAPE); 226 } 227 else if(VALUE_PAGE_ORIENTATION_REVERSE_LANDSCAPE.equalsIgnoreCase(pageOrientation)){ 228 pageFormat.setOrientation(PageFormat.REVERSE_LANDSCAPE); 229 } 230 231 pageFormat.setPaper(paper); 232 pageFormat = printerJob.validatePage(pageFormat); 233 234 Boolean showPageFormat = (Boolean )hints.get(KEY_SHOW_PAGE_DIALOG); 238 if(showPageFormat != null && showPageFormat.booleanValue()){ 239 PageFormat tmpPageFormat = printerJob.pageDialog(pageFormat); 240 if(tmpPageFormat == pageFormat){ 241 return; 244 } 245 246 pageFormat = tmpPageFormat; 247 } 248 249 Boolean showPrinterDialog = (Boolean )hints.get(KEY_SHOW_PRINTER_DIALOG); 253 if(showPrinterDialog != null && showPrinterDialog.booleanValue()){ 254 if(!printerJob.printDialog()){ 255 return; 258 } 259 } 260 261 printerJob.setPrintable(this, pageFormat); 263 printerJob.print(); 264 265 } 266 267 270 public int print(Graphics _g, PageFormat pageFormat, int pageIndex){ 271 if(printedInputs == null){ 276 printedInputs = (Vector )inputs.clone(); 277 } 278 279 if(pageIndex >= printedInputs.size()){ 283 curIndex = -1; 284 if (theCtx != null) 285 theCtx.dispose(); 286 userAgent.displayMessage("Done"); 287 return NO_SUCH_PAGE; 288 } 289 290 if(curIndex != pageIndex){ 294 if (theCtx != null) 295 theCtx.dispose(); 296 297 try{ 301 width = (int)(pageFormat.getImageableWidth()+0.5); 302 height = (int)(pageFormat.getImageableHeight()+0.5); 303 super.transcode 304 ((TranscoderInput)printedInputs.elementAt(pageIndex),null); 305 curIndex = pageIndex; 306 }catch(TranscoderException e){ 307 drawError(_g, e); 308 return PAGE_EXISTS; 309 } 310 } 311 312 Graphics2D g = (Graphics2D )_g; 314 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 315 RenderingHints.VALUE_ANTIALIAS_ON); 316 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 317 RenderingHints.VALUE_INTERPOLATION_BILINEAR); 318 g.setRenderingHint(RenderingHintsKeyExt.KEY_TRANSCODING, 319 RenderingHintsKeyExt.VALUE_TRANSCODING_PRINTING); 320 321 AffineTransform t = g.getTransform(); 325 Shape clip = g.getClip(); 326 327 g.transform(curTxf); 331 332 try{ 336 root.paint(g); 337 }catch(Exception e){ 338 g.setTransform(t); 339 g.setClip(clip); 340 drawError(_g, e); 341 } 342 343 g.setTransform(t); 347 g.setClip(clip); 348 349 352 353 return PAGE_EXISTS; 357 } 358 359 366 protected void setImageSize(float docWidth, float docHeight) { 367 Boolean scaleToPage = (Boolean )hints.get(KEY_SCALE_TO_PAGE); 369 if(scaleToPage != null && !scaleToPage.booleanValue()) { 370 float w = docWidth; 371 float h = docHeight; 372 if (hints.containsKey(KEY_AOI)) { 373 Rectangle2D aoi = (Rectangle2D )hints.get(KEY_AOI); 374 w = (float)aoi.getWidth(); 375 h = (float)aoi.getHeight(); 376 } 377 super.setImageSize(w, h); 378 } 379 } 380 381 384 private void drawError(Graphics g, Exception e){ 385 userAgent.displayError(e); 386 } 388 389 393 414 public static final TranscodingHints.Key KEY_SHOW_PAGE_DIALOG 415 = new BooleanKey(); 416 417 440 public static final TranscodingHints.Key KEY_SHOW_PRINTER_DIALOG 441 = new BooleanKey(); 442 443 444 463 public static final TranscodingHints.Key KEY_PAGE_WIDTH 464 = new LengthKey(); 465 466 485 public static final TranscodingHints.Key KEY_PAGE_HEIGHT 486 = new LengthKey(); 487 488 507 public static final TranscodingHints.Key KEY_MARGIN_TOP 508 = new LengthKey(); 509 510 529 public static final TranscodingHints.Key KEY_MARGIN_RIGHT 530 = new LengthKey(); 531 532 551 public static final TranscodingHints.Key KEY_MARGIN_BOTTOM 552 = new LengthKey(); 553 554 573 public static final TranscodingHints.Key KEY_MARGIN_LEFT 574 = new LengthKey(); 575 576 595 public static final TranscodingHints.Key KEY_PAGE_ORIENTATION 596 = new StringKey(); 597 598 599 619 public static final TranscodingHints.Key KEY_SCALE_TO_PAGE 620 = new BooleanKey(); 621 622 public static final String USAGE = "java org.apache.batik.transcoder.print.PrintTranscoder <svgFileToPrint>"; 623 624 public static void main(String args[]) throws Exception { 625 if(args.length < 1){ 626 System.err.println(USAGE); 627 System.exit(0); 628 } 629 630 PrintTranscoder transcoder = new PrintTranscoder(); 634 635 639 setTranscoderFloatHint(transcoder, 641 KEY_LANGUAGE_STR, 642 KEY_LANGUAGE); 643 644 setTranscoderFloatHint(transcoder, 646 KEY_USER_STYLESHEET_URI_STR, 647 KEY_USER_STYLESHEET_URI); 648 649 setTranscoderStringHint(transcoder, 651 KEY_XML_PARSER_CLASSNAME_STR, 652 KEY_XML_PARSER_CLASSNAME); 653 654 setTranscoderBooleanHint(transcoder, 656 KEY_SCALE_TO_PAGE_STR, 657 KEY_SCALE_TO_PAGE); 658 659 setTranscoderRectangleHint(transcoder, 661 KEY_AOI_STR, 662 KEY_AOI); 663 664 665 setTranscoderFloatHint(transcoder, 667 KEY_WIDTH_STR, 668 KEY_WIDTH); 669 setTranscoderFloatHint(transcoder, 670 KEY_HEIGHT_STR, 671 KEY_HEIGHT); 672 673 setTranscoderFloatHint(transcoder, 675 KEY_PIXEL_TO_MM_STR, 676 KEY_PIXEL_UNIT_TO_MILLIMETER); 677 678 setTranscoderStringHint(transcoder, 680 KEY_PAGE_ORIENTATION_STR, 681 KEY_PAGE_ORIENTATION); 682 683 setTranscoderFloatHint(transcoder, 685 KEY_PAGE_WIDTH_STR, 686 KEY_PAGE_WIDTH); 687 setTranscoderFloatHint(transcoder, 688 KEY_PAGE_HEIGHT_STR, 689 KEY_PAGE_HEIGHT); 690 691 setTranscoderFloatHint(transcoder, 693 KEY_MARGIN_TOP_STR, 694 KEY_MARGIN_TOP); 695 setTranscoderFloatHint(transcoder, 696 KEY_MARGIN_RIGHT_STR, 697 KEY_MARGIN_RIGHT); 698 setTranscoderFloatHint(transcoder, 699 KEY_MARGIN_BOTTOM_STR, 700 KEY_MARGIN_BOTTOM); 701 setTranscoderFloatHint(transcoder, 702 KEY_MARGIN_LEFT_STR, 703 KEY_MARGIN_LEFT); 704 705 setTranscoderBooleanHint(transcoder, 707 KEY_SHOW_PAGE_DIALOG_STR, 708 KEY_SHOW_PAGE_DIALOG); 709 710 setTranscoderBooleanHint(transcoder, 711 KEY_SHOW_PRINTER_DIALOG_STR, 712 KEY_SHOW_PRINTER_DIALOG); 713 714 for(int i=0; i<args.length; i++){ 719 transcoder.transcode(new TranscoderInput(new File (args[i]).toURL().toString()), 720 null); 721 } 722 723 transcoder.print(); 727 728 System.exit(0); 729 } 730 731 public static void setTranscoderFloatHint(Transcoder transcoder, 732 String property, 733 TranscodingHints.Key key){ 734 String str = System.getProperty(property); 735 if(str != null){ 736 try{ 737 Float value = new Float (Float.parseFloat(str)); 738 transcoder.addTranscodingHint(key, value); 739 }catch(NumberFormatException e){ 740 handleValueError(property, str); 741 } 742 } 743 } 744 745 public static void setTranscoderRectangleHint(Transcoder transcoder, 746 String property, 747 TranscodingHints.Key key){ 748 String str = System.getProperty(property); 749 if(str != null){ 750 StringTokenizer st = new StringTokenizer (str, " ,"); 751 if(st.countTokens() != 4){ 752 handleValueError(property, str); 753 } 754 755 try{ 756 String x = st.nextToken(); 757 String y = st.nextToken(); 758 String width = st.nextToken(); 759 String height = st.nextToken(); 760 Rectangle2D r = new Rectangle2D.Float (Float.parseFloat(x), 761 Float.parseFloat(y), 762 Float.parseFloat(width), 763 Float.parseFloat(height)); 764 transcoder.addTranscodingHint(key, r); 765 }catch(NumberFormatException e){ 766 handleValueError(property, str); 767 } 768 } 769 } 770 771 public static void setTranscoderBooleanHint(Transcoder transcoder, 772 String property, 773 TranscodingHints.Key key){ 774 String str = System.getProperty(property); 775 if(str != null){ 776 Boolean value = new Boolean ("true".equalsIgnoreCase(str)); 777 transcoder.addTranscodingHint(key, value); 778 } 779 } 780 781 public static void setTranscoderStringHint(Transcoder transcoder, 782 String property, 783 TranscodingHints.Key key){ 784 String str = System.getProperty(property); 785 if(str != null){ 786 transcoder.addTranscodingHint(key, str); 787 } 788 } 789 790 public static void handleValueError(String property, 791 String value){ 792 System.err.println("Invalid " + property + " value : " + value); 793 System.exit(1); 794 } 795 } 796 797 798 799 800 801 | Popular Tags |