KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > ps > PSGraphics2DAdapter


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: PSGraphics2DAdapter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.render.ps;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.geom.AffineTransform JavaDoc;
24 import java.awt.geom.Rectangle2D JavaDoc;
25 import java.io.IOException JavaDoc;
26
27 import org.apache.fop.render.Graphics2DAdapter;
28 import org.apache.fop.render.Graphics2DImagePainter;
29 import org.apache.fop.render.RendererContext;
30 import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
31 import org.apache.xmlgraphics.ps.PSGenerator;
32
33 /**
34  * Graphics2DAdapter implementation for PostScript.
35  */

36 public class PSGraphics2DAdapter implements Graphics2DAdapter {
37
38     private PSRenderer renderer;
39
40     /**
41      * Main constructor
42      * @param renderer the Renderer instance to which this instance belongs
43      */

44     public PSGraphics2DAdapter(PSRenderer renderer) {
45         this.renderer = renderer;
46     }
47     
48     /** @see org.apache.fop.render.Graphics2DAdapter */
49     public void paintImage(Graphics2DImagePainter painter,
50             RendererContext context,
51             int x, int y, int width, int height) throws IOException JavaDoc {
52         PSGenerator gen = renderer.gen;
53         
54         float fwidth = width / 1000f;
55         float fheight = height / 1000f;
56         float fx = x / 1000f;
57         float fy = y / 1000f;
58         
59         // get the 'width' and 'height' attributes of the SVG document
60
Dimension JavaDoc dim = painter.getImageSize();
61         float imw = (float)dim.getWidth() / 1000f;
62         float imh = (float)dim.getHeight() / 1000f;
63
64         float sx = fwidth / (float)imw;
65         float sy = fheight / (float)imh;
66
67         gen.commentln("%FOPBeginGraphics2D");
68         gen.saveGraphicsState();
69         // Clip to the image area.
70
gen.writeln("newpath");
71         gen.defineRect(fx, fy, fwidth, fheight);
72         gen.writeln("clip");
73         
74         // transform so that the coordinates (0,0) is from the top left
75
// and positive is down and to the right. (0,0) is where the
76
// viewBox puts it.
77
gen.concatMatrix(sx, 0, 0, sy, fx, fy);
78
79         final boolean textAsShapes = false;
80         PSGraphics2D graphics = new PSGraphics2D(textAsShapes, gen);
81         graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
82         AffineTransform JavaDoc transform = new AffineTransform JavaDoc();
83         // scale to viewbox
84
transform.translate(fx, fy);
85         gen.getCurrentState().concatMatrix(transform);
86         Rectangle2D JavaDoc area = new Rectangle2D.Double JavaDoc(0.0, 0.0, imw, imh);
87         painter.paint(graphics, area);
88
89         gen.restoreGraphicsState();
90         gen.commentln("%FOPEndGraphics2D");
91     }
92
93 }
94
Popular Tags