KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > print > PrintTranscoder


1 /*
2
3    Copyright 1999-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17 */

18
19 package org.apache.batik.transcoder.print;
20
21 import java.awt.Graphics JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.RenderingHints JavaDoc;
24 import java.awt.Shape JavaDoc;
25 import java.awt.geom.AffineTransform JavaDoc;
26 import java.awt.geom.Rectangle2D JavaDoc;
27 import java.awt.print.PageFormat JavaDoc;
28 import java.awt.print.Paper JavaDoc;
29 import java.awt.print.Printable JavaDoc;
30 import java.awt.print.PrinterException JavaDoc;
31 import java.awt.print.PrinterJob JavaDoc;
32 import java.io.File JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34 import java.util.Vector JavaDoc;
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 JavaDoc;
50
51 /**
52  * This class is a <tt>Transcoder</tt> that prints SVG images.
53  * This class works as follows: any-time the transcode method
54  * is invoked, the corresponding input is cached and nothing
55  * else happens. <br />
56  * However, the <tt>PrintTranscoder</tt> is also a Printable. If used
57  * in a print operation, it will print each of the input
58  * it cached, one input per page.
59  * <br />
60  * The <tt>PrintTranscoder</tt> uses several different hints that
61  * guide its printing:<br />
62  * <ul>
63  * <li><tt>KEY_LANGUAGE, KEY_USER_STYLESHEET_URI, KEY_PIXEL_TO_MM,
64  * KEY_XML_PARSER_CLASSNAME</tt> can be used to set the defaults for
65  * the various SVG properties.</li>
66  * <li><tt>KEY_PAGE_WIDTH, KEY_PAGE_HEIGHT, KEY_MARGIN_TOP, KEY_MARGIN_BOTTOM,
67  * KEY_MARGIN_LEFT, KEY_MARGIN_RIGHT</tt> and <tt>KEY_PAGE_ORIENTATION</tt>
68  * can be used to specify the printing page characteristics.</li>
69  * <li><tt>KEY_WIDTH, KEY_HEIGHT</tt> can be used to specify how to scale the
70  * SVG image</li>
71  * <li><tt>KEY_SCALE_TO_PAGE</tt> can be used to specify whether or not the
72  * SVG image should be scaled uniformly to fit into the printed page or
73  * if it should just be centered into the printed page.</li>
74  * </ul>
75  *
76  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
77  * @version $Id: PrintTranscoder.java,v 1.30 2004/10/30 18:38:06 deweese Exp $
78  */

