KickJava   Java API By Example, From Geeks To Geeks.

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


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;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Graphics2D JavaDoc;
24 import java.awt.Point JavaDoc;
25 import java.awt.RenderingHints JavaDoc;
26 import java.awt.Transparency JavaDoc;
27 import java.awt.color.ColorSpace JavaDoc;
28 import java.awt.geom.Rectangle2D JavaDoc;
29 import java.awt.image.BufferedImage JavaDoc;
30 import java.awt.image.ColorModel JavaDoc;
31 import java.awt.image.ComponentColorModel JavaDoc;
32 import java.awt.image.DataBuffer JavaDoc;
33 import java.awt.image.Raster JavaDoc;
34 import java.awt.image.WritableRaster JavaDoc;
35
36 import org.apache.fop.render.Graphics2DAdapter;
37 import org.apache.fop.render.Graphics2DImagePainter;
38 import org.apache.fop.render.RendererContext.RendererContextWrapper;
39 import org.apache.fop.util.UnitConv;
40
41 /**
42  * Graphics2DAdapter implementation for PCL and HP GL/2.
43  */

44 public abstract class AbstractGraphics2DAdapter implements Graphics2DAdapter {
45
46     /**
47      * Paints the image to a BufferedImage and returns that.
48      * @param painter the painter which will paint the actual image
49      * @param context the renderer context for the current renderer
50      * @param resolution the requested bitmap resolution
51      * @param gray true if the generated image should be in grayscales
52      * @return the generated BufferedImage
53      */

54     protected BufferedImage JavaDoc paintToBufferedImage(Graphics2DImagePainter painter,
55              RendererContextWrapper context, int resolution, boolean gray, boolean withAlpha) {
56         int bmw = (int)Math.ceil(UnitConv.mpt2px(context.getWidth(), resolution));
57         int bmh = (int)Math.ceil(UnitConv.mpt2px(context.getHeight(), resolution));
58         BufferedImage JavaDoc bi;
59         if (gray) {
60             if (withAlpha) {
61                 bi = createGrayBufferedImageWithAlpha(bmw, bmh);
62             } else {
63                 bi = new BufferedImage JavaDoc(bmw, bmh, BufferedImage.TYPE_BYTE_GRAY);
64             }
65         } else {
66             if (withAlpha) {
67                 bi = new BufferedImage JavaDoc(bmw, bmh, BufferedImage.TYPE_INT_ARGB);
68             } else {
69                 bi = new BufferedImage JavaDoc(bmw, bmh, BufferedImage.TYPE_INT_RGB);
70             }
71         }
72         Graphics2D JavaDoc g2d = bi.createGraphics();
73         try {
74             g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
75                     RenderingHints.VALUE_FRACTIONALMETRICS_ON);
76             setRenderingHintsForBufferedImage(g2d);
77             
78             g2d.setBackground(Color.white);
79             g2d.setColor(Color.black);
80             if (!withAlpha) {
81                 g2d.clearRect(0, 0, bmw, bmh);
82             }
83             /* debug code
84             int off = 2;
85             g2d.drawLine(off, 0, off, bmh);
86             g2d.drawLine(bmw - off, 0, bmw - off, bmh);
87             g2d.drawLine(0, off, bmw, off);
88             g2d.drawLine(0, bmh - off, bmw, bmh - off);
89             */

90             double sx = (double)bmw / context.getWidth();
91             double sy = (double)bmh / context.getHeight();
92             g2d.scale(sx, sy);
93
94             //Paint the image on the BufferedImage
95
Rectangle2D JavaDoc area = new Rectangle2D.Double JavaDoc(
96                     0.0, 0.0, context.getWidth(), context.getHeight());
97             painter.paint(g2d, area);
98         } finally {
99             g2d.dispose();
100         }
101         return bi;
102     }
103
104     private static BufferedImage JavaDoc createGrayBufferedImageWithAlpha(int width, int height) {
105         BufferedImage JavaDoc bi;
106         boolean alphaPremultiplied = true;
107         int bands = 2;
108         int[] bits = new int[bands];
109         for (int i = 0; i < bands; i++) {
110             bits[i] = 8;
111         }
112         ColorModel JavaDoc cm = new ComponentColorModel JavaDoc(
113                 ColorSpace.getInstance(ColorSpace.CS_GRAY),
114                 bits,
115                 true, alphaPremultiplied,
116                 Transparency.TRANSLUCENT,
117                 DataBuffer.TYPE_BYTE);
118         WritableRaster JavaDoc wr = Raster.createInterleavedRaster(
119                 DataBuffer.TYPE_BYTE,
120                 width, height, bands,
121                 new Point JavaDoc(0, 0));
122         bi = new BufferedImage JavaDoc(cm, wr, alphaPremultiplied, null);
123         return bi;
124     }
125
126     /**
127      * Sets rendering hints on the Graphics2D created for painting to a BufferedImage. Subclasses
128      * can modify the settings to customize the behaviour.
129      * @param g2d the Graphics2D instance
130      */

131     protected void setRenderingHintsForBufferedImage(Graphics2D JavaDoc g2d) {
132         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
133             RenderingHints.VALUE_ANTIALIAS_OFF);
134         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
135             RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
136     }
137
138 }
139
Popular Tags