KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id$ */
19  
20 package org.apache.fop.render.pcl;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.geom.AffineTransform JavaDoc;
24 import java.awt.geom.Rectangle2D JavaDoc;
25 import java.awt.image.BufferedImage JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import org.apache.commons.io.output.ByteArrayOutputStream;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.fop.render.AbstractGraphics2DAdapter;
32 import org.apache.fop.render.Graphics2DImagePainter;
33 import org.apache.fop.render.RendererContext;
34 import org.apache.fop.util.UnitConv;
35 import org.apache.xmlgraphics.java2d.GraphicContext;
36
37 /**
38  * Graphics2DAdapter implementation for PCL and HP GL/2.
39  */

40 public class PCLGraphics2DAdapter extends AbstractGraphics2DAdapter {
41
42     /** logging instance */
43     private static Log log = LogFactory.getLog(PCLGraphics2DAdapter.class);
44
45     /**
46      * Main constructor
47      */

48     public PCLGraphics2DAdapter() {
49     }
50     
51     /** @see org.apache.fop.render.Graphics2DAdapter */
52     public void paintImage(Graphics2DImagePainter painter,
53             RendererContext context,
54             int x, int y, int width, int height) throws IOException JavaDoc {
55         PCLRendererContext pclContext = PCLRendererContext.wrapRendererContext(context);
56         PCLRenderer pcl = (PCLRenderer)context.getRenderer();
57         PCLGenerator gen = pcl.gen;
58         
59         // get the 'width' and 'height' attributes of the image/document
60
Dimension JavaDoc dim = painter.getImageSize();
61         float imw = (float)dim.getWidth();
62         float imh = (float)dim.getHeight();
63
64         boolean painted = false;
65         boolean paintAsBitmap = pclContext.paintAsBitmap();
66         if (!paintAsBitmap) {
67             ByteArrayOutputStream baout = new ByteArrayOutputStream();
68             PCLGenerator tempGen = new PCLGenerator(baout, gen.getMaximumBitmapResolution());
69             try {
70                 GraphicContext ctx = (GraphicContext)pcl.getGraphicContext().clone();
71
72                 AffineTransform JavaDoc prepareHPGL2 = new AffineTransform JavaDoc();
73                 prepareHPGL2.scale(0.001, 0.001);
74                 ctx.setTransform(prepareHPGL2);
75
76                 PCLGraphics2D graphics = new PCLGraphics2D(tempGen);
77                 graphics.setGraphicContext(ctx);
78                 graphics.setClippingDisabled(pclContext.isClippingDisabled());
79                 Rectangle2D JavaDoc area = new Rectangle2D.Double JavaDoc(0.0, 0.0, imw, imh);
80                 painter.paint(graphics, area);
81                 
82                 //If we arrive here, the graphic is natively paintable, so write the graphic
83
pcl.saveGraphicsState();
84                 pcl.setCursorPos(x, y);
85                 gen.writeCommand("*c" + gen.formatDouble4(width / 100f) + "x"
86                         + gen.formatDouble4(height / 100f) + "Y");
87                 gen.writeCommand("*c0T");
88                 gen.enterHPGL2Mode(false);
89                 gen.writeText("\nIN;");
90                 gen.writeText("SP1;");
91                 //One Plotter unit is 0.025mm!
92
double scale = imw / UnitConv.mm2pt(imw * 0.025);
93                 gen.writeText("SC0," + gen.formatDouble4(scale)
94                         + ",0,-" + gen.formatDouble4(scale) + ",2;");
95                 gen.writeText("IR0,100,0,100;");
96                 gen.writeText("PU;PA0,0;\n");
97                 baout.writeTo(gen.getOutputStream()); //Buffer is written to output stream
98
gen.writeText("\n");
99
100                 gen.enterPCLMode(false);
101                 pcl.restoreGraphicsState();
102                 painted = true;
103             } catch (UnsupportedOperationException JavaDoc uoe) {
104                 log.debug(
105                     "Cannot paint graphic natively. Falling back to bitmap painting. Reason: "
106                         + uoe.getMessage());
107             }
108         }
109         
110         if (!painted) {
111             //Fallback solution: Paint to a BufferedImage
112
int resolution = (int)Math.round(context.getUserAgent().getTargetResolution());
113             BufferedImage JavaDoc bi = paintToBufferedImage(painter, pclContext, resolution, true, false);
114
115             pcl.setCursorPos(x, y);
116             gen.paintBitmap(bi, new Dimension JavaDoc(width, height), pclContext.isSourceTransparency());
117         }
118     }
119
120 }
121
Popular Tags