aboutsummaryrefslogtreecommitdiffstats
path: root/src/java
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2009-10-02 09:19:12 +0000
committerJeremias Maerki <jeremias@apache.org>2009-10-02 09:19:12 +0000
commitb6638c65c94d5afb8ca0f6ea0b37632cf8fb11be (patch)
tree2d71f9832b7552ce8e326c61ff3def25e7f6b190 /src/java
parent05c9bf87263a7917e1ee8e04de55505d1d564135 (diff)
downloadxmlgraphics-fop-b6638c65c94d5afb8ca0f6ea0b37632cf8fb11be.tar.gz
xmlgraphics-fop-b6638c65c94d5afb8ca0f6ea0b37632cf8fb11be.zip
Added support for creating thumbnails or preview bitmaps of fixed size for PNG and TIFF output.
Documentation update for bitmap output. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@820939 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java')
-rw-r--r--src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java70
-rw-r--r--src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java5
2 files changed, 67 insertions, 8 deletions
diff --git a/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java b/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java
index 3449ffe7e..e4606858b 100644
--- a/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java
+++ b/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java
@@ -22,9 +22,11 @@ package org.apache.fop.render.bitmap;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
+import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
+import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
@@ -53,6 +55,13 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
/** logging instance */
private static Log log = LogFactory.getLog(AbstractBitmapDocumentHandler.class);
+ /**
+ * Rendering Options key for the controlling the required bitmap size to create.
+ * This is used to create thumbnails, for example. If used, the target resolution is ignored.
+ * Value type: java.awt.Dimension (size in pixels)
+ */
+ public static final String TARGET_BITMAP_SIZE = "target-bitmap-size";
+
private ImageWriter imageWriter;
private MultiImageWriter multiImageWriter;
@@ -66,6 +75,7 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
private BitmapRenderingSettings bitmapSettings = new BitmapRenderingSettings();
private double scaleFactor = 1.0;
+ private Dimension targetBitmapSize;
/**
* Default constructor.
@@ -94,6 +104,9 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
//Set target resolution
int dpi = Math.round(context.getUserAgent().getTargetResolution());
getSettings().getWriterParams().setResolution(dpi);
+
+ Map renderingOptions = getUserAgent().getRendererOptions();
+ setTargetBitmapSize((Dimension)renderingOptions.get(TARGET_BITMAP_SIZE));
}
/** {@inheritDoc} */
@@ -113,6 +126,17 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
setFontInfo(fi);
}
+ /**
+ * Sets the target bitmap size (in pixels) of the bitmap that should be produced. Normally,
+ * the bitmap size is calculated automatically based on the page size and the target
+ * resolution. But for example, if you want to create thumbnails or small preview bitmaps
+ * from pages it is more practical (and efficient) to set the required bitmap size.
+ * @param size the target bitmap size (in pixels)
+ */
+ public void setTargetBitmapSize(Dimension size) {
+ this.targetBitmapSize = size;
+ }
+
//----------------------------------------------------------------------------------------------
/** {@inheritDoc} */
@@ -176,13 +200,43 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
/** {@inheritDoc} */
public IFPainter startPageContent() throws IFException {
- double scale = scaleFactor
- * (25.4f / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
- / getUserAgent().getTargetPixelUnitToMillimeter();
- int bitmapWidth = (int) ((this.currentPageDimensions.width * scale / 1000f) + 0.5f);
- int bitmapHeight = (int) ((this.currentPageDimensions.height * scale / 1000f) + 0.5f);
+ int bitmapWidth;
+ int bitmapHeight;
+ double scale;
+ Point2D offset = null;
+ if (targetBitmapSize != null) {
+ //Fit the generated page proportionally into the given rectangle (in pixels)
+ double scale2w = 1000 * targetBitmapSize.width
+ / this.currentPageDimensions.getWidth();
+ double scale2h = 1000 * targetBitmapSize.height
+ / this.currentPageDimensions.getHeight();
+ bitmapWidth = targetBitmapSize.width;
+ bitmapHeight = targetBitmapSize.height;
+
+ //Centering the page in the given bitmap
+ offset = new Point2D.Double();
+ if (scale2w < scale2h) {
+ scale = scale2w;
+ double h = this.currentPageDimensions.height * scale / 1000;
+ offset.setLocation(0, (bitmapHeight - h) / 2.0);
+ } else {
+ scale = scale2h;
+ double w = this.currentPageDimensions.width * scale / 1000;
+ offset.setLocation((bitmapWidth - w) / 2.0, 0);
+ }
+ } else {
+ //Normal case: just scale according to the target resolution
+ scale = scaleFactor
+ * getUserAgent().getTargetResolution()
+ / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION;
+ bitmapWidth = (int) ((this.currentPageDimensions.width * scale / 1000f) + 0.5f);
+ bitmapHeight = (int) ((this.currentPageDimensions.height * scale / 1000f) + 0.5f);
+ }
+
+ //Set up bitmap to paint on
this.currentImage = createBufferedImage(bitmapWidth, bitmapHeight);
Graphics2D graphics2D = this.currentImage.createGraphics();
+
// draw page background
if (!getSettings().hasTransparentPageBackground()) {
graphics2D.setBackground(getSettings().getPageBackgroundColor());
@@ -190,6 +244,7 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
graphics2D.fillRect(0, 0, bitmapWidth, bitmapHeight);
}
+ //Set rendering hints
graphics2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
if (getSettings().isAntiAliasingEnabled()
@@ -213,6 +268,11 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
}
graphics2D.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
+
+ //Set up initial coordinate system for the page
+ if (offset != null) {
+ graphics2D.translate(offset.getX(), offset.getY());
+ }
graphics2D.scale(scale / 1000f, scale / 1000f);
return new Java2DPainter(graphics2D, getContext(), getFontInfo());
diff --git a/src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java b/src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java
index 5237dbe35..1b6c43700 100644
--- a/src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java
+++ b/src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java
@@ -19,7 +19,6 @@
package org.apache.fop.render.bitmap;
-import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.List;
@@ -80,8 +79,6 @@ public class BitmapRendererConfigurator extends Java2DRendererConfigurator
if (background != null) {
settings.setPageBackgroundColor(
ColorUtil.parseColorString(this.userAgent, background));
- } else {
- settings.setPageBackgroundColor(Color.WHITE);
}
}
@@ -106,6 +103,8 @@ public class BitmapRendererConfigurator extends Java2DRendererConfigurator
settings.setBufferedImageType(BufferedImage.TYPE_BYTE_GRAY);
} else if ("binary".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
+ } else if ("bi-level".equalsIgnoreCase(color)) {
+ settings.setBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
} else {
throw new FOPException("Invalid value for color-mode: " + color);
}