KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > PrintRenderer


1 /*
2  * $Id: PrintRenderer.java,v 1.14.2.7 2003/03/02 16:55:17 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;
52
53 // FOP
54
import org.apache.fop.pdf.PDFPathPaint;
55 import org.apache.fop.pdf.PDFColor;
56 import org.apache.fop.apps.FOPException;
57 import org.apache.fop.layout.*;
58 import org.apache.fop.layout.inline.*;
59 import org.apache.fop.datatypes.*;
60 import org.apache.fop.render.pdf.FontSetup;
61 import org.apache.fop.apps.FOPException;
62
63 import org.apache.fop.svg.SVGArea;
64
65 // Java
66
import java.io.IOException JavaDoc;
67 import java.io.OutputStream JavaDoc;
68
69 // PrintRenderer is an abstract base class for renderers that produce printed type output.
70
// Subclasses would be PDFRenderer, PCLRenderer and similar renderers.
71

72 /**
73  * Abstract base class of "Print" type renderers.
74  *
75  * Modified by Mark Lillywhite mark-fop@inomial.com. Removed
76  * the render(AreaTree, OutputStream) method, and added
77  * no-op concrete implementation of startRenderer() and
78  * stopRenderer(). The method render(Page, OutputStream)
79  * is not mentioned in this class but it is inherited from
80  * the Renderer interface.
81  */

