KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > pcl > PCLRenderer


1 /*
2  * $Id: PCLRenderer.java,v 1.13.2.9 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.pcl;
52
53 // FOP
54
import org.apache.fop.render.PrintRenderer;
55 import org.apache.fop.fo.properties.*;
56 import org.apache.fop.datatypes.*;
57 import org.apache.fop.pdf.PDFPathPaint;
58 import org.apache.fop.pdf.PDFColor;
59 import org.apache.fop.layout.*;
60 import org.apache.fop.layout.inline.*;
61 import org.apache.fop.image.*;
62 import org.apache.fop.svg.SVGArea;
63
64 import org.w3c.dom.svg.SVGSVGElement;
65 import org.w3c.dom.svg.SVGDocument;
66
67 // Java
68
import java.io.IOException JavaDoc;
69 import java.io.OutputStream JavaDoc;
70
71 /**
72  * Renders areas to PCL.
73  *
74  * @author Arthur E Welch III (while at M&I EastPoint Technology --
75  * donated by EastPoint to the Apache FOP project March 2, 2001)
76  * @author <a HREF="mailto:mark-fop@inomial.com">Mark Lillywhite</a> (to use
77  * the new Renderer interface)
78  */

79 public class PCLRenderer extends PrintRenderer {
80
81     /**
82      * the current stream to add PCL commands to
83      */

84     public PCLStream currentStream;
85
86     private int pageHeight = 7920;
87
88     // These variables control the virtual paggination functionality.
89
public int curdiv = 0;
90     private int divisions = -1;
91     public int paperheight = -1; // Paper height in decipoints?
92
public int orientation = 0; // 0=default/portrait, 1=landscape.
93
public int topmargin = -1; // Top margin in decipoints?
94
public int leftmargin = -1; // Left margin in decipoints?
95
private int fullmargin = 0;
96     private final boolean debug = false;
97
98     private int xoffset =
99         -180; // X Offset to allow for PCL implicit 1/4" left margin.
100

101     private java.util.Map JavaDoc options;
102
103     /**
104      * Create the PCL renderer
105      */

106     public PCLRenderer() {}
107
108     /**
109      * set up renderer options
110      */

111     public void setOptions(java.util.Map JavaDoc options) {
112         this.options = options;
113     }
114
115     /**
116      * set the PCL document's producer
117      *
118      * @param producer string indicating application producing PCL
119      */

120     public void setProducer(String JavaDoc producer) {}
121
122     /**
123      * add a line to the current stream
124      *
125      * @param x1 the start x location in millipoints
126      * @param y1 the start y location in millipoints
127      * @param x2 the end x location in millipoints
128      * @param y2 the end y location in millipoints
129      * @param th the thickness in millipoints
130      * @param stroke the line color
131      */

132     protected void addLine(int x1, int y1, int x2, int y2, int th,
133                            PDFPathPaint stroke) {
134         if (x1 == x2)
135             addRect(x1 - (th / 2), y1, th, y2 - y1 + 1, stroke, stroke);
136         else if (y1 == y2)
137             addRect(x1, y1 + (th / 2), x2 - x1 + 1, th, stroke, stroke);
138     }
139
140     /**
141      * add a line to the current stream
142      *
143      * @param x1 the start x location in millipoints
144      * @param y1 the start y location in millipoints
145      * @param x2 the end x location in millipoints
146      * @param y2 the end y location in millipoints
147      * @param th the thickness in millipoints
148      * @param rs the rule style
149      * @param stroke the line color
150      */

151     protected void addLine(int x1, int y1, int x2, int y2, int th, int rs,
152                            PDFPathPaint stroke) {
153         int dashon = 0;
154         int dashoff = 0;
155         // if ( rs != null && rs.length() > 5 && rs.charAt(0) == '[' && rs.charAt(1) != ']' && rs.charAt(4) == ']' )
156
// {
157
// dashon = rs.charAt(1) - '0';
158
// dashoff = rs.charAt(3) - '0';
159
// }
160
switch (rs) {
161         case org.apache.fop.fo.properties.RuleStyle.DASHED:
162             dashon = 3;
163             dashoff = 3;
164             break;
165         case org.apache.fop.fo.properties.RuleStyle.DOTTED:
166             dashon = 1;
167             dashoff = 3;
168             break;
169         }
170         if (x1 == x2) {
171             if (dashon > 0 && dashoff > 0) {
172                 int start = y1;
173                 int len = th * dashon;
174                 while (start < y2) {
175                     if (start + len > y2)
176                         len = y2 - start;
177                     addRect(x1 - (th / 2), start, th, len, stroke, stroke);
178                     start += (len + dashoff * th);
179                 }
180             } else
181                 addRect(x1 - (th / 2), y1, th, y2 - y1 + 1, stroke, stroke);
182         } else if (y1 == y2) {
183             if (dashon > 0 && dashoff > 0) {
184                 int start = x1;
185                 int len = th * dashon;
186                 while (start < x2) {
187                     if (start + len > x2)
188                         len = x2 - start;
189                     addRect(start, y1 + (th / 2), len, th, stroke, stroke);
190                     start += (len + dashoff * th);
191                 }
192             } else
193                 addRect(x1, y1 + (th / 2), x2 - x1 + 1, th, stroke, stroke);
194         }
195     }
196
197     /**
198      * add a rectangle to the current stream
199      *
200      * @param x the x position of left edge in millipoints
201      * @param y the y position of top edge in millipoints
202      * @param w the width in millipoints
203      * @param h the height in millipoints
204      * @param stroke the stroke color/gradient
205      */

206     protected void addRect(int x, int y, int w, int h, PDFPathPaint stroke) {
207         //if (h < 0)
208
// h *= -1;
209

210         if ((h >= 0 && h < 720) || (h < 0 && h > -720) || w < 720) {
211             if (w < 720)
212                 w = 720;
213             if (h > 0 && h < 720)
214                 h = 720;
215             else if (h < 0 && h > -720)
216                 h = -720;
217             addRect(x, y, w, h, stroke, stroke);
218         } else {
219             addRect(x, y, w, 720, stroke, stroke);
220             addRect(x, y, 720, h, stroke, stroke);
221             addRect(x + w - 720, y, 720, h, stroke, stroke);
222             addRect(x, y - h + 720, w, 720, stroke, stroke);
223         }
224     }
225
226     /**
227      * add a filled rectangle to the current stream
228      *
229      * @param x the x position of left edge in millipoints
230      * @param y the y position of top edge in millipoints
231      * @param w the width in millipoints
232      * @param h the height in millipoints
233      * @param fill the fill color/gradient
234      * @param stroke the stroke color/gradient
235      */

236     protected void addRect(int x, int y, int w, int h, PDFPathPaint stroke,
237                            PDFPathPaint fill) {
238         if ((w == 0) || (h == 0))
239             return;
240         if (h < 0)
241             h *= -1;
242         else
243             y += h;
244
245
246         PDFColor sc = (PDFColor)stroke;
247         PDFColor fc = (PDFColor)fill;
248
249         sc.setColorSpace(ColorSpace.DEVICE_RGB);
250         fc.setColorSpace(ColorSpace.DEVICE_RGB);
251
252         int lineshade =
253             (int)(100
254                   - ((0.3f * sc.red() + 0.59f * sc.green() + 0.11f * sc.blue())
255                      * 100f));
256         int fillshade =
257             (int)(100
258                   - ((0.3f * fc.red() + 0.59f * fc.green() + 0.11f * fc.blue())
259                      * 100f));
260
261         int xpos = xoffset + (x / 100);
262         if (xpos < 0) {
263             xpos = 0;
264             log.warn("Horizontal position out of bounds.");
265         }
266
267         currentStream.add("\033*v1O\033&a" + xpos + "h"
268                           + (pageHeight - (y / 100)) + "V" + "\033*c"
269                           + (w / 100) + "h" + (h / 100) + "V" + "\033*c"
270                           + lineshade + "G" + "\033*c2P");
271         if (fillshade != lineshade && (w >= 720 || h >= 720)) {
272             xpos = xoffset + ((x + 240) / 100);
273             if (xpos < 0) {
274                 xpos = 0;
275                 log.warn("Horizontal position out of bounds.");
276             }
277             currentStream.add("\033&a" + xpos + "h"
278                               + (pageHeight - ((y + 240)) / 100) + "V"
279                               + "\033*c" + ((w - 480) / 100) + "h"
280                               + ((h - 480) / 100) + "V" + "\033*c"
281                               + fillshade + "G" + "\033*c2P");
282         }
283         // Reset pattern transparency mode.
284
currentStream.add("\033*v0O");
285     }
286
287     /**
288      * Renders an image, scaling it to the given width and height.
289      * If the scaled width and height is the same intrinsic size
290      * of the image, the image is not scaled.
291      *
292      * @param x the x position of left edge in millipoints
293      * @param y the y position of top edge in millipoints
294      * @param w the width in millipoints
295      * @param h the height in millipoints
296      * @param image the image to be rendered
297      * @param fs the font state to use when rendering text
298      * in non-bitmapped images.
299      */

300     protected void drawImageScaled(int x, int y, int w, int h,
301                    FopImage image,
302                    FontState fs) {
303     // XXX: implement this
304
}
305
306     /**
307      * Renders an image, clipping it as specified.
308      *
309      * @param x the x position of left edge in millipoints.
310      * @param y the y position of top edge in millipoints.
311      * @param clipX the left edge of the clip in millipoints
312      * @param clipY the top edge of the clip in millipoints
313      * @param clipW the clip width in millipoints
314      * @param clipH the clip height in millipoints
315      * @param fill the image to be rendered
316      * @param fs the font state to use when rendering text
317      * in non-bitmapped images.
318      */

319     protected void drawImageClipped(int x, int y,
320                     int clipX, int clipY,
321                     int clipW, int clipH,
322                     FopImage image,
323                     FontState fs) {
324     // XXX: implement this
325
}
326
327     boolean printBMP(FopImage img, int x, int y, int w,
328                      int h) throws FopImageException {
329         // Print the passed image file in PCL.
330
byte imgmap[] = img.getBitmaps();
331
332         int ix = 0;
333         int iy = 0;
334         int indx = 0;
335         int iw = img.getWidth();
336         int ih = img.getHeight();
337         int bytewidth = (iw / 8);
338         if ((iw % 8) != 0)
339             bytewidth++;
340         byte ib;
341         char ic[] = new char[bytewidth * 2];
342         char icu[] = new char[bytewidth];
343         int lastcount = -1;
344         byte lastbyte = 0;
345         int icwidth = 0;
346         int cr = 0;
347         int cg = 0;
348         int cb = 0;
349         int grey = 0;
350         boolean iscolor = img.getColorSpace().getColorSpace()
351                           != ColorSpace.DEVICE_GRAY;
352         int dcount = 0;
353         int xres = (iw * 72000) / w;
354         int yres = (ih * 72000) / h;
355         int resolution = xres;
356         if (yres > xres)
357             resolution = yres;
358
359         if (resolution > 300)
360             resolution = 600;
361         else if (resolution > 150)
362             resolution = 300;
363         else if (resolution > 100)
364             resolution = 150;
365         else if (resolution > 75)
366             resolution = 100;
367         else
368             resolution = 75;
369         if (debug)
370             System.out.println("PCLRenderer.printBMP() iscolor = " + iscolor);
371         // Setup for graphics
372
currentStream.add("\033*t" + resolution + "R\033*r0F\033*r1A");
373
374         // Transfer graphics data
375
for (iy = 0; iy < ih; iy++) {
376             ib = 0;
377             // int padding = iw % 8;
378
// if ( padding != 0 )
379
// padding = 8 - padding;
380
// padding = 0;
381
// indx = (ih - iy - 1 + padding) * iw;
382
indx = iy * iw;
383             if (iscolor)
384                 indx *= 3;
385             // currentStream.add("\033*b" + bytewidth + "W");
386
for (ix = 0; ix < iw; ix++) {
387                 if (iscolor) {
388                     cr = imgmap[indx++] & 0xFF;
389                     cg = imgmap[indx++] & 0xFF;
390                     cb = imgmap[indx++] & 0xFF;
391                     grey = (cr * 30 + cg * 59 + cb * 11) / 100;
392                 } else
393                     grey = imgmap[indx++] & 0xFF;
394                 if (grey < 128)
395                     ib |= (1 << (7 - (ix % 8)));
396                 if ((ix % 8) == 7 || ((ix + 1) == iw)) {
397                     if (icwidth < bytewidth) {
398                         if (lastcount >= 0) {
399                             if (ib == lastbyte)
400                                 lastcount++;
401                             else {
402                                 ic[icwidth++] = (char)(lastcount & 0xFF);
403                                 ic[icwidth++] = (char)lastbyte;
404                                 lastbyte = ib;
405                                 lastcount = 0;
406                             }
407                         } else {
408                             lastbyte = ib;
409                             lastcount = 0;
410                         }
411                         if (lastcount == 255 || ((ix + 1) == iw)) {
412                             ic[icwidth++] = (char)(lastcount & 0xFF);
413                             ic[icwidth++] = (char)lastbyte;
414                             lastbyte = 0;
415                             lastcount = -1;
416                         }
417                     }
418                     icu[ix / 8] = (char)ib;
419                     ib = 0;
420                 }
421             }
422             if (icwidth < bytewidth) {
423                 currentStream.add("\033*b1m" + icwidth + "W");
424                 currentStream.add(new String JavaDoc(ic, 0, icwidth));
425             } else {
426                 currentStream.add("\033*b0m" + bytewidth + "W");
427                 currentStream.add(new String JavaDoc(icu));
428             }
429             lastcount = -1;
430             icwidth = 0;
431         }
432
433         // End graphics
434
currentStream.add("\033*rB");
435
436
437         return (true);
438     }
439
440     /**
441      * render image area to PCL
442      *
443      * @param area the image area to render
444      */

445     public void renderImageArea(ImageArea area) {
446         int x = this.currentAreaContainerXPosition + area.getXOffset();
447         int y = this.currentYPosition;
448         int w = area.getContentWidth();
449         int h = area.getHeight();
450
451         this.currentYPosition -= h;
452
453         FopImage img = area.getImage();
454
455         int xpos = xoffset + (x / 100);
456         if (xpos < 0) {
457             xpos = 0;
458             log.warn("Horizontal position out of bounds.");
459         }
460
461         currentStream.add("\033&a" + xpos + "h" + (pageHeight - (y / 100))
462                           + "V");
463
464         try {
465             printBMP(img, x, y, w, h);
466         } catch (FopImageException e) {
467             // e.printStackTrace(System.out);
468
log.error("PCLRenderer.renderImageArea() Error printing BMP ("
469                                    + e.toString() + ")");
470         }
471     }
472
473     /**
474      * render a foreign object area
475      */

476     public void renderForeignObjectArea(ForeignObjectArea area) {
477         // if necessary need to scale and align the content
478
this.currentXPosition = this.currentXPosition + area.getXOffset();
479         this.currentYPosition = this.currentYPosition;
480         switch (area.getAlign()) {
481         case TextAlign.START:
482             break;
483         case TextAlign.END:
484             break;
485         case TextAlign.CENTER:
486         case TextAlign.JUSTIFY:
487             break;
488         }
489         switch (area.getVerticalAlign()) {
490         case VerticalAlign.BASELINE:
491             break;
492         case VerticalAlign.MIDDLE:
493             break;
494         case VerticalAlign.SUB:
495             break;
496         case VerticalAlign.SUPER:
497             break;
498         case VerticalAlign.TEXT_TOP:
499             break;
500         case VerticalAlign.TEXT_BOTTOM:
501             break;
502         case VerticalAlign.TOP:
503             break;
504         case VerticalAlign.BOTTOM:
505             break;
506         }
507         // in general the content will not be text
508

509         // align and scale
510

511         switch (area.scalingMethod()) {
512         case Scaling.UNIFORM:
513             break;
514         case Scaling.NON_UNIFORM:
515             break;
516         }
517         // if the overflow is auto (default), scroll or visible
518
// then the contents should not be clipped, since this
519
// is considered a printing medium.
520
switch (area.getOverflow()) {
521         case Overflow.VISIBLE:
522         case Overflow.SCROLL:
523         case Overflow.AUTO:
524             break;
525         case Overflow.HIDDEN:
526             break;
527         }
528         area.getObject().render(this);
529
530         this.currentXPosition += area.getEffectiveWidth();
531         // this.currentYPosition -= area.getEffectiveHeight();
532
}
533
534     /**
535      * render SVG area to PCL
536      *
537      * @param area the SVG area to render
538      */

539     public void renderSVGArea(SVGArea area) {
540         if (debug)
541             System.out.println("PCLRenderer.renderSVGArea(" + area + ")");
542         int x = this.currentXPosition;
543         int y = this.currentYPosition;
544         SVGSVGElement svg =
545             ((SVGDocument)area.getSVGDocument()).getRootElement();
546         int w = (int)(svg.getWidth().getBaseVal().getValue() * 1000);
547         int h = (int)(svg.getHeight().getBaseVal().getValue() * 1000);
548
549         /*
550          * Clip to the svg area.
551          * Note: To have the svg overlay (under) a text area then use
552          * an fo:block-container
553          */

554
555         // TODO - translate and clip to viewbox
556

557         // currentStream.add(svgRenderer.getString());
558

559         // currentStream.add("Q\n");
560
}
561
562
563     public void setFont(String JavaDoc name, float size) {
564         int fontcode = 0;
565         if (name.length() > 1 && name.charAt(0) == 'F') {
566             try {
567                 fontcode = Integer.parseInt(name.substring(1));
568             } catch (Exception JavaDoc e) {
569                 e.printStackTrace();
570             }
571         }
572         switch (fontcode) {
573         case 1: // F1 = Helvetica
574
// currentStream.add("\033(8U\033(s1p" + (size / 1000) + "v0s0b24580T");
575
// Arial is more common among PCL5 printers than Helvetica - so use Arial
576

577             currentStream.add("\033(0N\033(s1p" + (size / 1000)
578                               + "v0s0b16602T");
579             break;
580         case 2: // F2 = Helvetica Oblique
581

582             currentStream.add("\033(0N\033(s1p" + (size / 1000)
583                               + "v1s0b16602T");
584             break;
585         case 3: // F3 = Helvetica Bold
586

587             currentStream.add("\033(0N\033(s1p" + (size / 1000)
588                               + "v0s3b16602T");
589             break;
590         case 4: // F4 = Helvetica Bold Oblique
591

592             currentStream.add("\033(0N\033(s1p" + (size / 1000)
593                               + "v1s3b16602T");
594             break;
595         case 5: // F5 = Times Roman
596
// currentStream.add("\033(8U\033(s1p" + (size / 1000) + "v0s0b25093T");
597
// Times New is more common among PCL5 printers than Times - so use Times New
598

599             currentStream.add("\033(0N\033(s1p" + (size / 1000)
600                               + "v0s0b16901T");
601             break;
602         case 6: // F6 = Times Italic
603

604             currentStream.add("\033(0N\033(s1p" + (size / 1000)
605                               + "v1s0b16901T");
606             break;
607         case 7: // F7 = Times Bold
608

609             currentStream.add("\033(0N\033(s1p" + (size / 1000)
610                               + "v0s3b16901T");
611             break;
612         case 8: // F8 = Times Bold Italic
613

614             currentStream.add("\033(0N\033(s1p" + (size / 1000)
615                               + "v1s3b16901T");
616             break;
617         case 9: // F9 = Courier
618

619             currentStream.add("\033(0N\033(s0p"
620                               + (120.01f / (size / 1000.00f)) + "h0s0b4099T");
621             break;
622         case 10: // F10 = Courier Oblique
623

624             currentStream.add("\033(0N\033(s0p"
625                               + (120.01f / (size / 1000.00f)) + "h1s0b4099T");
626             break;
627         case 11: // F11 = Courier Bold
628

629             currentStream.add("\033(0N\033(s0p"
630                               + (120.01f / (size / 1000.00f)) + "h0s3b4099T");
631             break;
632         case 12: // F12 = Courier Bold Oblique
633

634             currentStream.add("\033(0N\033(s0p"
635                               + (120.01f / (size / 1000.00f)) + "h1s3b4099T");
636             break;
637         case 13: // F13 = Symbol
638

639             currentStream.add("\033(19M\033(s1p" + (size / 1000)
640                               + "v0s0b16686T");
641             // currentStream.add("\033(9U\033(s1p" + (size / 1000) + "v0s0b25093T"); // ECMA Latin 1 Symbol Set in Times Roman???
642
break;
643         case 14: // F14 = Zapf Dingbats
644

645             currentStream.add("\033(14L\033(s1p" + (size / 1000)
646                               + "v0s0b45101T");
647             break;
648         default:
649             currentStream.add("\033(0N\033(s" + (size / 1000) + "V");
650             break;
651         }
652     }
653
654     /**
655      * render inline area to PCL
656      *
657      * @param area inline area to render
658      */

659     public void renderWordArea(WordArea area) {
660         String JavaDoc name = area.getFontState().getFontName();
661         int size = area.getFontState().getFontSize();
662
663         float red = area.getRed();
664         float green = area.getGreen();
665         float blue = area.getBlue();
666         PDFColor theAreaColor = new PDFColor((double)area.getRed(),
667                                              (double)area.getGreen(),
668                                              (double)area.getBlue());
669
670         // currentStream.add("\033*c" + (int)(100 - ((0.3f * red + 0.59f * green + 0.11f * blue) * 100f)) + "G\033*v2T");
671
currentStream.add("\033*v1O\033*c"
672                           + (int)(100 - ((0.3f * red + 0.59f * green + 0.11f * blue) * 100f))
673                           + "G\033*v2T");
674
675         if ((!name.equals(this.currentFontName))
676                 || (size != this.currentFontSize)) {
677             this.currentFontName = name;
678             this.currentFontSize = size;
679             setFont(name, size);
680         }
681
682         this.currentFill = theAreaColor;
683
684         int rx = this.currentXPosition;
685         int bl = this.currentYPosition;
686
687         String JavaDoc s = area.getText();
688
689         addWordLines(area, rx, bl, size, theAreaColor);
690
691         int xpos = xoffset + (rx / 100);
692         if (xpos < 0) {
693             xpos = 0;
694             log.warn("Horizontal position out of bounds.");
695         }
696         currentStream.add("\033&a" + xpos + "h" + (pageHeight - (bl / 100))
697                           + "V" + s);
698
699         this.currentXPosition += area.getContentWidth();
700     }
701
702     /**
703      * render page into PCL
704      *
705      * @param page page to render
706      */

707     public void renderPage(Page page) {
708         if (debug)
709             System.out.println("PCLRenderer.renderPage() page.Height() = "
710                                + page.getHeight());
711
712         // FIXME: Set orientation. We made this assumption. It's not always correct but we need a
713
// method to generate landscape pages
714
if (page.getHeight() < page.getWidth()) {
715              orientation = 1;
716         }
717         currentStream.add("\033&l" + orientation + "O");
718         if (orientation == 1 || orientation == 3) {
719             xoffset = -144;
720         } else {
721             xoffset = -180;
722         }
723                                
724         if (paperheight > 0 && divisions == -1)
725             divisions = paperheight / (page.getHeight() / 100);
726
727         if (debug)
728             System.out.println("PCLRenderer.renderPage() paperheight="
729                                + paperheight + " divisions=" + divisions);
730
731         // Set top margin.
732
// float fullmargin = 0;
733
if (divisions > 0)
734             fullmargin = paperheight * curdiv / divisions;
735         if (topmargin > 0)
736             fullmargin += topmargin;
737         if (debug)
738             System.out.println("PCLRenderer.renderPage() curdiv=" + curdiv
739                                + " fullmargin=" + fullmargin);
740         // if ( fullmargin > 0 )
741
// currentStream.add("\033&l" + (fullmargin / 15f) + "c1e8C");
742
// this.currentYPosition = fullmargin * 100;
743

744         if (paperheight > 0)
745             pageHeight = (paperheight / divisions) + fullmargin;
746         else
747             pageHeight = page.getHeight() / 100;
748         if (debug)
749             System.out.println("PCLRenderer.renderPage() Set currentYPosition="
750                                + this.currentYPosition);
751         if (leftmargin > 0 && curdiv == 0)
752             currentStream.add("\033&k" + (leftmargin / 6f)
753                               + "H\033&a1L\033&k12H");
754
755         this.currentFontName = "";
756         this.currentFontSize = 0;
757
758         renderRegions(page);
759
760         // End page.
761
if (++curdiv == divisions || divisions == -1) {
762             curdiv = 0;
763             currentStream.add("\f");
764         }
765
766         // Links, etc not implemented...
767
/*
768          * currentPage = this.pdfDoc.makePage(this.pdfResources, currentStream,
769          * page.getWidth()/1000, page.getHeight()/1000, page);
770          * if (page.hasLinks()) {
771          * currentAnnotList = this.pdfDoc.makeAnnotList();
772          * currentPage.setAnnotList(currentAnnotList);
773          * Enumeration e = page.getLinkSets().elements();
774          * while (e.hasMoreElements()) {
775          * LinkSet linkSet = (LinkSet) e.nextElement();
776          * linkSet.align();
777          * String dest = linkSet.getDest();
778          * int linkType = linkSet.getLinkType();
779          * Enumeration f = linkSet.getRects().elements();
780          * while (f.hasMoreElements()) {
781          * LinkedRectangle lrect = (LinkedRectangle) f.nextElement();
782          * currentAnnotList.addLink(
783          * this.pdfDoc.makeLink(lrect.getRectangle(), dest, linkType));
784          * }
785          * }
786          * } else {
787          * // just to be on the safe side
788          * currentAnnotList = null;
789          * }
790          */

791     }
792     public void startRenderer(OutputStream JavaDoc outputStream)
793     throws IOException JavaDoc {
794         log.info("rendering areas to PCL");
795         currentStream = new PCLStream(outputStream);
796
797         // Reset the margins.
798
currentStream.add("\033" + "9\033&l0E");
799     }
800
801     public void stopRenderer(OutputStream JavaDoc outputStream)
802     throws IOException JavaDoc {
803         log.info("writing out PCL");
804         outputStream.flush();
805     }
806
807     public void render(Page page, OutputStream JavaDoc outputStream)
808     throws IOException JavaDoc {
809         idReferences = page.getIDReferences();
810         this.renderPage(page);
811     }
812
813 }
814
Popular Tags