79 public class PrintTranscoder extends SVGAbstractTranscoder
80     implements Printable JavaDoc {
81
82     public static final String JavaDoc KEY_AOI_STR = "aoi";
83     public static final String JavaDoc KEY_HEIGHT_STR = "height";
84     public static final String JavaDoc KEY_LANGUAGE_STR = "language";
85     public static final String JavaDoc KEY_MARGIN_BOTTOM_STR = "marginBottom";
86     public static final String JavaDoc KEY_MARGIN_LEFT_STR = "marginLeft";
87     public static final String JavaDoc KEY_MARGIN_RIGHT_STR = "marginRight";
88     public static final String JavaDoc KEY_MARGIN_TOP_STR = "marginTop";
89     public static final String JavaDoc KEY_PAGE_HEIGHT_STR = "pageHeight";
90     public static final String JavaDoc KEY_PAGE_ORIENTATION_STR = "pageOrientation";
91     public static final String JavaDoc KEY_PAGE_WIDTH_STR = "pageWidth";
92     public static final String JavaDoc KEY_PIXEL_TO_MM_STR = "pixelToMm";
93     public static final String JavaDoc KEY_SCALE_TO_PAGE_STR = "scaleToPage";
94     public static final String JavaDoc KEY_SHOW_PAGE_DIALOG_STR = "showPageDialog";
95     public static final String JavaDoc KEY_SHOW_PRINTER_DIALOG_STR = "showPrinterDialog";
96     public static final String JavaDoc KEY_USER_STYLESHEET_URI_STR = "userStylesheet";
97     public static final String JavaDoc KEY_WIDTH_STR = "width";
98     public static final String JavaDoc KEY_XML_PARSER_CLASSNAME_STR = "xmlParserClassName";
99     public static final String JavaDoc VALUE_MEDIA_PRINT = "print";
100     public static final String JavaDoc VALUE_PAGE_ORIENTATION_LANDSCAPE = "landscape";
101     public static final String JavaDoc VALUE_PAGE_ORIENTATION_PORTRAIT = "portrait";
102     public static final String JavaDoc VALUE_PAGE_ORIENTATION_REVERSE_LANDSCAPE = "reverseLandscape";
103
104     /**
105      * Set of inputs this transcoder has been requested to
106      * transcode so far
107      */

108     private Vector JavaDoc inputs = new Vector JavaDoc();
109
110     /**
111      * Currently printing set of pages. This vector is
112      * created as a clone of inputs when the first page is printed.
113      */

114     private Vector JavaDoc printedInputs = null;
115
116     /**
117      * Index of the page corresponding to root
118      */

119     private int curIndex = -1;
120
121     /**
122      * Place to cache BridgeContext so we can dispose of it when
123      * it is appropriate. The Baseclass would dispose of it too
124      * soon.
125      */

126     private BridgeContext theCtx;
127
128     /**
129      * Constructs a new transcoder that prints images.
130      */

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     /**
146      * Transcodes the specified Document as an image in the specified output.
147      *
148      * @param document the document to transcode
149      * @param uri the uri of the document or null if any
150      * @param output the ouput where to transcode
151      * @exception TranscoderException if an error occured while transcoding
152      */

153     protected void transcode(Document JavaDoc document,
154                              String JavaDoc uri,
155                              TranscoderOutput output)
156             throws TranscoderException {
157         super.transcode(document, uri, output);
158
159         // We do this to hide 'ctx' from the SVGAbstractTranscoder
160
// otherwise it will dispose of the context before we can
161
// print the document.
162
theCtx = ctx;
163         ctx = null;
164     }
165     /**
166      * Convenience method
167      */

168     public void print() throws PrinterException JavaDoc{
169         //
170
// Now, request the transcoder to actually perform the
171
// printing job.
172
//
173
PrinterJob JavaDoc printerJob =
174             PrinterJob.getPrinterJob();
175
176         PageFormat JavaDoc pageFormat =
177             printerJob.defaultPage();
178
179         //
180
// Set the page parameters from the hints
181
//
182
Paper JavaDoc paper = pageFormat.getPaper();
183
184         Float JavaDoc pageWidth = (Float JavaDoc)hints.get(KEY_PAGE_WIDTH);
185         Float JavaDoc pageHeight = (Float JavaDoc)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 JavaDoc leftMargin = (Float JavaDoc)hints.get(KEY_MARGIN_LEFT);
199         Float JavaDoc topMargin = (Float JavaDoc)hints.get(KEY_MARGIN_TOP);
200         Float JavaDoc rightMargin = (Float JavaDoc)hints.get(KEY_MARGIN_RIGHT);
201         Float JavaDoc bottomMargin = (Float JavaDoc)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 JavaDoc pageOrientation = (String JavaDoc)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         //
235
// If required, pop up a dialog to adjust the page format
236
//
237
Boolean JavaDoc showPageFormat = (Boolean JavaDoc)hints.get(KEY_SHOW_PAGE_DIALOG);
238         if(showPageFormat != null && showPageFormat.booleanValue()){
239             PageFormat JavaDoc tmpPageFormat = printerJob.pageDialog(pageFormat);
240             if(tmpPageFormat == pageFormat){
241                 // Dialog was cancelled, meaning that the print process should
242
// be stopped.
243
return;
244             }
245
246             pageFormat = tmpPageFormat;
247         }
248
249         //
250
// If required, pop up a dialog to select the printer
251
//
252
Boolean JavaDoc showPrinterDialog = (Boolean JavaDoc)hints.get(KEY_SHOW_PRINTER_DIALOG);
253         if(showPrinterDialog != null && showPrinterDialog.booleanValue()){
254             if(!printerJob.printDialog()){
255                 // Dialog was cancelled, meaning that the print process
256
// should be stopped.
257
return;
258             }
259         }
260
261         // Print now
262
printerJob.setPrintable(this, pageFormat);
263         printerJob.print();
264
265     }
266
267     /**
268      * Printable implementation
269      */

270     public int print(Graphics JavaDoc _g, PageFormat JavaDoc pageFormat, int pageIndex){
271         //
272
// On the first page, take a snapshot of the vector of
273
// TranscodeInputs.
274
//
275
if(printedInputs == null){
276             printedInputs = (Vector JavaDoc)inputs.clone();
277         }
278
279         //
280
// If we have already printed each page, return
281
//
282
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         //
291
// Load a new document now if we are printing a new page
292
//
293
if(curIndex != pageIndex){
294             if (theCtx != null)
295                 theCtx.dispose();
296
297             // The following call will invoke this class' transcode
298
// method which takes a document as an input. That method
299
// builds the GVT root tree.{
300
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         // Cast to Graphics2D to access Java 2D features
313
Graphics2D JavaDoc g = (Graphics2D JavaDoc)_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         //
322
// Compute transform so that the SVG document fits on one page
323
//
324
AffineTransform JavaDoc t = g.getTransform();
325         Shape JavaDoc clip = g.getClip();
326
327         //
328
// Append transform to selected area
329
//
330
g.transform(curTxf);
331
332         //
333
// Delegate rendering to painter
334
//
335
try{
336             root.paint(g);
337         }catch(Exception JavaDoc e){
338             g.setTransform(t);
339             g.setClip(clip);
340             drawError(_g, e);
341         }
342
343         //
344
// Restore transform and clip
345
//
346
g.setTransform(t);
347         g.setClip(clip);
348
349         // g.setPaint(Color.black);
350
// g.drawString(uris[pageIndex], 30, 30);
351

352
353         //
354
// Return status indicated that we did paint a page
355
//
356
return PAGE_EXISTS;
357     }
358
359     /**
360      * Sets document size according to the hints.
361      * Global variables width and height are modified.
362      *
363      * @param docWidth Width of the document.
364      * @param docHeight Height of the document.
365      */

366     protected void setImageSize(float docWidth, float docHeight) {
367         // Check hint to know if scaling is really needed
368
Boolean JavaDoc scaleToPage = (Boolean JavaDoc)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 JavaDoc aoi = (Rectangle2D JavaDoc)hints.get(KEY_AOI);
374                 w = (float)aoi.getWidth();
375                 h = (float)aoi.getHeight();
376             }
377             super.setImageSize(w, h);
378         }
379     }
380
381     /**
382      * Prints an error on the output page
383      */

