KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > svg > SVGRenderer


1 /*
2  * $Id: SVGRenderer.java,v 1.3.2.8 2003/03/02 16:55:18 pietsch Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.render.svg;
52
53 import org.apache.fop.layout.*;
54 import org.apache.fop.layout.inline.*;
55 import org.apache.fop.datatypes.IDReferences;
56 import org.apache.fop.datatypes.ColorType;
57 import org.apache.fop.image.*;
58 import org.apache.fop.svg.SVGArea;
59 import org.apache.fop.svg.SVGUtilities;
60 import org.apache.fop.apps.FOPException;
61
62 import org.w3c.dom.Node JavaDoc;
63 import org.w3c.dom.ProcessingInstruction JavaDoc;
64 import org.w3c.dom.svg.SVGSVGElement;
65 import org.w3c.dom.svg.SVGDocument;
66 import org.w3c.dom.Document JavaDoc;
67 import org.w3c.dom.Element JavaDoc;
68 import org.w3c.dom.DOMImplementation JavaDoc;
69
70 import org.apache.batik.dom.svg.SVGDOMImplementation;
71 import org.apache.batik.dom.util.XMLSupport;
72 import org.apache.batik.transcoder.svg2svg.SVGTranscoder;
73 import org.apache.batik.transcoder.TranscoderInput;
74 import org.apache.batik.transcoder.TranscoderOutput;
75 import org.apache.batik.transcoder.TranscoderException;
76
77 import java.awt.Color JavaDoc;
78 import java.awt.Image JavaDoc;
79 import java.awt.image.BufferedImage JavaDoc;
80 import java.awt.geom.Rectangle2D JavaDoc;
81 import java.util.Map JavaDoc;
82 import java.net.URL JavaDoc;
83 import java.net.MalformedURLException JavaDoc;
84 import java.io.OutputStream JavaDoc;
85 import java.io.IOException JavaDoc;
86 import java.io.OutputStreamWriter JavaDoc;
87 import javax.swing.ImageIcon JavaDoc;
88
89 import org.apache.fop.render.AbstractRenderer;
90
91 public class SVGRenderer extends AbstractRenderer {
92     static final String JavaDoc svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
93     Document svgDocument;
94     Element svgRoot;
95     Element currentPageG = null;
96     Element lastLink = null;
97
98     float totalWidth = 0;
99     float totalHeight = 0;
100
101     protected int pageWidth = 0;
102     protected int pageHeight = 0;
103     protected int pageNumber = 0;
104
105     protected Map JavaDoc fontNames = new java.util.HashMap JavaDoc();
106     protected Map JavaDoc fontStyles = new java.util.HashMap JavaDoc();
107     protected Color JavaDoc saveColor = null;
108
109     /**
110      * The current (internal) font name
111      */

112     protected String JavaDoc currentFontName;
113
114     /**
115      * The current font size in millipoints
116      */

117     protected int currentFontSize;
118
119     /**
120      * The current colour's red, green and blue component
121      */

122     protected float currentRed = 0;
123     protected float currentGreen = 0;
124     protected float currentBlue = 0;
125
126     /**
127      * options
128      */

129     protected java.util.Map JavaDoc options;
130
131     /**
132      * set up renderer options
133      */

134     public void setOptions(java.util.Map JavaDoc options) {
135         this.options = options;
136     }
137
138     public SVGRenderer() {
139     }
140
141     /**
142      * add a line to the current stream
143      *
144      * @param x1 the start x location in millipoints
145      * @param y1 the start y location in millipoints
146      * @param x2 the end x location in millipoints
147      * @param y2 the end y location in millipoints
148      * @param th the thickness in millipoints
149      * @param r the red component
150      * @param g the green component
151      * @param b the blue component
152      */

