Browse Source

Added a DocumentHandler implementation for PNG output including multi-file output support.

git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@820637 13f79535-47bb-0310-9956-ffa450edef68
tags/fop-1_0
Jeremias Maerki 14 years ago
parent
commit
5ad3308501

+ 1
- 0
src/java/META-INF/services/org.apache.fop.render.intermediate.IFDocumentHandler View File

@@ -1,6 +1,7 @@
org.apache.fop.render.pdf.PDFDocumentHandlerMaker
org.apache.fop.render.pcl.PCLDocumentHandlerMaker
org.apache.fop.render.bitmap.TIFFDocumentHandlerMaker
org.apache.fop.render.bitmap.PNGDocumentHandlerMaker
org.apache.fop.render.ps.PSDocumentHandlerMaker
org.apache.fop.render.afp.AFPDocumentHandlerMaker
org.apache.fop.render.intermediate.IFSerializerMaker

+ 279
- 0
src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java View File

@@ -0,0 +1,279 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.bitmap;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

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.FopFactoryConfigurator;
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;

/**
* Abstract {@code IFDocumentHandler} implementation for producing bitmap images.
*/
public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritingIFDocumentHandler {

/** logging instance */
private static Log log = LogFactory.getLog(AbstractBitmapDocumentHandler.class);

private ImageWriter imageWriter;
private MultiImageWriter multiImageWriter;

/** Helper class for generating multiple files */
private MultiFileRenderingUtil multiFileUtil;

private int pageCount;
private Dimension currentPageDimensions;
private BufferedImage currentImage;

private BitmapRenderingSettings bitmapSettings = new BitmapRenderingSettings();

private double scaleFactor = 1.0;

/**
* Default constructor.
*/
public AbstractBitmapDocumentHandler() {
}

/** {@inheritDoc} */
public boolean supportsPagesOutOfOrder() {
return false;
}

/** {@inheritDoc} */
public abstract String getMimeType();

/**
* Returns the default file extension for the supported image type.
* @return the default file extension (ex. "png")
*/
public abstract String getDefaultExtension();

/** {@inheritDoc} */
public void setContext(IFContext context) {
super.setContext(context);

//Set target resolution
int dpi = Math.round(context.getUserAgent().getTargetResolution());
getSettings().getWriterParams().setResolution(dpi);
}

/** {@inheritDoc} */
public abstract IFDocumentHandlerConfigurator getConfigurator();

/**
* Returns the settings for bitmap rendering.
* @return the settings object
*/
public BitmapRenderingSettings getSettings() {
return this.bitmapSettings;
}

/** {@inheritDoc} */
public void setDefaultFontInfo(FontInfo fontInfo) {
FontInfo fi = Java2DUtil.buildDefaultJava2DBasedFontInfo(fontInfo, getUserAgent());
setFontInfo(fi);
}

//----------------------------------------------------------------------------------------------

/** {@inheritDoc} */
public void startDocument() throws IFException {
super.startDocument();
try {
// Creates writer
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);
}
}

/** {@inheritDoc} */
public void endDocumentHeader() throws IFException {
}

/** {@inheritDoc} */
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();
}

/** {@inheritDoc} */
public void startPageSequence(String id) throws IFException {
//nop
}

/** {@inheritDoc} */
public void endPageSequence() throws IFException {
//nop
}

/** {@inheritDoc} */
public void startPage(int index, String name, String pageMasterName, Dimension size)
throws IFException {
this.pageCount++;
this.currentPageDimensions = new Dimension(size);
}

/** {@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);
this.currentImage = createBufferedImage(bitmapWidth, bitmapHeight);
Graphics2D graphics2D = this.currentImage.createGraphics();
// draw page background
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);
graphics2D.scale(scale / 1000f, scale / 1000f);

return new Java2DPainter(graphics2D, getContext(), getFontInfo());
}

/**
* Creates a new BufferedImage.
* @param bitmapWidth the desired width in pixels
* @param bitmapHeight the desired height in pixels
* @return the new BufferedImage instance
*/
protected BufferedImage createBufferedImage(int bitmapWidth, int bitmapHeight) {
return new BufferedImage(bitmapWidth, bitmapHeight, getSettings().getBufferedImageType());
}

/** {@inheritDoc} */
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);
}
try {
this.imageWriter.writeImage(
this.currentImage, out,
getSettings().getWriterParams());
} finally {
IOUtils.closeQuietly(out);
}
}
} else {
this.multiImageWriter.writeImage(this.currentImage,
getSettings().getWriterParams());
}
this.currentImage = null;
} catch (IOException ioe) {
throw new IFException("I/O error while encoding BufferedImage", ioe);
}
}