384     private void drawError(Graphics JavaDoc g, Exception JavaDoc e){
385         userAgent.displayError(e);
386         // Should also probably draw exception on page.
387
}
388
389     // --------------------------------------------------------------------
390
// Keys definition
391
// --------------------------------------------------------------------
392

393     /**
394      * The showPageDialog key.
395      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
396      * <TR>
397      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
398      * <TD VALIGN="TOP">KEY_SHOW_PAGE_DIALOG</TD></TR>
399      * <TR>
400      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
401      * <TD VALIGN="TOP">Boolean</TD></TR>
402      * <TR>
403      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
404      * <TD VALIGN="TOP">false</TD></TR>
405      * <TR>
406      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
407      * <TD VALIGN="TOP">No</TD></TR>
408      * <TR>
409      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
410      * <TD VALIGN="TOP">Specifies whether or not the transcoder
411      * should pop up a dialog box for selecting
412      * the page format.</TD></TR>
413      * </TABLE> */

414     public static final TranscodingHints.Key KEY_SHOW_PAGE_DIALOG
415         = new BooleanKey();
416
417     /**
418      * The showPrinterDialog key.
419      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
420      * <TR>
421      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
422      * <TD VALIGN="TOP">KEY_SHOW_PAGE_DIALOG</TD></TR>
423      * <TR>
424      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
425      * <TD VALIGN="TOP">Boolean</TD></TR>
426      * <TR>
427      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
428      * <TD VALIGN="TOP">false</TD></TR>
429      * <TR>
430      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
431      * <TD VALIGN="TOP">No</TD></TR>
432      * <TR>
433      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
434      * <TD VALIGN="TOP">Specifies whether or not the transcoder
435      * should pop up a dialog box for selecting
436      * the printer. If the dialog box is not
437      * shown, the transcoder will use the default
438      * printer.</TD></TR>
439      * </TABLE> */