153     protected void addLine(int x1, int y1, int x2, int y2, int th, float r,
154                            float g, float b) {
155         Element line = SVGUtilities.createLine(svgDocument, x1 / 1000f, pageHeight - (y1 / 1000f), x2 / 1000f, pageHeight - (y2 / 1000f));
156         line.setAttributeNS(null, "style", "stroke-width:" + (Math.abs(th) / 1000f)
157                             + ";stroke:rgb(" + ((int)(255 * r)) + "," + ((int)(255 * g)) + "," + ((int)(255 * b)) + ")");
158         currentPageG.appendChild(line);
159     }
160
161     /**
162      * draw a rectangle
163      *
164      * @param x the x position of left edge in millipoints
165      * @param y the y position of top edge in millipoints
166      * @param w the width in millipoints
167      * @param h the height in millipoints
168      * @param r the red component
169      * @param g the green component
170      * @param b the blue component
171      */

172     protected void addRect(int x, int y, int w, int h, float r, float g,
173                            float b) {
174         Element rect = SVGUtilities.createRect(svgDocument, x / 1000f, pageHeight - (y / 1000f), w / 1000f, h / 1000f);
175         rect.setAttributeNS(null, "style", "stroke:rgb(" + ((int)(255 * r)) + "," + ((int)(255 * g)) + "," + ((int)(255 * b)) + ")");
176         currentPageG.appendChild(rect);
177     }
178
179     /**
180      * draw a filled rectangle
181      *
182      * @param x the x position of left edge in millipoints
183      * @param y the y position of top edge in millipoints
184      * @param w the width in millipoints
185      * @param h the height in millipoints
186      * @param r the red component of edges
187      * @param g the green component of edges
188      * @param b the blue component of edges
189      * @param fr the red component of the fill
190      * @param fg the green component of the fill
191      * @param fb the blue component of the fill
192      */

193     protected void addRect(int x, int y, int w, int h, float r, float g,
194                            float b, float fr, float fg, float fb) {
195         Element rect = SVGUtilities.createRect(svgDocument, x / 1000f, pageHeight - (y / 1000f), w / 1000f, h / 1000f);
196         rect.setAttributeNS(null, "style", "stroke:rgb(" + ((int)(255 * r)) + "," + ((int)(255 * g)) + "," + ((int)(255 * b)) + ");fill:rgb(" + ((int)(255 * fr)) + "," + ((int)(255 * fg)) + "," + ((int)(255 * fb)) + ")");
197         currentPageG.appendChild(rect);
198     }
199
200     /**
201      * draw a filled rectangle in the current color
202      *
203      * @param x the x position of left edge in millipoints
204      * @param y the y position of top edge in millipoints
205      * @param w the width in millipoints
206      * @param h the height in millipoints
207      * @param drawAsOutline true for draw, false for fill
208      */