/** {@inheritDoc} */
public void endPage() throws IFException {
this.currentPageDimensions = null;
}

/** {@inheritDoc} */
public void handleExtensionObject(Object extension) throws IFException {
log.debug("Don't know how to handle extension object. Ignoring: "
+ extension + " (" + extension.getClass().getName() + ")");
}

}

+ 142
- 0
src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java View File

@@ -0,0 +1,142 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.bitmap;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.List;

import org.apache.avalon.framework.configuration.Configuration;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.fonts.FontCollection;
import org.apache.fop.fonts.FontEventAdapter;
import org.apache.fop.fonts.FontEventListener;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontManager;
import org.apache.fop.fonts.FontResolver;
import org.apache.fop.render.DefaultFontResolver;
import org.apache.fop.render.intermediate.IFDocumentHandler;
import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
import org.apache.fop.render.java2d.Base14FontCollection;
import org.apache.fop.render.java2d.ConfiguredFontCollection;
import org.apache.fop.render.java2d.InstalledFontCollection;
import org.apache.fop.render.java2d.Java2DFontMetrics;
import org.apache.fop.render.java2d.Java2DRenderer;
import org.apache.fop.render.java2d.Java2DRendererConfigurator;
import org.apache.fop.util.ColorUtil;

/**
* Configurator for bitmap output.
*/
public class BitmapRendererConfigurator extends Java2DRendererConfigurator
implements IFDocumentHandlerConfigurator {

/**
* Default constructor
* @param userAgent user agent
*/
public BitmapRendererConfigurator(FOUserAgent userAgent) {
super(userAgent);
}

// ---=== IFDocumentHandler configuration ===---

/** {@inheritDoc} */
public void configure(IFDocumentHandler documentHandler) throws FOPException {
super.configure(documentHandler);
Configuration cfg = super.getRendererConfig(documentHandler.getMimeType());
if (cfg != null) {
AbstractBitmapDocumentHandler bitmapHandler
= (AbstractBitmapDocumentHandler)documentHandler;
BitmapRenderingSettings settings = bitmapHandler.getSettings();

boolean transparent = cfg.getChild(
Java2DRenderer.JAVA2D_TRANSPARENT_PAGE_BACKGROUND).getValueAsBoolean(
settings.hasTransparentPageBackground());
if (transparent) {
settings.setPageBackgroundColor(null);
} else {
String background = cfg.getChild("background-color").getValue(null);
if (background != null) {
settings.setPageBackgroundColor(
ColorUtil.parseColorString(this.userAgent, background));
} else {
settings.setPageBackgroundColor(Color.WHITE);
}
}

boolean antiAliasing = cfg.getChild("anti-aliasing").getValueAsBoolean(
settings.isAntiAliasingEnabled());
settings.setAntiAliasing(antiAliasing);

String optimization = cfg.getChild("rendering").getValue(null);
if ("quality".equalsIgnoreCase(optimization)) {
settings.setQualityRendering(true);
} else if ("speed".equalsIgnoreCase(optimization)) {
settings.setQualityRendering(false);
}

String color = cfg.getChild("color-mode").getValue(null);
if (color != null) {
if ("rgba".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_INT_ARGB);
} else if ("rgb".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_INT_RGB);
} else if ("gray".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_BYTE_GRAY);
} else if ("binary".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
} else {
throw new FOPException("Invalid value for color-mode: " + color);
}
}
}
}

/** {@inheritDoc} */
public void setupFontInfo(IFDocumentHandler documentHandler, FontInfo fontInfo)
throws FOPException {
FontManager fontManager = userAgent.getFactory().getFontManager();

Graphics2D graphics2D = Java2DFontMetrics.createFontMetricsGraphics2D();

List fontCollections = new java.util.ArrayList();
fontCollections.add(new Base14FontCollection(graphics2D));
fontCollections.add(new InstalledFontCollection(graphics2D));

Configuration cfg = super.getRendererConfig(documentHandler.getMimeType());
if (cfg != null) {
FontResolver fontResolver = new DefaultFontResolver(userAgent);
FontEventListener listener = new FontEventAdapter(
userAgent.getEventBroadcaster());
List fontList = buildFontList(cfg, fontResolver, listener);
fontCollections.add(new ConfiguredFontCollection(fontResolver, fontList));
}

fontManager.setup(fontInfo,
(FontCollection[])fontCollections.toArray(
new FontCollection[fontCollections.size()]));
documentHandler.setFontInfo(fontInfo);
}

}

