diff options
author | Jeremias Maerki <jeremias@apache.org> | 2006-06-29 10:13:08 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2006-06-29 10:13:08 +0000 |
commit | 4ca46cc8d528b98fd4870d50789b7080a97edbc5 (patch) | |
tree | 6de60ff017a341daa42f3d89596f072b530450ab /src/sandbox | |
parent | 9a3b5017d5c055351f76830b5f4aa98175027b24 (diff) | |
download | xmlgraphics-fop-4ca46cc8d528b98fd4870d50789b7080a97edbc5.tar.gz xmlgraphics-fop-4ca46cc8d528b98fd4870d50789b7080a97edbc5.zip |
Re-routing SVG painting to the Graphics2DAdapter and teach the latter to respect the renderer's colorImages setting.
Removed obsolete SVGConverter.
This fixes painting SVG at a hard-coded resolution. Now uses the target resolution from the user agent.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@417984 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/sandbox')
4 files changed, 75 insertions, 147 deletions
diff --git a/src/sandbox/org/apache/fop/render/afp/AFPGraphics2DAdapter.java b/src/sandbox/org/apache/fop/render/afp/AFPGraphics2DAdapter.java index 3fc558d76..5b1611ddc 100644 --- a/src/sandbox/org/apache/fop/render/afp/AFPGraphics2DAdapter.java +++ b/src/sandbox/org/apache/fop/render/afp/AFPGraphics2DAdapter.java @@ -43,10 +43,12 @@ public class AFPGraphics2DAdapter extends AbstractGraphics2DAdapter { RendererContext.RendererContextWrapper wrappedContext = new RendererContext.RendererContextWrapper(context); AFPRenderer afp = (AFPRenderer)context.getRenderer(); + Boolean grayObj = (Boolean)context.getProperty(AFPRendererContextConstants.AFP_GRAYSCALE); + boolean gray = (grayObj != null ? grayObj.booleanValue() : false); //Paint to a BufferedImage int resolution = (int)Math.round(context.getUserAgent().getTargetResolution()); - BufferedImage bi = paintToBufferedImage(painter, wrappedContext, resolution, false, false); + BufferedImage bi = paintToBufferedImage(painter, wrappedContext, resolution, gray, false); afp.drawBufferedImage(bi, resolution, x, y, width, height); } diff --git a/src/sandbox/org/apache/fop/render/afp/AFPRenderer.java b/src/sandbox/org/apache/fop/render/afp/AFPRenderer.java index 91f235cb1..7afab78a3 100644 --- a/src/sandbox/org/apache/fop/render/afp/AFPRenderer.java +++ b/src/sandbox/org/apache/fop/render/afp/AFPRenderer.java @@ -67,6 +67,7 @@ import org.apache.fop.image.TIFFImage; import org.apache.fop.image.XMLImage; import org.apache.fop.render.AbstractPathOrientedRenderer; import org.apache.fop.render.Graphics2DAdapter; +import org.apache.fop.render.RendererContext; import org.apache.fop.render.afp.extensions.AFPElementMapping; import org.apache.fop.render.afp.extensions.AFPPageSetup; import org.apache.fop.render.afp.fonts.AFPFontInfo; @@ -1054,6 +1055,18 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { } /** + * @see org.apache.fop.render.PrintRenderer#createRendererContext( + * int, int, int, int, java.util.Map) + */ + protected RendererContext createRendererContext(int x, int y, int width, int height, Map foreignAttributes) { + RendererContext context; + context = super.createRendererContext(x, y, width, height, foreignAttributes); + context.setProperty(AFPRendererContextConstants.AFP_GRAYSCALE, + new Boolean(!this.colorImages)); + return context; + } + + /** * Draw an image at the indicated location. * @see org.apache.fop.render.AbstractRenderer#drawImage(String, Rectangle2D, Map) */ @@ -1077,7 +1090,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { return; } String mime = fopimage.getMimeType(); - if ("text/xml".equals(mime)) { + if ("text/xml".equals(mime) || MimeConstants.MIME_SVG.equals(mime)) { if (!fopimage.load(FopImage.ORIGINAL_DATA)) { return; } @@ -1085,27 +1098,6 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { String ns = ((XMLImage) fopimage).getNameSpace(); renderDocument(doc, ns, pos, foreignAttributes); - } else if (MimeConstants.MIME_SVG.equals(mime)) { - if (!fopimage.load(FopImage.ORIGINAL_DATA)) { - return; - } - int x = mpts2units(pos.getX() + currentIPPosition); - int y = mpts2units(pos.getY() + currentBPPosition); - int w = mpts2units(pos.getWidth()); - int h = mpts2units(pos.getHeight()); - ImageObject io = _afpDataStream.getImageObject(x, y, w, h); - io.setImageParameters( - (int)(fopimage.getHorizontalResolution() * 10), - (int)(fopimage.getVerticalResolution() * 10), - fopimage.getWidth(), - fopimage.getHeight() - ); - if (colorImages) { - io.setImageIDESize((byte)24); - io.setImageData(SVGConverter.convertToTIFF((XMLImage)fopimage)); - } else { - convertToGrayScaleImage(io, SVGConverter.convertToTIFF((XMLImage)fopimage)); - } } else if (MimeConstants.MIME_EPS.equals(mime)) { log.warn("EPS images are not supported by this renderer"); /* @@ -1199,6 +1191,29 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { } /** + * Writes a BufferedImage to an OutputStream as raw sRGB bitmaps. + * @param img the BufferedImage + * @param out the OutputStream + * @throws IOException In case of an I/O error. + */ + public static void writeImage(BufferedImage img, OutputStream out) throws IOException { + int w = img.getWidth(); + int h = img.getHeight(); + int[] tmpMap = img.getRGB(0, 0, w, h, null, 0, w); + for (int i = 0; i < h; i++) { + for (int j = 0; j < w; j++) { + int p = tmpMap[i * w + j]; + int r = (p >> 16) & 0xFF; + int g = (p >> 8) & 0xFF; + int b = (p) & 0xFF; + out.write((byte)(r & 0xFF)); + out.write((byte)(g & 0xFF)); + out.write((byte)(b & 0xFF)); + } + } + } + + /** * Draws a BufferedImage to AFP. * @param bi the BufferedImage * @param resolution the resolution of the BufferedImage @@ -1215,7 +1230,7 @@ public class AFPRenderer extends AbstractPathOrientedRenderer { ByteArrayOutputStream baout = new ByteArrayOutputStream(); try { //Serialize image - SVGConverter.writeImage(bi, baout); + writeImage(bi, baout); byte[] buf = baout.toByteArray(); //Generate image diff --git a/src/sandbox/org/apache/fop/render/afp/AFPRendererContextConstants.java b/src/sandbox/org/apache/fop/render/afp/AFPRendererContextConstants.java new file mode 100644 index 000000000..2d10f71e6 --- /dev/null +++ b/src/sandbox/org/apache/fop/render/afp/AFPRendererContextConstants.java @@ -0,0 +1,34 @@ +/*
+ * 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.afp;
+
+import org.apache.fop.render.RendererContextConstants;
+
+/**
+ * Defines a number of standard constants (keys) for use by the RendererContext class.
+ */
+public interface AFPRendererContextConstants extends RendererContextConstants {
+
+ /**
+ * Key for a Boolean value that enables grayscale processing instead of color
+ * processing.
+ */
+ String AFP_GRAYSCALE = "afpGrayscale";
+
+}
diff --git a/src/sandbox/org/apache/fop/render/afp/SVGConverter.java b/src/sandbox/org/apache/fop/render/afp/SVGConverter.java deleted file mode 100644 index 0adc32a53..000000000 --- a/src/sandbox/org/apache/fop/render/afp/SVGConverter.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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.afp; - -import java.awt.image.BufferedImage; -import java.io.IOException; -import java.io.OutputStream; -import org.apache.batik.transcoder.TranscoderException; -import org.apache.batik.transcoder.TranscoderInput; -import org.apache.batik.transcoder.TranscoderOutput; -import org.apache.batik.transcoder.image.ImageTranscoder; -import org.apache.batik.transcoder.image.JPEGTranscoder; -import org.apache.batik.transcoder.image.TIFFTranscoder; -import org.apache.commons.io.output.ByteArrayOutputStream; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.fop.image.XMLImage; - -/** - * Helper class for converting SVG to bitmap images. - */ -public class SVGConverter extends ImageTranscoder { - - public void writeImage(BufferedImage img, TranscoderOutput output) { - OutputStream os = output.getOutputStream(); - try { - writeImage(img, os); - } catch (IOException ioe) { - //ignore - //TODO Handle IOException properly! - } - } - - /** - * Writes a BufferedImage to an OutputStream as raw sRGB bitmaps. - * @param img the BufferedImage - * @param out the OutputStream - * @throws IOException In case of an I/O error. - */ - public static void writeImage(BufferedImage img, OutputStream out) throws IOException { - int w = img.getWidth(); - int h = img.getHeight(); - int[] tmpMap = img.getRGB(0, 0, w, h, null, 0, w); - for (int i = 0; i < h; i++) { - for (int j = 0; j < w; j++) { - int p = tmpMap[i * w + j]; - int r = (p >> 16) & 0xFF; - int g = (p >> 8) & 0xFF; - int b = (p) & 0xFF; - out.write((byte)(r & 0xFF)); - out.write((byte)(g & 0xFF)); - out.write((byte)(b & 0xFF)); - } - } - } - - public BufferedImage createImage(int width, int height) { - return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - } - - /** logger instance */ - private static Log log = LogFactory.getLog(SVGConverter.class); - - /** - * Converts a SVG image to a TIFF bitmap. - * @param image the SVG image - * @return a byte array containing the TIFF image - * @todo Please rename! The method name is misleading. - */ - public static byte[] convertToTIFF(XMLImage image) { - // TIFFTranscoder transcoder = new TIFFTranscoder(); - // JPEGTranscoder transcoder = new JPEGTranscoder(); - SVGConverter transcoder = new SVGConverter(); - transcoder.addTranscodingHint(ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, - new Float(25.4f / 72)); //300dpi should be enough for now. - transcoder.addTranscodingHint(ImageTranscoder.KEY_WIDTH, new Float((float)image.getWidth())); - transcoder.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, new Float((float)image.getHeight())); - TranscoderInput input = new TranscoderInput(image.getDocument()); - ByteArrayOutputStream baout = new ByteArrayOutputStream(image.getWidth() * image.getHeight() * 3); - TranscoderOutput output = new TranscoderOutput(baout); - try { - transcoder.transcode(input, output); - return baout.toByteArray(); - } catch (TranscoderException e) { - log.error(e); - return null; - } - } - - public static void main(String args[]) { - // TIFFTranscoder transcoder = new TIFFTranscoder(); - // JPEGTranscoder transcoder = new JPEGTranscoder(); - SVGConverter transcoder = new SVGConverter(); - transcoder.addTranscodingHint(ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, - new Float(25.4f / 300)); //300dpi should be enough for now. - TranscoderInput input = new TranscoderInput("file:///home/mm/fop-trunk/test/resources/images/img.svg"); - try { - java.io.FileOutputStream out = new java.io.FileOutputStream("/home/mm/fop-trunk/img.raw");; - TranscoderOutput output = new TranscoderOutput(out); - transcoder.transcode(input, output); - } catch (Exception e) { - log.error(e); - } - - } - -} |