209     protected void addRect(int x, int y, int w, int h,
210                            boolean drawAsOutline) {
211         int startx = (x + 500) / 1000;
212         int starty = pageHeight - ((y + 500) / 1000);
213         int endx = (x + w + 500) / 1000;
214         int endy = pageHeight - ((y + h + 500) / 1000);
215         if (drawAsOutline) {
216             Element rect = SVGUtilities.createRect(svgDocument, startx, starty, endx - startx, endy - starty);
217             rect.setAttributeNS(null, "style", "fill:none");
218             currentPageG.appendChild(rect);
219         } else {
220             Element rect = SVGUtilities.createRect(svgDocument, startx, starty, endx - startx, starty - endy);
221             rect.setAttributeNS(null, "style", "stroke:none");
222             currentPageG.appendChild(rect);
223         }
224     }
225
226     protected void addFilledRect(int x, int y, int w, int h,
227                                  ColorType col) {
228         float r = col.red();
229         float g = col.green();
230         float b = col.blue();
231         addRect(x, y, w, h, r, g, b, r, g, b);
232     }
233
234     protected void drawFrame() {
235         int width = pageWidth;
236         int height = pageHeight;
237         Element rect = SVGUtilities.createRect(svgDocument, 0, 0, width, height);
238         rect.setAttributeNS(null, "style", "fill:none;stroke:black");
239         currentPageG.appendChild(rect);
240     }
241
242     public void render(Page page, OutputStream JavaDoc stream)
243     throws IOException JavaDoc {
244         pageNumber++;
245         this.render(page);
246     }
247
248     public void render(Page page)
249     throws IOException JavaDoc {
250         idReferences = page.getIDReferences();
251
252         int lastWidth = pageWidth;
253         int lastHeight = pageHeight;
254
255         pageWidth = (int)((float)page.getWidth() / 1000f + .5);
256         pageHeight = (int)((float)page.getHeight() / 1000f + .5);
257
258         if(lastLink != null && currentPageG != null) {
259             lastLink.setAttributeNS(null, "xlink:href", "#svgView(viewBox(0, "+ (totalHeight) + ", " + pageWidth + ", " + pageHeight + "))");
260             currentPageG.appendChild(lastLink);
261         }
262
263         totalHeight += pageHeight;
264         if(totalWidth < pageWidth) {
265             totalWidth = pageWidth;
266         }
267
268         currentPageG = SVGUtilities.createG(svgDocument);
269         currentPageG.setAttributeNS(null, "id", /*title + */"Page-" + pageNumber);
270         currentPageG.setAttributeNS(null, "style", "font-family:sanserif;font-size:12");
271         svgRoot.appendChild(currentPageG);
272
273         drawFrame();
274
275         renderPage(page);
276
277         currentPageG.setAttributeNS(null, "transform", "translate(0," + (totalHeight - pageHeight) + ")");
278
279         Element lastPageLink = svgDocument.createElementNS(svgNS, "a");
280         if(lastLink != null) {
281             lastPageLink.setAttributeNS(null, "xlink:href", "#svgView(viewBox(0, " + (totalHeight - pageHeight - lastHeight) + ", " + lastWidth + ", " + lastHeight + "))");
282         } else {
283             lastPageLink.setAttributeNS(null, "xlink:href", "#svgView(viewBox(0, " + (totalHeight - pageHeight) + ", " + pageWidth + ", " + pageHeight + "))");
284         }
285         currentPageG.appendChild(lastPageLink);
286         Element rect = SVGUtilities.createRect(svgDocument, 0, 0, pageWidth / 2, pageHeight);
287         rect.setAttributeNS(null, "style", "fill:blue;visibility:hidden");
288         lastPageLink.appendChild(rect);
289
290         lastLink = svgDocument.createElementNS(svgNS, "a");
291         rect = SVGUtilities.createRect(svgDocument, pageWidth / 2, 0, pageWidth / 2, pageHeight);
292         rect.setAttributeNS(null, "style", "fill:blue;visibility:hidden");
293         lastLink.appendChild(rect);
294
295         /*
296          * if (page.hasLinks()) {
297          * ....
298          * }
299          */

300
301     }
302
303     public void renderPage(Page page) {
304
305         this.currentFontName = "";
306         this.currentFontSize = 0;
307
308         renderRegions(page);
309     }
310
311     protected void doFrame(org.apache.fop.layout.Area area) {
312         int w, h;
313         int rx = this.currentAreaContainerXPosition;
314         w = area.getContentWidth();
315
316         if (area instanceof BlockArea) {
317             rx += ((BlockArea)area).getStartIndent();
318         }
319
320         h = area.getContentHeight();
321         int ry = this.currentYPosition;
322
323         rx = rx - area.getPaddingLeft();
324         ry = ry + area.getPaddingTop();
325         w = w + area.getPaddingLeft() + area.getPaddingRight();
326         h = h + area.getPaddingTop() + area.getPaddingBottom();
327
328     doBackground(area, rx, ry, w, h);
329
330         rx = rx - area.getBorderLeftWidth();
331         ry = ry + area.getBorderTopWidth();
332         w = w + area.getBorderLeftWidth() + area.getBorderRightWidth();
333         h = h + area.getBorderTopWidth() + area.getBorderBottomWidth();
334
335         BorderAndPadding bp = area.getBorderAndPadding();
336         ColorType borderColor;
337
338         if (area.getBorderTopWidth() != 0) {
339             borderColor = bp.getBorderColor(BorderAndPadding.TOP);
340             addLine(rx, ry, rx + w, ry, area.getBorderTopWidth(),
341                     borderColor.red(), borderColor.green(),
342                     borderColor.blue());
343         }
344
345         if (area.getBorderLeftWidth() != 0) {
346             borderColor = bp.getBorderColor(BorderAndPadding.LEFT);
347             addLine(rx, ry, rx, ry - h, area.getBorderLeftWidth(),
348                     borderColor.red(), borderColor.green(),
349                     borderColor.blue());
350         }
351
352         if (area.getBorderRightWidth() != 0) {
353             borderColor = bp.getBorderColor(BorderAndPadding.RIGHT);
354             addLine(rx + w, ry, rx + w, ry - h,
355                     area.getBorderRightWidth(), borderColor.red(),
356                     borderColor.green(),
357                     borderColor.blue());
358         }
359
360         if (area.getBorderBottomWidth() != 0) {
361             borderColor = bp.getBorderColor(BorderAndPadding.BOTTOM);
362             addLine(rx, ry - h, rx + w, ry - h, area.getBorderBottomWidth(),
363                     borderColor.red(), borderColor.green(),
364                     borderColor.blue());
365         }
366     }
367
368     protected Rectangle2D JavaDoc getBounds(org.apache.fop.layout.Area a) {
369         return new Rectangle2D.Double JavaDoc(currentAreaContainerXPosition,
370                                       currentYPosition,
371                                       a.getAllocationWidth(), a.getHeight());
372     }
373
374     public void setupFontInfo(FontInfo fontInfo) throws FOPException {
375         // create a temp Image to test font metrics on
376
BufferedImage JavaDoc fontImage =
377             new BufferedImage JavaDoc(100, 100, BufferedImage.TYPE_INT_RGB);
378         org.apache.fop.render.awt.FontSetup.setup(fontInfo, fontImage.createGraphics());
379     }
380
381     public void renderDisplaySpace(DisplaySpace space) {
382         int d = space.getSize();
383         this.currentYPosition -= d;
384     }
385
386     /**
387      * Renders an image, scaling it to the given width and height.
388      * If the scaled width and height is the same intrinsic size
389      * of the image, the image is not scaled.
390      *
391      * @param x the x position of left edge in millipoints
392      * @param y the y position of top edge in millipoints
393      * @param w the width in millipoints
394      * @param h the height in millipoints
395      * @param image the image to be rendered
396      * @param fs the font state to use when rendering text
397      * in non-bitmapped images.
398      */

