KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > svg > PDFDocumentGraphics2D


1 /*
2  * $Id: PDFDocumentGraphics2D.java,v 1.11.2.2 2003/02/25 15:08:11 jeremias 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.svg;
52
53 import org.apache.fop.pdf.*;
54 import org.apache.fop.render.pdf.FontSetup;
55 import org.apache.fop.layout.*;
56 import org.apache.fop.apps.FOPException;
57
58 import java.awt.Graphics JavaDoc;
59 import java.awt.Font JavaDoc;
60 import java.awt.Color JavaDoc;
61 import java.awt.Shape JavaDoc;
62 import java.awt.font.FontRenderContext JavaDoc;
63 import java.awt.font.GlyphVector JavaDoc;
64 import java.io.OutputStream JavaDoc;
65 import java.io.IOException JavaDoc;
66
67 import org.apache.batik.ext.awt.g2d.GraphicContext;
68
69 /**
70  * This class is a wrapper for the <tt>PDFGraphics2D</tt> that
71  * is used to create a full document around the pdf rendering from
72  * <tt>PDFGraphics2D</tt>.
73  *
74  * @author <a HREF="mailto:keiron@aftexsw.com">Keiron Liddle</a>
75  * @version $Id: PDFDocumentGraphics2D.java,v 1.11.2.2 2003/02/25 15:08:11 jeremias Exp $
76  * @see org.apache.fop.svg.PDFGraphics2D
77  */

78 public class PDFDocumentGraphics2D extends PDFGraphics2D {
79     OutputStream JavaDoc stream;
80
81     PDFStream pdfStream;
82     int width;
83     int height;
84
85     FontInfo fontInfo = null;
86
87     /**
88      * Create a new PDFDocumentGraphics2D.
89      * This is used to create a new pdf document of the given height
90      * and width.
91      * The resulting document is written to the stream after rendering.
92      *
93      * @param textAsShapes set this to true so that text will be rendered
94      * using curves and not the font.
95      * @param stream the stream that the final document should be written to.
96      * @param width the width of the document
97      * @param height the height of the document
98      */

99     public PDFDocumentGraphics2D(boolean textAsShapes, OutputStream JavaDoc stream,
100                                  int width, int height) throws FOPException {
101         super(textAsShapes);
102
103         if (!textAsShapes) {
104             fontInfo = new FontInfo();
105             FontSetup.setup(fontInfo);
106             try {
107                 fontState = new FontState(fontInfo, "Helvetica", "normal",
108                                           "normal", 12, 0);
109             } catch (FOPException e) {}
110         }
111         standalone = true;
112         this.stream = stream;
113         this.pdfDoc = new PDFDocument();
114         this.pdfDoc.setProducer("FOP SVG Renderer");
115         pdfStream = this.pdfDoc.makeStream();
116         this.width = width;
117         this.height = height;
118
119         currentFontName = "";
120         currentFontSize = 0;
121         currentYPosition = 0;
122         currentXPosition = 0;
123
124         currentStream.write("1 0 0 -1 0 " + height + " cm\n");
125
126     }
127
128     public FontState getFontState() {
129         return fontState;
130     }
131
132     public PDFDocument getPDFDocument() {
133         return this.pdfDoc;
134     }
135
136     /**
137      * Set the dimensions of the svg document that will be drawn.
138      * This is useful if the dimensions of the svg document are different
139      * from the pdf document that is to be created.
140      * The result is scaled so that the svg fits correctly inside the pdf document.
141      */

142     public void setSVGDimension(float w, float h) {
143         currentStream.write("" + PDFNumber.doubleOut(width / w) + " 0 0 "
144                             + PDFNumber.doubleOut(height / h) + " 0 0 cm\n");
145     }
146
147     /**
148      * Set the background of the pdf document.
149      * This is used to set the background for the pdf document
150      * Rather than leaving it as the default white.
151      */

152     public void setBackgroundColor(Color JavaDoc col) {
153         Color JavaDoc c = col;
154         currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue());
155         currentStream.write("q\n");
156         currentStream.write(currentColour.getColorSpaceOut(true));
157
158         currentStream.write("0 0 " + width + " " + height + " re\n");
159
160         currentStream.write("f\n");
161         currentStream.write("Q\n");
162     }
163
164     /**
165      * The rendering process has finished.
166      * This should be called after the rendering has completed as there is
167      * no other indication it is complete.
168      * This will then write the results to the output stream.
169      */

170     public void finish() throws IOException JavaDoc {
171         pdfStream.add(getString());
172         PDFResources pdfResources = this.pdfDoc.getResources();
173         PDFPage currentPage = this.pdfDoc.makePage(pdfResources, pdfStream,
174                                                    width, height, null);
175         if(currentAnnotList != null) {
176             currentPage.setAnnotList(currentAnnotList);
177         }
178         if (fontInfo != null) {
179             FontSetup.addToResources(this.pdfDoc, fontInfo);
180         }
181         pdfDoc.outputHeader(stream);
182         this.pdfDoc.output(stream);
183         pdfDoc.outputTrailer(stream);
184     }
185
186     public void setGraphicContext(GraphicContext c) {
187         gc = c;
188     }
189
190     /**
191      * This constructor supports the create method
192      */

193     public PDFDocumentGraphics2D(PDFDocumentGraphics2D g) {
194         super(g);
195     }
196
197     /**
198      * Creates a new <code>Graphics</code> object that is
199      * a copy of this <code>Graphics</code> object.
200      * @return a new graphics context that is a copy of
201      * this graphics context.
202      */

203     public Graphics JavaDoc create() {
204         return new PDFDocumentGraphics2D(this);
205     }
206
207     public void drawString(String JavaDoc s, float x, float y) {
208         if (super.textAsShapes) {
209             Font JavaDoc font = super.getFont();
210             FontRenderContext JavaDoc frc = super.getFontRenderContext();
211             GlyphVector JavaDoc gv = font.createGlyphVector(frc, s);
212             Shape JavaDoc glyphOutline = gv.getOutline(x, y);
213             super.fill(glyphOutline);
214         } else {
215             super.drawString(s, x, y);
216         }
217     }
218
219 }
220
221
Popular Tags