+ 0
- 1
src/java/org/apache/fop/render/bitmap/BitmapRenderingSettings.java View File

@@ -47,7 +47,6 @@ public class BitmapRenderingSettings extends Java2DRenderingSettings implements
*/
public BitmapRenderingSettings() {
writerParams = new ImageWriterParams();
writerParams.setCompressionMethod(COMPRESSION_PACKBITS);
}

/**

+ 25
- 0
src/java/org/apache/fop/render/bitmap/MultiFileRenderingUtil.java View File

@@ -1,3 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.bitmap;

import java.io.BufferedOutputStream;
@@ -61,6 +80,12 @@ public class MultiFileRenderingUtil {
}
}

/**
* Creates a new {@link OutputStream} for the given page number.
* @param pageNumber the page number (zero-based)
* @return the output stream for the page
* @throws IOException if there's an I/O error while setting up the output stream
*/
public OutputStream createOutputStream(int pageNumber) throws IOException {
if (filePrefix == null) {
return null;

+ 45
- 0
src/java/org/apache/fop/render/bitmap/PNGDocumentHandler.java View File

@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.bitmap;

import org.apache.fop.apps.MimeConstants;
import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;

/**
* {@code IFDocumentHandler} implementation that produces PNG files.
*/
public class PNGDocumentHandler extends AbstractBitmapDocumentHandler {

/** {@inheritDoc} */
public String getMimeType() {
return MimeConstants.MIME_PNG;
}

/** {@inheritDoc} */
public String getDefaultExtension() {
return "png";
}

/** {@inheritDoc} */
public IFDocumentHandlerConfigurator getConfigurator() {
return new BitmapRendererConfigurator(getUserAgent());
}

}

+ 52
- 0
src/java/org/apache/fop/render/bitmap/PNGDocumentHandlerMaker.java View File

@@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.bitmap;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.render.intermediate.AbstractIFDocumentHandlerMaker;
import org.apache.fop.render.intermediate.IFContext;
import org.apache.fop.render.intermediate.IFDocumentHandler;

/**
* Document handler factory for PNG output.
*/
public class PNGDocumentHandlerMaker extends AbstractIFDocumentHandlerMaker {

private static final String[] MIMES = new String[] {MimeConstants.MIME_PNG};

/** {@inheritDoc} */
public IFDocumentHandler makeIFDocumentHandler(FOUserAgent ua) {
PNGDocumentHandler handler = new PNGDocumentHandler();
handler.setContext(new IFContext(ua));
return handler;
}

/** {@inheritDoc} */
public boolean needsOutputStream() {
return true;
}

/** {@inheritDoc} */
public String[] getSupportedMimeTypes() {
return MIMES;
}

}

+ 4
- 225
src/java/org/apache/fop/render/bitmap/TIFFDocumentHandler.java View File

@@ -19,60 +19,13 @@

package org.apache.fop.render.bitmap;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;

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.FopFactoryConfigurator;
import org.apache.fop.apps.MimeConstants;
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;

/**
* {@code IFDocumentHandler} implementation that produces PCL 5.
* {@code IFDocumentHandler} implementation that produces TIFF files.
*/
public class TIFFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler
implements TIFFConstants {

/** logging instance */
private static Log log = LogFactory.getLog(TIFFDocumentHandler.class);

private ImageWriter imageWriter;
private MultiImageWriter multiImageWriter;

private int pageCount;
private Dimension currentPageDimensions;
private BufferedImage currentImage;

private BitmapRenderingSettings bitmapSettings = new BitmapRenderingSettings();

private double scaleFactor = 1.0;

/**
* Default constructor.
*/
public TIFFDocumentHandler() {
}

/** {@inheritDoc} */
public boolean supportsPagesOutOfOrder() {
return false;
}
public class TIFFDocumentHandler extends AbstractBitmapDocumentHandler {

/** {@inheritDoc} */
public String getMimeType() {
@@ -80,12 +33,8 @@ public class TIFFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler
}

/** {@inheritDoc} */
public void setContext(IFContext context) {
super.setContext(context);

//Set target resolution
int dpi = Math.round(context.getUserAgent().getTargetResolution());
getSettings().getWriterParams().setResolution(dpi);
public String getDefaultExtension() {
return "tif";
}

/** {@inheritDoc} */
@@ -93,174 +42,4 @@ public class TIFFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler
return new TIFFRendererConfigurator(getUserAgent());
}

/**
* Returns the settings for bitmap rendering.
* @return the settings object
*/
public BitmapRenderingSettings getSettings() {
return this.bitmapSettings;
}

/** {@inheritDoc} */
public void setDefaultFontInfo(FontInfo fontInfo) {
FontInfo fi = Java2DUtil.buildDefaultJava2DBasedFontInfo(fontInfo, getUserAgent());
setFontInfo(fi);
}

//----------------------------------------------------------------------------------------------

/** {@inheritDoc} */
public void startDocument() throws IFException {
super.startDocument();
try {
// Creates writer
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);
}
this.pageCount = 0;
} catch (IOException e) {
throw new IFException("I/O error in startDocument()", e);
}
}

/** {@inheritDoc} */
public void endDocumentHeader() throws IFException {
}

/** {@inheritDoc} */
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();
}

/** {@inheritDoc} */
public void startPageSequence(String id) throws IFException {
//nop
}

/** {@inheritDoc} */
public void endPageSequence() throws IFException {
//nop
}

/** {@inheritDoc} */
public void startPage(int index, String name, String pageMasterName, Dimension size)
throws IFException {
this.pageCount++;
this.currentPageDimensions = new Dimension(size);
}

/** {@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);
this.currentImage = createBufferedImage(bitmapWidth, bitmapHeight);
Graphics2D graphics2D = this.currentImage.createGraphics();
// draw page background
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);
graphics2D.scale(scale / 1000f, scale / 1000f);

return new Java2DPainter(graphics2D, getContext(), getFontInfo());
}

/**
* Creates a new BufferedImage.
* @param bitmapWidth the desired width in pixels
* @param bitmapHeight the desired height in pixels
* @return the new BufferedImage instance
*/
protected BufferedImage createBufferedImage(int bitmapWidth, int bitmapHeight) {
return new BufferedImage(bitmapWidth, bitmapHeight, getSettings().getBufferedImageType());
}

/** {@inheritDoc} */
public void endPageContent() throws IFException {
try {
if (this.multiImageWriter == null) {
switch (this.pageCount) {
case 1:
this.imageWriter.writeImage(
this.currentImage, this.outputStream,
getSettings().getWriterParams());
break;
case 2:
BitmapRendererEventProducer eventProducer
= BitmapRendererEventProducer.Provider.get(
getUserAgent().getEventBroadcaster());
eventProducer.stoppingAfterFirstPageNoFilename(this);
break;
default:
//ignore
}
} else {
this.multiImageWriter.writeImage(this.currentImage,
getSettings().getWriterParams());
}
this.currentImage = null;
} catch (IOException ioe) {
throw new IFException("I/O error while encoding BufferedImage", ioe);
}
}

/** {@inheritDoc} */
public void endPage() throws IFException {
this.currentPageDimensions = null;
}

/** {@inheritDoc} */
public void handleExtensionObject(Object extension) throws IFException {
if (false) {
//TODO Handle extensions
} else {
log.debug("Don't know how to handle extension object. Ignoring: "
+ extension + " (" + extension.getClass().getName() + ")");
}
}

}