399     protected void drawImageScaled(int x, int y, int w, int h,
400                    FopImage image,
401                    FontState fs) {
402     // XXX: implement this
403
}
404
405     /**
406      * Renders an image, clipping it as specified.
407      *
408      * @param x the x position of left edge in millipoints.
409      * @param y the y position of top edge in millipoints.
410      * @param clipX the left edge of the clip in millipoints
411      * @param clipY the top edge of the clip in millipoints
412      * @param clipW the clip width in millipoints
413      * @param clipH the clip height in millipoints
414      * @param fill the image to be rendered
415      * @param fs the font state to use when rendering text
416      * in non-bitmapped images.
417      */

418     protected void drawImageClipped(int x, int y,
419                     int clipX, int clipY,
420                     int clipW, int clipH,
421                     FopImage image,
422                     FontState fs) {
423     // XXX: implement this
424
}
425
426     public void renderImageArea(ImageArea area) {
427
428         int x = currentXPosition + area.getXOffset();
429         int y = currentYPosition;
430         int w = area.getContentWidth();
431         int h = area.getHeight();
432         this.currentYPosition -= h;
433
434         FopImage img = area.getImage();
435
436         if (img == null) {
437             log.error("Error while loading image : area.getImage() is null");
438
439             addRect(x, y, w, h, true); // use helper function
440

441         } else {
442             if (img instanceof SVGImage) {
443                 try {
444                     SVGDocument svg = ((SVGImage)img).getSVGDocument();
445                     renderSVGDocument(svg, x / 1000f, pageHeight - y / 1000f);
446                 } catch (FopImageException e) {}
447
448             } else {
449
450                 String JavaDoc urlString = img.getURL();
451                 try {
452                     URL JavaDoc url = new URL JavaDoc(urlString);
453
454                     ImageIcon JavaDoc icon = new ImageIcon JavaDoc(url);
455                     Image JavaDoc image = icon.getImage();
456
457                     int startx = (x + 500) / 1000;
458                     int starty = pageHeight - ((y + 500) / 1000);
459                     int endx = (x + w + 500) / 1000;
460                     int endy = pageHeight - ((y + h + 500) / 1000);
461
462                     // reverse start and end y because h is positive
463
//graphics.drawImage(image, startx, starty, endx - startx,
464
// starty - endy, null);
465

466                 } catch (MalformedURLException JavaDoc mue) {
467                     // cannot normally occur because, if URL is wrong, constructing FopImage
468
// will already have failed earlier on
469
}
470             }
471         }
472
473         this.currentXPosition += area.getContentWidth();
474     }
475
476     public void renderWordArea(WordArea area) {
477         char ch;
478         StringBuffer JavaDoc pdf = new StringBuffer JavaDoc();
479
480         String JavaDoc name = area.getFontState().getFontFamily();
481         int size = area.getFontState().getFontSize();
482         boolean underlined = area.getUnderlined();
483
484         float red = area.getRed();
485         float green = area.getGreen();
486         float blue = area.getBlue();
487
488         if ((!name.equals(this.currentFontName))
489                 || (size != this.currentFontSize)) {
490             this.currentFontName = name;
491             this.currentFontSize = size;
492         }
493
494         if ((red != this.currentRed) || (green != this.currentGreen)
495                 || (blue != this.currentBlue)) {
496             this.currentRed = red;
497             this.currentGreen = green;
498             this.currentBlue = blue;
499         }
500
501         int rx = this.currentXPosition;
502         int bl = this.currentYPosition;
503
504         String JavaDoc s = area.getText();
505
506         if (saveColor != null) {
507             if (saveColor.getRed() != red || saveColor.getGreen() != green
508                     || saveColor.getBlue() != blue) {
509                 saveColor = new Color JavaDoc(red, green, blue);
510             }
511         } else {
512             saveColor = new Color JavaDoc(red, green, blue);
513         }
514
515         Element text = SVGUtilities.createText(svgDocument, rx / 1000f, pageHeight - bl / 1000f, s);
516         String JavaDoc st = null;
517         if(!"sans-serif".equals(this.currentFontName)) {
518             st = "font-family:" + this.currentFontName;
519         }
520         if(this.currentFontSize != 12000) {
521             if(st == null) {
522                 st = "";
523             } else {
524                 st += ";";
525             }
526             st += "font-size:" + (this.currentFontSize / 1000f);
527         }
528         if(red != 0 || green != 0 || blue != 0) {
529             if(st == null) {
530                 st = "";
531             } else {
532                 st += ";";
533             }
534             st += "fill:rgb(" + ((int)(255 * red)) + "," + ((int)(255 * green)) + "," + ((int)(255 * blue)) + ")";
535         }
536         String JavaDoc fweight = area.getFontState().getFontWeight();
537         if(!"normal".equals(fweight)) {
538             if(st == null) {
539                 st = "";
540             } else {
541                 st += ";";
542             }
543             st += "font-weight:" + fweight;
544         }
545         String JavaDoc fstyle = area.getFontState().getFontStyle();
546         if(!"normal".equals(fstyle)) {
547             if(st == null) {
548                 st = "";
549             } else {
550                 st += ";";
551             }
552             st += "font-style:" + fstyle;
553         }
554
555         if(st != null) {
556             text.setAttributeNS(null, "style", st);
557         }
558         currentPageG.appendChild(text);
559
560         this.currentXPosition += area.getContentWidth();
561     }
562
563     public void renderInlineSpace(InlineSpace space) {
564         this.currentXPosition += space.getSize();
565     }
566
567     /**
568      *
569      * @param area area to render
570      */