440     public static final TranscodingHints.Key KEY_SHOW_PRINTER_DIALOG
441         = new BooleanKey();
442
443
444     /**
445      * The pageWidth key.
446      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
447      * <TR>
448      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
449      * <TD VALIGN="TOP">KEY_PAGE_WIDTH</TD></TR>
450      * <TR>
451      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
452      * <TD VALIGN="TOP">Length</TD></TR>
453      * <TR>
454      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
455      * <TD VALIGN="TOP">None</TD></TR>
456      * <TR>
457      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
458      * <TD VALIGN="TOP">No</TD></TR>
459      * <TR>
460      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
461      * <TD VALIGN="TOP">The width of the print page</TD></TR>
462      * </TABLE> */

463     public static final TranscodingHints.Key KEY_PAGE_WIDTH
464         = new LengthKey();
465
466     /**
467      * The pageHeight key.
468      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
469      * <TR>
470      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
471      * <TD VALIGN="TOP">KEY_PAGE_HEIGHT</TD></TR>
472      * <TR>
473      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
474      * <TD VALIGN="TOP">Length</TD></TR>
475      * <TR>
476      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
477      * <TD VALIGN="TOP">None</TD></TR>
478      * <TR>
479      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
480      * <TD VALIGN="TOP">No</TD></TR>
481      * <TR>
482      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
483      * <TD VALIGN="TOP">The height of the print page</TD></TR>
484      * </TABLE> */

485     public static final TranscodingHints.Key KEY_PAGE_HEIGHT
486         = new LengthKey();
487
488     /**
489      * The marginTop key.
490      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
491      * <TR>
492      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
493      * <TD VALIGN="TOP">KEY_MARGIN_TOP</TD></TR>
494      * <TR>
495      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
496      * <TD VALIGN="TOP">Length</TD></TR>
497      * <TR>
498      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
499      * <TD VALIGN="TOP">None</TD></TR>
500      * <TR>
501      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
502      * <TD VALIGN="TOP">No</TD></TR>
503      * <TR>
504      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
505      * <TD VALIGN="TOP">The print page top margin</TD></TR>
506      * </TABLE> */

507     public static final TranscodingHints.Key KEY_MARGIN_TOP
508         = new LengthKey();
509
510     /**
511      * The marginRight key.
512      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
513      * <TR>
514      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
515      * <TD VALIGN="TOP">KEY_MARGIN_RIGHT</TD></TR>
516      * <TR>
517      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
518      * <TD VALIGN="TOP">Length</TD></TR>
519      * <TR>
520      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
521      * <TD VALIGN="TOP">None</TD></TR>
522      * <TR>
523      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
524      * <TD VALIGN="TOP">No</TD></TR>
525      * <TR>
526      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
527      * <TD VALIGN="TOP">The print page right margin</TD></TR>
528      * </TABLE> */