+ 8
- 88
src/java/org/apache/fop/render/bitmap/TIFFRendererConfigurator.java View File

@@ -19,38 +19,19 @@

package org.apache.fop.render.bitmap;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.List;

import org.apache.avalon.framework.configuration.Configuration;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.fonts.FontCollection;
import org.apache.fop.fonts.FontEventAdapter;
import org.apache.fop.fonts.FontEventListener;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontManager;
import org.apache.fop.fonts.FontResolver;
import org.apache.fop.render.DefaultFontResolver;
import org.apache.fop.render.Renderer;
import org.apache.fop.render.intermediate.IFDocumentHandler;
import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator;
import org.apache.fop.render.java2d.Base14FontCollection;
import org.apache.fop.render.java2d.ConfiguredFontCollection;
import org.apache.fop.render.java2d.InstalledFontCollection;
import org.apache.fop.render.java2d.Java2DFontMetrics;
import org.apache.fop.render.java2d.Java2DRenderer;
import org.apache.fop.render.java2d.Java2DRendererConfigurator;
import org.apache.fop.util.ColorUtil;

/**
* TIFF Renderer configurator
*/
public class TIFFRendererConfigurator extends Java2DRendererConfigurator
implements IFDocumentHandlerConfigurator {
public class TIFFRendererConfigurator extends BitmapRendererConfigurator {

/**
* Default constructor
@@ -85,6 +66,12 @@ public class TIFFRendererConfigurator extends Java2DRendererConfigurator
super.configure(renderer);
}

/**
* Determines the type value for the BufferedImage to be produced for rendering
* the bitmap image.
* @param compressionName the compression name
* @return a value from the {@link BufferedImage}.TYPE_* constants
*/
private int getBufferedImageTypeFor(String compressionName) {
if (compressionName.equalsIgnoreCase(TIFFConstants.COMPRESSION_CCITT_T6)) {
return BufferedImage.TYPE_BYTE_BINARY;
@@ -99,6 +86,7 @@ public class TIFFRendererConfigurator extends Java2DRendererConfigurator

/** {@inheritDoc} */
public void configure(IFDocumentHandler documentHandler) throws FOPException {
super.configure(documentHandler);
Configuration cfg = super.getRendererConfig(documentHandler.getMimeType());
if (cfg != null) {
TIFFDocumentHandler tiffHandler = (TIFFDocumentHandler)documentHandler;
@@ -113,75 +101,7 @@ public class TIFFRendererConfigurator extends Java2DRendererConfigurator
if (log.isInfoEnabled()) {
log.info("TIFF compression set to " + name);
}

boolean transparent = cfg.getChild(
Java2DRenderer.JAVA2D_TRANSPARENT_PAGE_BACKGROUND).getValueAsBoolean(
settings.hasTransparentPageBackground());
if (transparent) {
settings.setPageBackgroundColor(null);
} else {
String background = cfg.getChild("background-color").getValue(null);
if (background != null) {
settings.setPageBackgroundColor(
ColorUtil.parseColorString(this.userAgent, background));
} else {
settings.setPageBackgroundColor(Color.WHITE);
}
}

boolean antiAliasing = cfg.getChild("anti-aliasing").getValueAsBoolean(
settings.isAntiAliasingEnabled());
settings.setAntiAliasing(antiAliasing);

String optimization = cfg.getChild("rendering").getValue(null);
if ("quality".equalsIgnoreCase(optimization)) {
settings.setQualityRendering(true);
} else if ("speed".equalsIgnoreCase(optimization)) {
settings.setQualityRendering(false);
}

String color = cfg.getChild("color-mode").getValue(null);
if (color != null) {
if ("rgba".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_INT_ARGB);
} else if ("rgb".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_INT_RGB);
} else if ("gray".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_BYTE_GRAY);
} else if ("binary".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
} else {
throw new FOPException("Invalid value for color-mode: " + color);
}
}
}
}

/** {@inheritDoc} */
public void setupFontInfo(IFDocumentHandler documentHandler, FontInfo fontInfo)
throws FOPException {
FontManager fontManager = userAgent.getFactory().getFontManager();

Graphics2D graphics2D = Java2DFontMetrics.createFontMetricsGraphics2D();

List fontCollections = new java.util.ArrayList();
fontCollections.add(new Base14FontCollection(graphics2D));
fontCollections.add(new InstalledFontCollection(graphics2D));

Configuration cfg = super.getRendererConfig(documentHandler.getMimeType());
if (cfg != null) {
FontResolver fontResolver = new DefaultFontResolver(userAgent);
FontEventListener listener = new FontEventAdapter(
userAgent.getEventBroadcaster());
List fontList = buildFontList(cfg, fontResolver, listener);
fontCollections.add(new ConfiguredFontCollection(fontResolver, fontList));
}

fontManager.setup(fontInfo,
(FontCollection[])fontCollections.toArray(
new FontCollection[fontCollections.size()]));
documentHandler.setFontInfo(fontInfo);
}


}

+ 5
- 3
src/sandbox/org/apache/fop/render/svg/SVGDocumentHandler.java View File

@@ -45,6 +45,7 @@ import org.xml.sax.helpers.AttributesImpl;

import org.apache.commons.io.IOUtils;

import org.apache.fop.render.bitmap.BitmapRendererEventProducer;
import org.apache.fop.render.bitmap.MultiFileRenderingUtil;
import org.apache.fop.render.intermediate.DelegatingFragmentContentHandler;
import org.apache.fop.render.intermediate.IFException;
@@ -209,9 +210,10 @@ public class SVGDocumentHandler extends AbstractSVGDocumentHandler {
} else {
out = this.multiFileUtil.createOutputStream(index);
if (out == null) {
//TODO Convert to event
throw new IFException(
"No filename information available. Stopping after first page.", null);
BitmapRendererEventProducer eventProducer
= BitmapRendererEventProducer.Provider.get(
getUserAgent().getEventBroadcaster());
eventProducer.stoppingAfterFirstPageNoFilename(this);
}
}
} catch (IOException ioe) {

Loading…
Cancel
Save