diff options
author | Jeremias Maerki <jeremias@apache.org> | 2006-05-03 13:47:18 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2006-05-03 13:47:18 +0000 |
commit | fc3f1a4c0a64e7f5559e30fbd2664900326d8359 (patch) | |
tree | d2acf51af5b2b65b746f14c527a766250714617d /src/java/org/apache/fop/render/AbstractGraphics2DAdapter.java | |
parent | 48d632c3c572444f32df26859e7d876eaed1dc2f (diff) | |
download | xmlgraphics-fop-fc3f1a4c0a64e7f5559e30fbd2664900326d8359.tar.gz xmlgraphics-fop-fc3f1a4c0a64e7f5559e30fbd2664900326d8359.zip |
Some refactoring in the XML handling area which results in more code reuse and less redundancy.
Support for i-f-o for the AFPRenderer including a Graphics2DAdapter so extensions like Barcode4J can paint barcodes more efficiently (i.e. not via SVG).
The SVGConverter class could now be removed except for the writeImage() method because the same functionality is now covered by the Graphics2DAdapter via renderDocument() in the renderer.
PCL and AFP now use practically the same extension painting code (both render to bitmap images).
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@399306 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/AbstractGraphics2DAdapter.java')
-rw-r--r-- | src/java/org/apache/fop/render/AbstractGraphics2DAdapter.java | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/java/org/apache/fop/render/AbstractGraphics2DAdapter.java b/src/java/org/apache/fop/render/AbstractGraphics2DAdapter.java new file mode 100644 index 000000000..af5343f33 --- /dev/null +++ b/src/java/org/apache/fop/render/AbstractGraphics2DAdapter.java @@ -0,0 +1,92 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* $Id$ */ + +package org.apache.fop.render; + +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.RenderingHints; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.fop.render.Graphics2DAdapter; +import org.apache.fop.render.Graphics2DImagePainter; +import org.apache.fop.render.RendererContext.RendererContextWrapper; +import org.apache.fop.util.UnitConv; + +/** + * Graphics2DAdapter implementation for PCL and HP GL/2. + */ +public abstract class AbstractGraphics2DAdapter implements Graphics2DAdapter { + + /** + * Paints the image to a BufferedImage and returns that. + * @param painter the painter which will paint the actual image + * @param context the renderer context for the current renderer + * @param resolution the requested bitmap resolution + * @param gray true if the generated image should be in grayscales + * @return the generated BufferedImage + */ + protected BufferedImage paintToBufferedImage(Graphics2DImagePainter painter, + RendererContextWrapper context, int resolution, boolean gray) { + int bmw = UnitConv.mpt2px(context.getWidth(), resolution); + int bmh = UnitConv.mpt2px(context.getHeight(), resolution); + BufferedImage bi; + if (gray) { + bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_GRAY); + } else { + bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_ARGB); + } + Graphics2D g2d = bi.createGraphics(); + try { + g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, + RenderingHints.VALUE_FRACTIONALMETRICS_ON); + setRenderingHintsForBufferedImage(g2d); + + g2d.setBackground(Color.white); + g2d.setColor(Color.black); + g2d.clearRect(0, 0, bmw, bmh); + double sx = (double)bmw / context.getWidth(); + double sy = (double)bmh / context.getHeight(); + g2d.scale(sx, sy); + + //Paint the image on the BufferedImage + Rectangle2D area = new Rectangle2D.Double( + 0.0, 0.0, context.getWidth(), context.getHeight()); + painter.paint(g2d, area); + } finally { + g2d.dispose(); + } + return bi; + } + + /** + * Sets rendering hints on the Graphics2D created for painting to a BufferedImage. Subclasses + * can modify the settings to customize the behaviour. + * @param g2d the Graphics2D instance + */ + protected void setRenderingHintsForBufferedImage(Graphics2D g2d) { + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_OFF); + g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, + RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); + } + +} |