529     public static final TranscodingHints.Key KEY_MARGIN_RIGHT
530         = new LengthKey();
531
532     /**
533      * The marginBottom key.
534      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
535      * <TR>
536      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
537      * <TD VALIGN="TOP">KEY_MARGIN_BOTTOM</TD></TR>
538      * <TR>
539      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
540      * <TD VALIGN="TOP">Length</TD></TR>
541      * <TR>
542      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
543      * <TD VALIGN="TOP">None</TD></TR>
544      * <TR>
545      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
546      * <TD VALIGN="TOP">No</TD></TR>
547      * <TR>
548      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
549      * <TD VALIGN="TOP">The print page bottom margin</TD></TR>
550      * </TABLE> */

551     public static final TranscodingHints.Key KEY_MARGIN_BOTTOM
552         = new LengthKey();
553
554     /**
555      * The marginLeft key.
556      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
557      * <TR>
558      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
559      * <TD VALIGN="TOP">KEY_MARGIN_LEFT</TD></TR>
560      * <TR>
561      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
562      * <TD VALIGN="TOP">Length</TD></TR>
563      * <TR>
564      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
565      * <TD VALIGN="TOP">None</TD></TR>
566      * <TR>
567      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
568      * <TD VALIGN="TOP">No</TD></TR>
569      * <TR>
570      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
571      * <TD VALIGN="TOP">The print page left margin</TD></TR>
572      * </TABLE> */

573     public static final TranscodingHints.Key KEY_MARGIN_LEFT
574         = new LengthKey();
575
576     /**
577      * The pageOrientation key.
578      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
579      * <TR>
580      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
581      * <TD VALIGN="TOP">KEY_PAGE_ORIENTATION</TD></TR>
582      * <TR>
583      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
584      * <TD VALIGN="TOP">String</TD></TR>
585      * <TR>
586      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
587      * <TD VALIGN="TOP">VALUE_PAGE_ORIENTATION_PORTRAIT</TD></TR>
588      * <TR>
589      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
590      * <TD VALIGN="TOP">No</TD></TR>
591      * <TR>
592      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
593      * <TD VALIGN="TOP">The print page's orientation</TD></TR>
594      * </TABLE> */

595     public static final TranscodingHints.Key KEY_PAGE_ORIENTATION
596         = new StringKey();
597
598
599     /**
600      * The scaleToPage key.
601      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
602      * <TR>
603      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
604      * <TD VALIGN="TOP">KEY_SCALE_TO_PAGE</TD></TR>
605      * <TR>
606      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
607      * <TD VALIGN="TOP">Boolean</TD></TR>
608      * <TR>
609      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
610      * <TD VALIGN="TOP">true</TD></TR>
611      * <TR>
612      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
613      * <TD VALIGN="TOP">No</TD></TR>
614      * <TR>
615      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
616      * <TD VALIGN="TOP">Specifies whether or not the SVG images are scaled to
617      * fit into the printed page</TD></TR>
618      * </TABLE> */

