KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > pdf > PDFGraphics2DAdapter


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: PDFGraphics2DAdapter.java 437210 2006-08-26 18:59:42Z jeremias $ */
19  
20 package org.apache.fop.render.pdf;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Graphics2D JavaDoc;
25 import java.awt.RenderingHints JavaDoc;
26 import java.awt.geom.AffineTransform JavaDoc;
27 import java.awt.geom.Rectangle2D JavaDoc;
28 import java.awt.image.BufferedImage JavaDoc;
29 import java.io.IOException JavaDoc;
30
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.render.RendererContext.RendererContextWrapper;
35 import org.apache.fop.svg.PDFGraphics2D;
36
37 /**
38  * Graphics2DAdapter implementation for PDF.
39  */

40 public class PDFGraphics2DAdapter extends AbstractGraphics2DAdapter {
41
42     private PDFRenderer renderer;
43
44     /**
45      * Main constructor
46      * @param renderer the Renderer instance to which this instance belongs
47      */

48     public PDFGraphics2DAdapter(PDFRenderer renderer) {
49         this.renderer = renderer;
50     }
51     
52     /** @see org.apache.fop.render.Graphics2DAdapter */
53     public void paintImage(Graphics2DImagePainter painter,
54             RendererContext context,
55             int x, int y, int width, int height) throws IOException JavaDoc {
56         
57         PDFSVGHandler.PDFInfo pdfInfo = PDFSVGHandler.getPDFInfo(context);
58         float fwidth = width / 1000f;
59         float fheight = height / 1000f;
60         float fx = x / 1000f;
61         float fy = y / 1000f;
62         
63         // get the 'width' and 'height' attributes of the SVG document
64
Dimension JavaDoc dim = painter.getImageSize();
65         float imw = (float)dim.getWidth() / 1000f;
66         float imh = (float)dim.getHeight() / 1000f;
67
68         float sx = fwidth / (float)imw;
69         float sy = fheight / (float)imh;
70
71         renderer.saveGraphicsState();
72         renderer.setColor(Color.black, false, null);
73         renderer.setColor(Color.black, true, null);
74         
75         //TODO Clip to the image area.
76

77         // transform so that the coordinates (0,0) is from the top left
78
// and positive is down and to the right. (0,0) is where the
79
// viewBox puts it.
80
renderer.currentStream.add(sx + " 0 0 " + sy + " " + fx + " "
81                           + fy + " cm\n");
82
83
84         final boolean textAsShapes = false;
85         if (pdfInfo.pdfContext == null) {
86             pdfInfo.pdfContext = pdfInfo.pdfPage;
87         }
88         PDFGraphics2D graphics = new PDFGraphics2D(textAsShapes,
89                 pdfInfo.fi, pdfInfo.pdfDoc,
90                 pdfInfo.pdfContext, pdfInfo.pdfPage.referencePDF(),
91                 renderer.currentFontName,
92                 renderer.currentFontSize);
93         graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
94         
95         AffineTransform JavaDoc transform = new AffineTransform JavaDoc();
96         transform.translate(fx, fy);
97         pdfInfo.pdfState.concatenate(transform);
98         graphics.setPDFState(pdfInfo.pdfState);
99         graphics.setOutputStream(pdfInfo.outputStream);
100
101         if (pdfInfo.paintAsBitmap) {
102             //Fallback solution: Paint to a BufferedImage
103
int resolution = (int)Math.round(context.getUserAgent().getTargetResolution());
104             RendererContextWrapper ctx = RendererContext.wrapRendererContext(context);
105             BufferedImage JavaDoc bi = paintToBufferedImage(painter, ctx, resolution, false, false);
106
107             float scale = PDFRenderer.NORMAL_PDF_RESOLUTION
108                             / context.getUserAgent().getTargetResolution();
109             graphics.drawImage(bi, new AffineTransform JavaDoc(scale, 0, 0, scale, 0, 0), null);
110         } else {
111             Rectangle2D JavaDoc area = new Rectangle2D.Double JavaDoc(0.0, 0.0, imw, imh);
112             painter.paint(graphics, area);
113         }
114
115         pdfInfo.currentStream.add(graphics.getString());
116         renderer.restoreGraphicsState();
117         pdfInfo.pdfState.pop();
118     }
119
120     /**
121      * @see org.apache.fop.render.AbstractGraphics2DAdapter#setRenderingHintsForBufferedImage(
122      * java.awt.Graphics2D)
123      */

124     protected void setRenderingHintsForBufferedImage(Graphics2D JavaDoc g2d) {
125         super.setRenderingHintsForBufferedImage(g2d);
126         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
127                 RenderingHints.VALUE_ANTIALIAS_ON);
128         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
129                 RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
130     }
131
132 }
133
Popular Tags