82 public abstract class PrintRenderer extends AbstractRenderer {
83     // vvv These are not currently referenced by the PrintRenderer, but are common to PCL and PDF renderers - so declare here.
84

85     /**
86      * the current (internal) font name
87      */

88     protected String JavaDoc currentFontName;
89
90     /**
91      * the current font size in millipoints
92      */

93     protected int currentFontSize;
94
95     /**
96      * the current color/gradient for borders, letters, etc.
97      */

98     protected PDFPathPaint currentStroke = null;
99
100     /**
101      * the current color/gradient to fill shapes with
102      */

103     protected PDFPathPaint currentFill = null;
104
105     /**
106      * the current colour's red component
107      */

108     // protected float currentRed = 0;
109

110     /**
111      * the current colour's green component
112      */

113     // protected float currentGreen = 0;
114

115     /**
116      * the current colour's blue component
117      */

118     // protected float currentBlue = 0;
119
// ^^^
120

121     // previous values used for text-decoration drawing
122
protected int prevUnderlineXEndPos;
123     protected int prevUnderlineYEndPos;
124     protected int prevUnderlineSize;
125     protected PDFColor prevUnderlineColor;
126     protected int prevOverlineXEndPos;
127     protected int prevOverlineYEndPos;
128     protected int prevOverlineSize;
129     protected PDFColor prevOverlineColor;
130     protected int prevLineThroughXEndPos;
131     protected int prevLineThroughYEndPos;
132     protected int prevLineThroughSize;
133     protected PDFColor prevLineThroughColor;
134
135     protected FontInfo fontInfo;
136
137     /**
138      * set the document's producer
139      *
140      * @param producer string indicating application producing PDF
141      */

142     public abstract void setProducer(String JavaDoc producer);
143
144     /**
145      * add a line to the current stream
146      *
147      * @param x1 the start x location in millipoints
148      * @param y1 the start y location in millipoints
149      * @param x2 the end x location in millipoints
150      * @param y2 the end y location in millipoints
151      * @param th the thickness in millipoints
152      * @param r the red component
153      * @param g the green component
154      * @param b the blue component
155      */

156     protected abstract void addLine(int x1, int y1, int x2, int y2, int th,
157                                     PDFPathPaint stroke);
158
159     /**
160      * add a line to the current stream
161      *
162      * @param x1 the start x location in millipoints
163      * @param y1 the start y location in millipoints
164      * @param x2 the end x location in millipoints
165      * @param y2 the end y location in millipoints
166      * @param th the thickness in millipoints
167      * @param rs the rule style
168      * @param r the red component
169      * @param g the green component
170      * @param b the blue component
171      */

172     protected abstract void addLine(int x1, int y1, int x2, int y2, int th,
173                                     int rs, PDFPathPaint stroke);
174
175     /**
176      * add a rectangle to the current stream
177      *
178      * @param x the x position of left edge in millipoints
179      * @param y the y position of top edge in millipoints
180      * @param w the width in millipoints
181      * @param h the height in millipoints
182      * @param stroke the stroke color/gradient
183      */

184     protected abstract void addRect(int x, int y, int w, int h,
185                                     PDFPathPaint stroke);
186
187     /**
188      * add a filled and stroked rectangle to the current stream
189      *
190      * @param x the x position of left edge in millipoints
191      * @param y the y position of top edge in millipoints
192      * @param w the width in millipoints
193      * @param h the height in millipoints
194      * @param fill the fill color/gradient
195      * @param stroke the stroke color/gradient
196      */

197     protected abstract void addRect(int x, int y, int w, int h,
198                                     PDFPathPaint stroke, PDFPathPaint fill);
199
200     /**
201      * Add a filled rectangle to the current stream
202      * This default implementation calls addRect
203      * using the same color for fill and border.
204      *
205      * @param x the x position of left edge in millipoints
206      * @param y the y position of top edge in millipoints
207      * @param w the width in millipoints
208      * @param h the height in millipoints
209      * @param fill the fill color/gradient
210      */

211     protected void addFilledRect(int x, int y, int w, int h,
212                                  PDFPathPaint fill) {
213         addRect(x, y, w, h, fill, fill);
214     }
215
216     protected void addFilledRect(int x, int y, int w, int h,
217                                  ColorType col) {
218         PDFColor pdfcol = new PDFColor(col);
219         addRect(x, y, w, h, pdfcol, pdfcol);
220     }
221
222     protected void doFrame(Area area) {
223         int w, h;
224         int rx = this.currentAreaContainerXPosition;
225         w = area.getContentWidth();
226         if (area instanceof BlockArea)
227             rx += ((BlockArea)area).getStartIndent();
228         h = area.getContentHeight();
229         int ry = this.currentYPosition;
230
231         rx = rx - area.getPaddingLeft();
232         ry = ry + area.getPaddingTop();
233         w = w + area.getPaddingLeft() + area.getPaddingRight();
234         h = h + area.getPaddingTop() + area.getPaddingBottom();
235
236         doBackground(area, rx, ry, w, h);
237
238         // rx = rx - area.getBorderLeftWidth();
239
// ry = ry + area.getBorderTopWidth();
240
// w = w + area.getBorderLeftWidth() + area.getBorderRightWidth();
241
// h = h + area.getBorderTopWidth() + area.getBorderBottomWidth();
242

243         // Handle line style
244
// Offset for haft the line width!
245
BorderAndPadding bp = area.getBorderAndPadding();
246         // int left = rx - area.getBorderLeftWidth() / 2;
247
// int right = rx + w + area.getBorderRightWidth() / 2;
248
// int top = ry + area.getBorderTopWidth() / 2;
249
// int bottom = ry - h - area.getBorderBottomWidth() / 2;
250
// if (area.getBorderTopWidth() != 0)
251
// addLine(left, top, right, top, area.getBorderTopWidth(),
252
// new PDFColor(bp.getBorderColor(BorderAndPadding.TOP)));
253
// if (area.getBorderLeftWidth() != 0)
254
// addLine(left, ry + area.getBorderTopWidth(), left, bottom, area.getBorderLeftWidth(),
255
// new PDFColor(bp.getBorderColor(BorderAndPadding.LEFT)));
256
// if (area.getBorderRightWidth() != 0)
257
// addLine(right, ry + area.getBorderTopWidth(), right, bottom, area.getBorderRightWidth(),
258
// new PDFColor(bp.getBorderColor(BorderAndPadding.RIGHT)));
259
// if (area.getBorderBottomWidth() != 0)
260
// addLine(rx - area.getBorderLeftWidth(), bottom, rx + w + area.getBorderRightWidth(), bottom, area.getBorderBottomWidth(),
261
// new PDFColor(bp.getBorderColor(BorderAndPadding.BOTTOM)));
262
// Try using rectangles instead of lines. Line style will be a
263
// problem though?
264
int left = area.getBorderLeftWidth();
265         int right = area.getBorderRightWidth();
266         int top = area.getBorderTopWidth();
267         int bottom = area.getBorderBottomWidth();
268         // If style is solid, use filled rectangles
269
if (top != 0)
270             addFilledRect(rx, ry, w, top,
271                           new PDFColor(bp.getBorderColor(BorderAndPadding.TOP)));
272         if (left != 0)
273             addFilledRect(rx - left, ry - h - bottom, left, h + top + bottom,
274                           new PDFColor(bp.getBorderColor(BorderAndPadding.LEFT)));
275         if (right != 0)
276             addFilledRect(rx + w, ry - h - bottom, right, h + top + bottom,
277                           new PDFColor(bp.getBorderColor(BorderAndPadding.RIGHT)));
278         if (bottom != 0)
279             addFilledRect(rx, ry - h - bottom, w, bottom,
280                           new PDFColor(bp.getBorderColor(BorderAndPadding.BOTTOM)));
281     }
282
283     /**
284      * render display space
285      *
286      * @param space the display space to render
287      */

288     public void renderDisplaySpace(DisplaySpace space) {
289         int d = space.getSize();
290         this.currentYPosition -= d;
291     }
292
293     /**
294      * render a foreign object area
295      */

296     public abstract void renderForeignObjectArea(ForeignObjectArea area);
297
298     /**
299      * render SVG area
300      *
301      * @param area the SVG area to render
302      */

303     public abstract void renderSVGArea(SVGArea area);
304
305     /**
306      * render inline area
307      *
308      * @param area inline area to render
309      */

310     public abstract void renderWordArea(WordArea area);
311
312     protected void addWordLines(WordArea area, int rx, int bl, int size,
313                                 PDFColor theAreaColor) {
314         if (area.getUnderlined()) {
315             int yPos = bl - size / 10;
316             addLine(rx, yPos, rx + area.getContentWidth(), yPos, size / 14,
317                     theAreaColor);
318             // save position for underlining a following InlineSpace
319
prevUnderlineXEndPos = rx + area.getContentWidth();
320             prevUnderlineYEndPos = yPos;
321             prevUnderlineSize = size / 14;
322             prevUnderlineColor = theAreaColor;
323         }
324
325         if (area.getOverlined()) {
326             int yPos = bl + area.getFontState().getAscender() + size / 10;
327             addLine(rx, yPos, rx + area.getContentWidth(), yPos, size / 14,
328                     theAreaColor);
329             prevOverlineXEndPos = rx + area.getContentWidth();
330             prevOverlineYEndPos = yPos;
331             prevOverlineSize = size / 14;
332             prevOverlineColor = theAreaColor;
333         }
334
335         if (area.getLineThrough()) {
336             int yPos = bl + area.getFontState().getAscender() * 3 / 8;
337             addLine(rx, yPos, rx + area.getContentWidth(), yPos, size / 14,
338                     theAreaColor);
339             prevLineThroughXEndPos = rx + area.getContentWidth();
340             prevLineThroughYEndPos = yPos;
341             prevLineThroughSize = size / 14;
342             prevLineThroughColor = theAreaColor;
343         }
344     }
345
346     /**
347      * render inline space
348      *
349      * @param space space to render
350      */

351     public void renderInlineSpace(InlineSpace space) {
352         this.currentXPosition += space.getSize();
353         if (space.getUnderlined()) {
354             if (prevUnderlineColor != null) {
355                 addLine(prevUnderlineXEndPos, prevUnderlineYEndPos,
356                         prevUnderlineXEndPos + space.getSize(),
357                         prevUnderlineYEndPos, prevUnderlineSize,
358                         prevUnderlineColor);
359                 // save position for a following InlineSpace
360
prevUnderlineXEndPos = prevUnderlineXEndPos + space.getSize();
361             }
362         }
363         if (space.getOverlined()) {
364             if (prevOverlineColor != null) {
365                 addLine(prevOverlineXEndPos, prevOverlineYEndPos,
366                         prevOverlineXEndPos + space.getSize(),
367                         prevOverlineYEndPos, prevOverlineSize,
368                         prevOverlineColor);
369                 prevOverlineXEndPos = prevOverlineXEndPos + space.getSize();
370             }
371         }
372         if (space.getLineThrough()) {
373             if (prevLineThroughColor != null) {
374                 addLine(prevLineThroughXEndPos, prevLineThroughYEndPos,
375                         prevLineThroughXEndPos + space.getSize(),
376                         prevLineThroughYEndPos, prevLineThroughSize,
377                         prevLineThroughColor);
378                 prevLineThroughXEndPos = prevLineThroughXEndPos + space.getSize();
379             }
380         }
381     }
382
383     /**
384      * render page
385      *
386      * @param page page to render
387      */

388     public abstract void renderPage(Page page);
389
390     /**
391      * render leader area
392      *
393      * @param area area to render
394      */

395     public void renderLeaderArea(LeaderArea area) {
396         int rx = this.currentXPosition;
397         int ry = this.currentYPosition;
398         int w = area.getContentWidth();
399         int h = area.getHeight();
400         int th = area.getRuleThickness();
401         int st = area.getRuleStyle();
402
403         // checks whether thickness is = 0, because of bug in pdf (or where?),
404
// a line with thickness 0 is still displayed
405
if (th != 0) {
406             switch (st) {
407             case org.apache.fop.fo.properties.RuleStyle.DOUBLE:
408                 addLine(rx, ry, rx + w, ry, th / 3, st,
409                         new PDFColor(area.getRed(), area.getGreen(),
410                                      area.getBlue()));
411                 addLine(rx, ry + (2 * th / 3), rx + w, ry + (2 * th / 3),
412                         th / 3, st,
413                         new PDFColor(area.getRed(), area.getGreen(),
414                                      area.getBlue()));
415                 break;
416             case org.apache.fop.fo.properties.RuleStyle.GROOVE:
417                 addLine(rx, ry, rx + w, ry, th / 2, st,
418                         new PDFColor(area.getRed(), area.getGreen(),
419                                      area.getBlue()));
420                 addLine(rx, ry + (th / 2), rx + w, ry + (th / 2), th / 2, st,
421                         new PDFColor(255, 255, 255));
422                 break;
423             case org.apache.fop.fo.properties.RuleStyle.RIDGE:
424                 addLine(rx, ry, rx + w, ry, th / 2, st,
425                         new PDFColor(255, 255, 255));
426                 addLine(rx, ry + (th / 2), rx + w, ry + (th / 2), th / 2, st,
427                         new PDFColor(area.getRed(), area.getGreen(),
428                                      area.getBlue()));
429                 break;
430             default:
431                 addLine(rx, ry, rx + w, ry, th, st,
432                         new PDFColor(area.getRed(), area.getGreen(),
433                                      area.getBlue()));
434             }
435             this.currentXPosition += area.getContentWidth();
436             this.currentYPosition += th;
437         }
438     }
439
440     /**
441      * set up the font info
442      *
443      * @param fontInfo font info to set up
444      */

445     public void setupFontInfo(FontInfo fontInfo) throws FOPException {
446         this.fontInfo = fontInfo;
447         FontSetup.setup(fontInfo);
448     }
449
450     /**
451       Default start renderer method. This would
452       normally be overridden. (mark-fop@inomial.com).
453     */

454     public void startRenderer(OutputStream JavaDoc outputStream)
455     throws IOException JavaDoc {}
456
457     /**
458      Default stop renderer method. This would
459      normally be overridden. (mark-fop@inomial.com).
460     */

461     public void stopRenderer(OutputStream JavaDoc outputStream)
462     throws IOException JavaDoc
463     {
464         this.idReferences = null;
465         currentFontName = "";
466         currentStroke = null;
467         currentFill = null;
468         prevUnderlineColor = null;
469         prevOverlineColor = null;
470         prevLineThroughColor = null;
471         fontInfo = null;
472     }
473
474 }
475
Popular Tags