KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > mif > MIFRenderer


1 /*
2  * $Id: MIFRenderer.java,v 1.11.2.8 2003/02/25 14:39:49 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.render.mif;
52
53 // FOP
54
import org.apache.fop.render.AbstractRenderer;
55 import org.apache.fop.layout.*;
56 import org.apache.fop.layout.inline.*;
57 import org.apache.fop.datatypes.*;
58 import org.apache.fop.svg.*;
59 import org.apache.fop.mif.*;
60 import org.apache.fop.image.*;
61
62 // Java
63
import java.io.IOException JavaDoc;
64 import java.io.OutputStream JavaDoc;
65
66 /**
67  * Renders areas to MIF. Collects all the Pages and print them out at the end.
68  * This means that the MIF renderer does not stream.
69  *
70  * @author Seshadri G
71  * @author <a HREF="mailto:mark-fop@inomial.com">Mark Lillywhite</a>
72  */

73 public class MIFRenderer extends AbstractRenderer {
74
75     private String JavaDoc currentFontName;
76     private String JavaDoc currentFontSize;
77     private int pageHeight;
78     private int pageWidth;
79
80     /**
81      * the current vertical position in millipoints from bottom
82      */

83     protected int currentYPosition = 0;
84
85     /**
86      * the current horizontal position in millipoints from left
87      */

88     protected int currentXPosition = 0;
89
90     /**
91      * the horizontal position of the current area container
92      */

93     private int currentAreaContainerXPosition = 0;
94
95
96     /**
97      * the MIF Document being created
98      */

99     protected MIFDocument mifDoc;
100
101
102     /* is a table currently open? */
103     private boolean inTable = false;
104
105     /**
106      * options
107      */

108     protected java.util.Map JavaDoc options;
109
110     /**
111      * create the MIF renderer
112      */

113     public MIFRenderer() {
114         this.mifDoc = new MIFDocument();
115     }
116
117     /**
118      * set up renderer options
119      */

120     public void setOptions(java.util.Map JavaDoc options) {
121         this.options = options;
122     }
123
124     /**
125      * set up the given FontInfo
126      */

127     public void setupFontInfo(FontInfo fontInfo) {
128
129         FontSetup.setup(fontInfo);
130         // FontSetup.addToFontFormat(this.mifDoc, fontInfo);
131

132     }
133
134     /**
135      * set the producer of the rendering
136      */

137     public void setProducer(String JavaDoc producer) {}
138
139
140     public void renderAreaContainer(AreaContainer area) {
141
142         if (area.foCreator != null
143                 && area.foCreator.getName() == "fo:table") {
144
145             this.mifDoc.createTable();
146             this.inTable = true;
147         } else if (area.foCreator != null
148                    && area.foCreator.getName() == "fo:table-body") {
149
150             this.mifDoc.setCurrent("fo:table-body");
151         } else if (area.foCreator != null
152                    && area.foCreator.getName() == "fo:table-column") {
153
154             int colWidth =
155                 ((org.apache.fop.fo.flow.TableColumn)area.foCreator).getColumnWidth();
156             this.mifDoc.setColumnProp(colWidth);
157         } else if (area.foCreator != null
158                    && area.foCreator.getName() == "fo:table-row") {
159
160             this.mifDoc.startRow();
161         } else if (area.foCreator != null
162                    && area.foCreator.getName() == "fo:table-cell") {
163
164             int rowSpan =
165                 ((org.apache.fop.fo.flow.TableCell)area.foCreator).getNumRowsSpanned();
166             int colSpan =
167                 ((org.apache.fop.fo.flow.TableCell)area.foCreator).getNumColumnsSpanned();
168             this.mifDoc.startCell(rowSpan, colSpan);
169         } else if (inTable) {
170
171             inTable = false;
172             this.mifDoc.endTable();
173
174         }
175         super.renderAreaContainer(area);
176     }
177
178     protected void addFilledRect(int x, int y, int w, int h,
179                                  ColorType col) {
180     }
181
182     protected void doFrame(Area area) {
183         int w, h;
184         int rx = this.currentAreaContainerXPosition;
185         w = area.getContentWidth();
186
187         if (area instanceof BlockArea)
188             rx += ((BlockArea)area).getStartIndent();
189
190         h = area.getContentHeight();
191         int ry = this.currentYPosition;
192
193         rx = rx - area.getPaddingLeft();
194         ry = ry + area.getPaddingTop();
195         w = w + area.getPaddingLeft() + area.getPaddingRight();
196         h = h + area.getPaddingTop() + area.getPaddingBottom();
197
198     doBackground(area, rx, ry, w, h);
199
200         rx = rx - area.getBorderLeftWidth();
201         ry = ry + area.getBorderTopWidth();
202         w = w + area.getBorderLeftWidth() + area.getBorderRightWidth();
203         h = h + area.getBorderTopWidth() + area.getBorderBottomWidth();
204
205         // Create a textrect with these dimensions.
206
// The y co-ordinate is measured +ve downwards so subtract page-height
207

208         this.mifDoc.setTextRectProp(rx, pageHeight - ry, w, h);
209
210         /*
211          * BorderAndPadding bp = area.getBorderAndPadding();
212          * if (area.getBorderTopWidth() != 0)
213          * addLine(rx, ry, rx + w, ry, area.getBorderTopWidth(),
214          * new PDFColor(bp.getBorderColor(BorderAndPadding.TOP)));
215          * if (area.getBorderLeftWidth() != 0)
216          * addLine(rx, ry, rx, ry - h, area.getBorderLeftWidth(),
217          * new PDFColor(bp.getBorderColor(BorderAndPadding.LEFT)));
218          * if (area.getBorderRightWidth() != 0)
219          * addLine(rx + w, ry, rx + w, ry - h, area.getBorderRightWidth(),
220          * new PDFColor(bp.getBorderColor(BorderAndPadding.RIGHT)));
221          * if (area.getBorderBottomWidth() != 0)
222          * addLine(rx, ry - h, rx + w, ry - h, area.getBorderBottomWidth(),
223          * new PDFColor(bp.getBorderColor(BorderAndPadding.BOTTOM)));
224          */

225     }
226
227     public void renderSpanArea(SpanArea area) {
228         // A span maps to a textframe
229
this.mifDoc.createTextRect(area.getColumnCount());
230         super.renderSpanArea(area);
231     }
232
233     /**
234      * render the given block area
235      */

236     public void renderBlockArea(BlockArea area) {
237         this.mifDoc.setBlockProp(area.getStartIndent(), area.getEndIndent());
238         super.renderBlockArea(area);
239     }
240
241     /**
242      * render the given display space
243      */

244     public void renderDisplaySpace(DisplaySpace space) {
245         int d = space.getSize();
246         this.currentYPosition -= d;
247     }
248
249     /**
250      * render the given SVG area
251      */

252     public void renderSVGArea(SVGArea area) {}
253
254     /**
255      * render a foreign object area
256      */

257     public void renderForeignObjectArea(ForeignObjectArea area) {}
258
259     public void renderWordArea(WordArea area) {
260         String JavaDoc s;
261         s = area.getText();
262         this.mifDoc.addToStream(s);
263
264         this.currentXPosition += area.getContentWidth();
265     }
266
267     /**
268      * Renders an image, scaling it to the given width and height.
269      * If the scaled width and height is the same intrinsic size
270      * of the image, the image is not scaled.
271      *
272      * @param x the x position of left edge in millipoints
273      * @param y the y position of top edge in millipoints
274      * @param w the width in millipoints
275      * @param h the height in millipoints
276      * @param image the image to be rendered
277      * @param fs the font state to use when rendering text
278      * in non-bitmapped images.
279      */

280     protected void drawImageScaled(int x, int y, int w, int h,
281                    FopImage image,
282                    FontState fs) {
283     // XXX: implement this
284
}
285
286     /**
287      * Renders an image, clipping it as specified.
288      *
289      * @param x the x position of left edge in millipoints.
290      * @param y the y position of top edge in millipoints.
291      * @param clipX the left edge of the clip in millipoints
292      * @param clipY the top edge of the clip in millipoints
293      * @param clipW the clip width in millipoints
294      * @param clipH the clip height in millipoints
295      * @param fill the image to be rendered
296      * @param fs the font state to use when rendering text
297      * in non-bitmapped images.
298      */

299     protected void drawImageClipped(int x, int y,
300                     int clipX, int clipY,
301                     int clipW, int clipH,
302                     FopImage image,
303                     FontState fs) {
304     // XXX: implement this
305
}
306
307     /**
308      * render the given image area
309      */

310     public void renderImageArea(ImageArea area) {
311
312         int x = this.currentAreaContainerXPosition + area.getXOffset();
313         int y = this.currentYPosition;
314         int w = area.getContentWidth();
315         int h = area.getHeight();
316
317         this.currentYPosition -= h;
318
319         FopImage img = area.getImage();
320         if (img instanceof SVGImage) {
321             /*
322              * try {
323              * SVGSVGElement svg =
324              * ((SVGImage) img).getSVGDocument().getRootElement();
325              * currentStream.add("ET\nq\n" + (((float) w) / 1000f) +
326              * " 0 0 " + (((float) h) / 1000f) + " " +
327              * (((float) x) / 1000f) + " " +
328              * (((float)(y - h)) / 1000f) + " cm\n");
329              * // renderSVG(svg, (int) x, (int) y);
330              * currentStream.add("Q\nBT\n");
331              * } catch (FopImageException e) {
332              * }
333              */

334
335             log.warn("SVG images not supported in this version");
336         } else {
337             String JavaDoc url = img.getURL();
338             this.mifDoc.addImage(url, x, pageHeight - y, w, h);
339
340         }
341     }
342
343     /**
344      * render the given inline area
345      */

346     public void renderInlineArea(InlineArea area) {}
347
348     /**
349      * render the given inline space
350      */

351     public void renderInlineSpace(InlineSpace space) {
352
353         // I dont need the size of space! I just need to
354
// leave a blank space each time
355
String JavaDoc s = " ";
356         this.mifDoc.addToStream(s); // cool!
357
this.currentXPosition += space.getSize();
358     }
359
360     /**
361      * render the given line area
362      */

363     public void renderLineArea(LineArea area) {
364         // The start of a new linearea corresponds to a new para in FM
365
this.mifDoc.startLine();
366         super.renderLineArea(area);
367     }
368
369     /**
370      * render the given page
371      */

372     public void renderPage(Page page) {
373
374         AreaContainer before, after;
375         BodyAreaContainer body;
376         body = page.getBody();
377         before = page.getBefore();
378         after = page.getAfter();
379
380         this.currentFontName = "";
381         this.currentFontSize = "0";
382
383         pageHeight = page.getHeight();
384         pageWidth = page.getWidth();
385         this.mifDoc.setDocumentHeightWidth(pageHeight, pageWidth);
386
387         this.mifDoc.createPage();
388
389         body.render(this);
390
391
392         // If the area is an instance of anything other than body, it goes into the
393
// corresponding master page.
394

395
396         if (before != null) {
397
398             this.mifDoc.createTextRect(1); // Create a rect with one col
399
before.render(this);
400         }
401
402         if (after != null) {
403
404             this.mifDoc.createTextRect(1); // Create a rect with one col
405
after.render(this);
406         }
407
408     }
409
410     /**
411      * render the given leader area
412      */

413     public void renderLeaderArea(LeaderArea area) {}
414
415     /**
416       Default start renderer method. This would
417       normally be overridden. (mark-fop@inomial.com).
418     */

419     public void startRenderer(OutputStream JavaDoc outputStream)
420     throws IOException JavaDoc {
421         log.info("rendering areas to MIF");
422     }
423
424     /**
425       Default stop renderer method. This would
426       normally be overridden. (mark-fop@inomial.com)
427     */

428     public void stopRenderer(OutputStream JavaDoc outputStream)
429     throws IOException JavaDoc {
430         log.info("writing out MIF");
431         this.mifDoc.output(outputStream);
432         outputStream.flush();
433     }
434
435     public void render(Page page, OutputStream JavaDoc outputStream) {
436         this.renderPage(page);
437     }
438 }
439
440
Popular Tags