571     public void renderLeaderArea(LeaderArea area) {
572
573         int rx = this.currentXPosition;
574         int ry = this.currentYPosition;
575         int w = area.getLeaderLength();
576         int h = area.getHeight();
577         int th = area.getRuleThickness();
578         int st = area.getRuleStyle(); // not used at the moment
579
float r = area.getRed();
580         float g = area.getGreen();
581         float b = area.getBlue();
582
583         //graphics.setColor(new Color(r, g, b));
584

585         addRect(rx, ry, w, th, false);
586
587         this.currentXPosition += area.getContentWidth();
588     }
589
590     public void renderSVGArea(SVGArea area) {
591
592         float x = this.currentXPosition / 1000f;
593         float y = pageHeight - this.currentYPosition / 1000f;
594         int w = area.getContentWidth();
595         int h = area.getHeight();
596
597         Document doc = area.getSVGDocument();
598         renderSVGDocument(doc, x, y);
599         this.currentXPosition += area.getContentWidth();
600     }
601
602     protected void renderSVGDocument(Document doc, float x, float y) {
603         SVGSVGElement svg = ((SVGDocument)doc).getRootElement();
604         Element view = svgDocument.createElementNS(svgNS, "svg");
605         Node JavaDoc newsvg = svgDocument.importNode(svg, true);
606         //view.setAttributeNS(null, "viewBox", "0 0 ");
607
view.setAttributeNS(null, "x", "" + x);
608         view.setAttributeNS(null, "y", "" + y);
609
610         // this fixes a problem where the xmlns is repeated sometimes
611
Element ele = (Element)newsvg;
612         ele.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", svgNS);
613         if(ele.hasAttributeNS(null, "xmlns")) {
614            ele.removeAttributeNS(null, "xmlns");
615         }
616
617         view.appendChild(newsvg);
618         currentPageG.appendChild(view);
619     }
620
621     public void setProducer(String JavaDoc producer) {
622         // defined in Renderer Interface
623
}
624
625     public static Color JavaDoc colorType2Color(ColorType ct) {
626         if (ct == null) {
627             return null;
628         }
629         return new Color JavaDoc(ct.red(), ct.green(), ct.blue());
630     }
631
632     public void renderForeignObjectArea(ForeignObjectArea area) {
633         area.getObject().render(this);
634     }
635
636     public void startRenderer(OutputStream JavaDoc outputStream)
637               throws IOException JavaDoc {
638         DOMImplementation JavaDoc impl = SVGDOMImplementation.getDOMImplementation();
639         svgDocument = impl.createDocument(svgNS, "svg", null);
640         ProcessingInstruction JavaDoc pi =
641                  svgDocument.createProcessingInstruction(
642                          "xml",
643                          " version=\"1.0\" encoding=\"ISO-8859-1\"");
644                 svgRoot = svgDocument.getDocumentElement();
645         svgDocument.insertBefore(pi, svgRoot);
646         }
647
648     public void stopRenderer(OutputStream JavaDoc outputStream)
649     throws IOException JavaDoc {
650         svgRoot.setAttributeNS(null, "width", "" + totalWidth);
651         svgRoot.setAttributeNS(null, "height", "" + totalHeight);
652         //svgRoot.setAttributeNS(null, "viewBox", "0 0 " + pageWidth + " " + pageHeight);
653
SVGTranscoder svgT = new SVGTranscoder();
654         TranscoderInput input = new TranscoderInput(svgDocument);
655         TranscoderOutput output = new TranscoderOutput(new OutputStreamWriter JavaDoc(outputStream));
656         try {
657             svgT.transcode(input, output);
658         } catch(TranscoderException e) {
659             log.error("could not write svg file :" + e.getMessage(), e);
660         }
661         outputStream.flush();
662
663         svgDocument = null;
664         svgRoot = null;
665         currentPageG = null;
666         lastLink = null;
667
668         totalWidth = 0;
669         totalHeight = 0;
670
671         pageNumber = 0;
672     }
673
674 }
675
Popular Tags