619     public static final TranscodingHints.Key KEY_SCALE_TO_PAGE
620         = new BooleanKey();
621
622     public static final String JavaDoc USAGE = "java org.apache.batik.transcoder.print.PrintTranscoder <svgFileToPrint>";
623
624     public static void main(String JavaDoc args[]) throws Exception JavaDoc{
625         if(args.length < 1){
626             System.err.println(USAGE);
627             System.exit(0);
628         }
629
630         //
631
// Builds a PrintTranscoder
632
//
633
PrintTranscoder transcoder = new PrintTranscoder();
634
635         //
636
// Set the hints, from the command line arguments
637
//
638

639         // Language
640
setTranscoderFloatHint(transcoder,
641                                KEY_LANGUAGE_STR,
642                                KEY_LANGUAGE);
643
644         // User stylesheet
645
setTranscoderFloatHint(transcoder,
646                                KEY_USER_STYLESHEET_URI_STR,
647                                KEY_USER_STYLESHEET_URI);
648
649         // XML parser
650
setTranscoderStringHint(transcoder,
651                                  KEY_XML_PARSER_CLASSNAME_STR,
652                                  KEY_XML_PARSER_CLASSNAME);
653
654         // Scale to page
655
setTranscoderBooleanHint(transcoder,
656                                  KEY_SCALE_TO_PAGE_STR,
657                                  KEY_SCALE_TO_PAGE);
658
659         // AOI
660
setTranscoderRectangleHint(transcoder,
661                                    KEY_AOI_STR,
662                                    KEY_AOI);
663
664
665         // Image size
666
setTranscoderFloatHint(transcoder,
667                                KEY_WIDTH_STR,
668                                KEY_WIDTH);
669         setTranscoderFloatHint(transcoder,
670                                KEY_HEIGHT_STR,
671                                KEY_HEIGHT);
672
673         // Pixel to millimeter
674
setTranscoderFloatHint(transcoder,
675                                KEY_PIXEL_TO_MM_STR,
676                                KEY_PIXEL_UNIT_TO_MILLIMETER);
677
678         // Page orientation
679
setTranscoderStringHint(transcoder,
680                                 KEY_PAGE_ORIENTATION_STR,
681                                 KEY_PAGE_ORIENTATION);
682
683         // Page size
684
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         // Margins
692
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         // Dialog options
706
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         //
715
// First, request the transcoder to transcode
716
// each of the input files
717
//
718
for(int i=0; i<args.length; i++){
719             transcoder.transcode(new TranscoderInput(new File JavaDoc(args[i]).toURL().toString()),
720                                  null);
721         }
722
723         //
724
// Now, print...
725
//
726
transcoder.print();
727
728         System.exit(0);
729     }
730
731     public static void setTranscoderFloatHint(Transcoder transcoder,
732                                               String JavaDoc property,
733                                               TranscodingHints.Key key){
734         String JavaDoc str = System.getProperty(property);
735         if(str != null){
736             try{
737                 Float JavaDoc value = new Float JavaDoc(Float.parseFloat(str));
738                 transcoder.addTranscodingHint(key, value);
739             }catch(NumberFormatException JavaDoc e){
740                 handleValueError(property, str);
741             }
742         }
743     }
744
745     public static void setTranscoderRectangleHint(Transcoder transcoder,
746                                                   String JavaDoc property,
747                                                   TranscodingHints.Key key){
748         String JavaDoc str = System.getProperty(property);
749         if(str != null){
750             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str, " ,");
751             if(st.countTokens() != 4){
752                 handleValueError(property, str);
753             }
754
755             try{
756                 String JavaDoc x = st.nextToken();
757                 String JavaDoc y = st.nextToken();
758                 String JavaDoc width = st.nextToken();
759                 String JavaDoc height = st.nextToken();
760                 Rectangle2D JavaDoc r = new Rectangle2D.Float JavaDoc(Float.parseFloat(x),
761                                                       Float.parseFloat(y),
762                                                       Float.parseFloat(width),
763                                                       Float.parseFloat(height));
764                 transcoder.addTranscodingHint(key, r);
765             }catch(NumberFormatException JavaDoc e){
766                 handleValueError(property, str);
767             }
768         }
769     }
770
771     public static void setTranscoderBooleanHint(Transcoder transcoder,
772                                                 String JavaDoc property,
773                                                 TranscodingHints.Key key){
774         String JavaDoc str = System.getProperty(property);
775         if(str != null){
776             Boolean JavaDoc value = new Boolean JavaDoc("true".equalsIgnoreCase(str));
777             transcoder.addTranscodingHint(key, value);
778         }
779     }
780
781     public static void setTranscoderStringHint(Transcoder transcoder,
782                                               String JavaDoc property,
783                                               TranscodingHints.Key key){
784         String JavaDoc str = System.getProperty(property);
785         if(str != null){
786             transcoder.addTranscodingHint(key, str);
787         }
788     }
789
790     public static void handleValueError(String JavaDoc property,
791                                         String JavaDoc value){
792         System.err.println("Invalid " + property + " value : " + value);
793         System.exit(1);
794     }
795 }
796
797
798
799
800
801
Popular Tags