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;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlgraphics.image.writer.ImageWriter;
import org.apache.xmlgraphics.image.writer.ImageWriterRegistry;
import org.apache.xmlgraphics.image.writer.MultiImageWriter;
import org.apache.fop.apps.FopFactoryConfig;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler;
import org.apache.fop.render.intermediate.IFContext;
import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFPainter;
import org.apache.fop.render.java2d.Java2DPainter;
import org.apache.fop.render.java2d.Java2DUtil;
public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {
private static Log log = LogFactory.getLog(AbstractBitmapDocumentHandler.class);
public static final String TARGET_BITMAP_SIZE = "target-bitmap-size";
private ImageWriter imageWriter;
private MultiImageWriter multiImageWriter;
private MultiFileRenderingUtil multiFileUtil;
private int pageCount;
private Dimension currentPageDimensions;
private BufferedImage currentImage;
private BitmapRenderingSettings bitmapSettings = new BitmapRenderingSettings();
private double scaleFactor = 1.0;
private Dimension targetBitmapSize;
public AbstractBitmapDocumentHandler(IFContext context) {
super(context);
int dpi = Math.round(context.getUserAgent().getTargetResolution());
getSettings().setResolution(dpi);
Map renderingOptions = getUserAgent().getRendererOptions();
setTargetBitmapSize((Dimension)renderingOptions.get(TARGET_BITMAP_SIZE));
}
public boolean supportsPagesOutOfOrder() {
return false;
}
public abstract String getMimeType();
public abstract String getDefaultExtension();
public abstract IFDocumentHandlerConfigurator getConfigurator();
public BitmapRenderingSettings getSettings() {
return this.bitmapSettings;
}
public void setDefaultFontInfo(FontInfo fontInfo) {
FontInfo fi = Java2DUtil.buildDefaultJava2DBasedFontInfo(fontInfo, getUserAgent());
setFontInfo(fi);
}
public void setTargetBitmapSize(Dimension size) {
this.targetBitmapSize = size;
}
public void startDocument() throws IFException {
super.startDocument();
try {
this.imageWriter = ImageWriterRegistry.getInstance().getWriterFor(getMimeType());
if (this.imageWriter == null) {
BitmapRendererEventProducer eventProducer
= BitmapRendererEventProducer.Provider.get(
getUserAgent().getEventBroadcaster());
eventProducer.noImageWriterFound(this, getMimeType());
}
if (this.imageWriter.supportsMultiImageWriter()) {
this.multiImageWriter = this.imageWriter.createMultiImageWriter(outputStream);
} else {
this.multiFileUtil = new MultiFileRenderingUtil(getDefaultExtension(),
getUserAgent().getOutputFile());
}
this.pageCount = 0;
} catch (IOException e) {
throw new IFException("I/O error in startDocument()", e);
}
}
public void endDocumentHeader() throws IFException {
}
public void endDocument() throws IFException {
try {
if (this.multiImageWriter != null) {
this.multiImageWriter.close();
}
this.multiImageWriter = null;
this.imageWriter = null;
} catch (IOException ioe) {
throw new IFException("I/O error in endDocument()", ioe);
}
super.endDocument();
}
public void startPageSequence(String id) throws IFException {
}
public void endPageSequence() throws IFException {
}
public void startPage(int index, String name, String pageMasterName, Dimension size)
throws IFException {
this.pageCount++;
this.currentPageDimensions = new Dimension(size);
}
public IFPainter startPageContent() throws IFException {
int bitmapWidth;
int bitmapHeight;
double scale;
Point2D offset = null;
if (targetBitmapSize != null) {
double scale2w = 1000 * targetBitmapSize.width
/ this.currentPageDimensions.getWidth();
double scale2h = 1000 * targetBitmapSize.height
/ this.currentPageDimensions.getHeight();
bitmapWidth = targetBitmapSize.width;
bitmapHeight = targetBitmapSize.height;
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 {
scale = scaleFactor
* getUserAgent().getTargetResolution()
/ FopFactoryConfig.DEFAULT_TARGET_RESOLUTION;
bitmapWidth = (int) ((this.currentPageDimensions.width * scale / 1000f) + 0.5f);
bitmapHeight = (int) ((this.currentPageDimensions.height * scale / 1000f) + 0.5f);
}
if (currentImage == null || currentImage.getWidth() != bitmapWidth
|| currentImage.getHeight() != bitmapHeight) {
currentImage = createBufferedImage(bitmapWidth, bitmapHeight);
}
Graphics2D graphics2D = this.currentImage.createGraphics();
if (!getSettings().hasTransparentPageBackground()) {
graphics2D.setBackground(getSettings().getPageBackgroundColor());
graphics2D.setPaint(getSettings().getPageBackgroundColor());
graphics2D.fillRect(0, 0, bitmapWidth, bitmapHeight);
}
graphics2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
if (getSettings().isAntiAliasingEnabled()
&& this.currentImage.getColorModel().getPixelSize() > 1) {
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
} else {
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
}
if (getSettings().isQualityRenderingEnabled()) {
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
} else {
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_SPEED);
}
graphics2D.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
if (offset != null) {
graphics2D.translate(offset.getX(), offset.getY());
}
graphics2D.scale(scale / 1000f, scale / 1000f);
return new Java2DPainter(graphics2D, getContext(), getFontInfo(), this);
}
protected BufferedImage createBufferedImage(int bitmapWidth, int bitmapHeight) {
return new BufferedImage(bitmapWidth, bitmapHeight, getSettings().getBufferedImageType());
}
public void endPageContent() throws IFException {
try {
if (this.multiImageWriter == null) {
switch (this.pageCount) {
case 1:
this.imageWriter.writeImage(
this.currentImage, this.outputStream,
getSettings().getWriterParams());
IOUtils.closeQuietly(this.outputStream);
this.outputStream = null;
break;
default:
OutputStream out = this.multiFileUtil.createOutputStream(this.pageCount - 1);
if (out == null) {
BitmapRendererEventProducer eventProducer
= BitmapRendererEventProducer.Provider.get(
getUserAgent().getEventBroadcaster());
eventProducer.stoppingAfterFirstPageNoFilename(this);
} else {
try {
this.imageWriter.writeImage(
this.currentImage, out,
getSettings().getWriterParams());
} finally {
IOUtils.closeQuietly(out);
}
}
}
} else {
this.multiImageWriter.writeImage(this.currentImage,
getSettings().getWriterParams());
}
} catch (IOException ioe) {
throw new IFException("I/O error while encoding BufferedImage", ioe);
}
}
public void endPage() throws IFException {
this.currentPageDimensions = null;
}
public void handleExtensionObject(Object extension) throws IFException {
log.debug("Don't know how to handle extension object. Ignoring: "
+ extension + " (" + extension.getClass().getName() + ")");
}
}