From 9b49126cd645f4377d1bb56d5078a8d4a0cd491e Mon Sep 17 00:00:00 2001 From: Jeremias Maerki Date: Thu, 17 Sep 2009 09:58:48 +0000 Subject: [PATCH] Changed SVG output to produce coordinates in points rather than millipoints. Millipoint coordinates can become quite large and can cause trouble later. This is related to (but not fixing) Bugzilla #47641. git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@816116 13f79535-47bb-0310-9956-ffa450edef68 --- .../render/svg/EmbeddedSVGImageHandler.java | 20 ++--- .../fop/render/svg/SVGDocumentHandler.java | 7 +- .../org/apache/fop/render/svg/SVGPainter.java | 57 +++++++------- .../org/apache/fop/render/svg/SVGUtil.java | 78 +++++++++++++++++++ 4 files changed, 118 insertions(+), 44 deletions(-) create mode 100644 src/sandbox/org/apache/fop/render/svg/SVGUtil.java diff --git a/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java b/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java index b20982d54..fdcac6c35 100644 --- a/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java +++ b/src/sandbox/org/apache/fop/render/svg/EmbeddedSVGImageHandler.java @@ -90,12 +90,11 @@ public class EmbeddedSVGImageHandler implements ImageHandler, SVGConstants { ImageXMLDOM svg = (ImageXMLDOM)image; ContentHandler handler = svgContext.getContentHandler(); AttributesImpl atts = new AttributesImpl(); - atts.addAttribute("", "x", "x", CDATA, Integer.toString(pos.x)); - atts.addAttribute("", "y", "y", CDATA, Integer.toString(pos.y)); - atts.addAttribute("", "width", "width", CDATA, Integer.toString(pos.width)); - atts.addAttribute("", "height", "height", CDATA, Integer.toString(pos.height)); + atts.addAttribute("", "x", "x", CDATA, SVGUtil.formatMptToPt(pos.x)); + atts.addAttribute("", "y", "y", CDATA, SVGUtil.formatMptToPt(pos.y)); + atts.addAttribute("", "width", "width", CDATA, SVGUtil.formatMptToPt(pos.width)); + atts.addAttribute("", "height", "height", CDATA, SVGUtil.formatMptToPt(pos.height)); try { - //handler.startElement(NAMESPACE, "svg", "svg", atts); Document doc = (Document)svg.getDocument(); Element svgEl = (Element)doc.getDocumentElement(); @@ -127,10 +126,10 @@ public class EmbeddedSVGImageHandler implements ImageHandler, SVGConstants { && SVG_ELEMENT.getLocalName().equals(localName)) { topLevelSVGFound = true; AttributesImpl modAtts = new AttributesImpl(atts); - setAttribute(modAtts, "x", Integer.toString(pos.x)); - setAttribute(modAtts, "y", Integer.toString(pos.y)); - setAttribute(modAtts, "width", Integer.toString(pos.width)); - setAttribute(modAtts, "height", Integer.toString(pos.height)); + setAttribute(modAtts, "x", SVGUtil.formatMptToPt(pos.x)); + setAttribute(modAtts, "y", SVGUtil.formatMptToPt(pos.y)); + setAttribute(modAtts, "width", SVGUtil.formatMptToPt(pos.width)); + setAttribute(modAtts, "height", SVGUtil.formatMptToPt(pos.height)); super.startElement(uri, localName, name, modAtts); } else { super.startElement(uri, localName, name, atts); @@ -139,9 +138,6 @@ public class EmbeddedSVGImageHandler implements ImageHandler, SVGConstants { }); transformer.transform(src, res); - //handler.endElement(NAMESPACE, "svg", "svg"); - //} catch (SAXException e) { - //throw new IOException(e.getMessage()); } catch (TransformerException te) { throw new IOException(te.getMessage()); } diff --git a/src/sandbox/org/apache/fop/render/svg/SVGDocumentHandler.java b/src/sandbox/org/apache/fop/render/svg/SVGDocumentHandler.java index cc7957f1d..42ca17e08 100644 --- a/src/sandbox/org/apache/fop/render/svg/SVGDocumentHandler.java +++ b/src/sandbox/org/apache/fop/render/svg/SVGDocumentHandler.java @@ -175,10 +175,11 @@ public class SVGDocumentHandler extends AbstractSVGDocumentHandler { XMLUtil.addAttribute(atts, "index", Integer.toString(index)); XMLUtil.addAttribute(atts, "name", name); */ - XMLUtil.addAttribute(atts, "width", Float.toString(size.width / 1000f) + "pt"); - XMLUtil.addAttribute(atts, "height", Float.toString(size.height / 1000f) + "pt"); + XMLUtil.addAttribute(atts, "width", SVGUtil.formatMptToPt(size.width) + "pt"); + XMLUtil.addAttribute(atts, "height", SVGUtil.formatMptToPt(size.height) + "pt"); XMLUtil.addAttribute(atts, "viewBox", - "0 0 " + Integer.toString(size.width) + " " + Integer.toString(size.height)); + "0 0 " + SVGUtil.formatMptToPt(size.width) + + " " + SVGUtil.formatMptToPt(size.height)); handler.startElement("svg", atts); try { diff --git a/src/sandbox/org/apache/fop/render/svg/SVGPainter.java b/src/sandbox/org/apache/fop/render/svg/SVGPainter.java index 0ee13c021..32df7446f 100644 --- a/src/sandbox/org/apache/fop/render/svg/SVGPainter.java +++ b/src/sandbox/org/apache/fop/render/svg/SVGPainter.java @@ -52,7 +52,6 @@ import org.apache.fop.render.intermediate.IFConstants; import org.apache.fop.render.intermediate.IFContext; import org.apache.fop.render.intermediate.IFException; import org.apache.fop.render.intermediate.IFState; -import org.apache.fop.render.intermediate.IFUtil; import org.apache.fop.traits.BorderProps; import org.apache.fop.traits.RuleStyle; import org.apache.fop.util.ColorUtil; @@ -99,13 +98,13 @@ public class SVGPainter extends AbstractIFPainter implements SVGConstants { /** {@inheritDoc} */ public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect) throws IFException { - startViewport(IFUtil.toString(transform), size, clipRect); + startViewport(SVGUtil.formatAffineTransformMptToPt(transform), size, clipRect); } /** {@inheritDoc} */ public void startViewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect) throws IFException { - startViewport(IFUtil.toString(transforms), size, clipRect); + startViewport(SVGUtil.formatAffineTransformsMptToPt(transforms), size, clipRect); } private void startViewport(String transform, Dimension size, Rectangle clipRect) @@ -119,8 +118,8 @@ public class SVGPainter extends AbstractIFPainter implements SVGConstants { handler.startElement("g", atts); atts.clear(); - XMLUtil.addAttribute(atts, "width", Integer.toString(size.width)); - XMLUtil.addAttribute(atts, "height", Integer.toString(size.height)); + XMLUtil.addAttribute(atts, "width", SVGUtil.formatMptToPt(size.width)); + XMLUtil.addAttribute(atts, "height", SVGUtil.formatMptToPt(size.height)); if (clipRect != null) { int[] v = new int[] { clipRect.y, @@ -133,10 +132,10 @@ public class SVGPainter extends AbstractIFPainter implements SVGConstants { } if (sum != 0) { StringBuffer sb = new StringBuffer("rect("); - sb.append(v[0]).append(','); - sb.append(v[1]).append(','); - sb.append(v[2]).append(','); - sb.append(v[3]).append(')'); + sb.append(SVGUtil.formatMptToPt(v[0])).append(','); + sb.append(SVGUtil.formatMptToPt(v[1])).append(','); + sb.append(SVGUtil.formatMptToPt(v[2])).append(','); + sb.append(SVGUtil.formatMptToPt(v[3])).append(')'); XMLUtil.addAttribute(atts, "clip", sb.toString()); } XMLUtil.addAttribute(atts, "overflow", "hidden"); @@ -162,12 +161,12 @@ public class SVGPainter extends AbstractIFPainter implements SVGConstants { /** {@inheritDoc} */ public void startGroup(AffineTransform[] transforms) throws IFException { - startGroup(IFUtil.toString(transforms)); + startGroup(SVGUtil.formatAffineTransformsMptToPt(transforms)); } /** {@inheritDoc} */ public void startGroup(AffineTransform transform) throws IFException { - startGroup(IFUtil.toString(transform)); + startGroup(SVGUtil.formatAffineTransformMptToPt(transform)); } private void startGroup(String transform) throws IFException { @@ -216,10 +215,10 @@ public class SVGPainter extends AbstractIFPainter implements SVGConstants { //TODO Some additional URI rewriting might be necessary AttributesImpl atts = new AttributesImpl(); XMLUtil.addAttribute(atts, IFConstants.XLINK_HREF, uri); - XMLUtil.addAttribute(atts, "x", Integer.toString(rect.x)); - XMLUtil.addAttribute(atts, "y", Integer.toString(rect.y)); - XMLUtil.addAttribute(atts, "width", Integer.toString(rect.width)); - XMLUtil.addAttribute(atts, "height", Integer.toString(rect.height)); + XMLUtil.addAttribute(atts, "x", SVGUtil.formatMptToPt(rect.x)); + XMLUtil.addAttribute(atts, "y", SVGUtil.formatMptToPt(rect.y)); + XMLUtil.addAttribute(atts, "width", SVGUtil.formatMptToPt(rect.width)); + XMLUtil.addAttribute(atts, "height", SVGUtil.formatMptToPt(rect.height)); handler.element("image", atts); } else { drawImageUsingImageHandler(info, rect); @@ -282,10 +281,10 @@ public class SVGPainter extends AbstractIFPainter implements SVGConstants { try { establish(MODE_NORMAL); AttributesImpl atts = new AttributesImpl(); - XMLUtil.addAttribute(atts, "x", Integer.toString(rect.x)); - XMLUtil.addAttribute(atts, "y", Integer.toString(rect.y)); - XMLUtil.addAttribute(atts, "width", Integer.toString(rect.width)); - XMLUtil.addAttribute(atts, "height", Integer.toString(rect.height)); + XMLUtil.addAttribute(atts, "x", SVGUtil.formatMptToPt(rect.x)); + XMLUtil.addAttribute(atts, "y", SVGUtil.formatMptToPt(rect.y)); + XMLUtil.addAttribute(atts, "width", SVGUtil.formatMptToPt(rect.width)); + XMLUtil.addAttribute(atts, "height", SVGUtil.formatMptToPt(rect.height)); if (fill != null) { XMLUtil.addAttribute(atts, "fill", toString(fill)); } @@ -311,10 +310,10 @@ public class SVGPainter extends AbstractIFPainter implements SVGConstants { try { establish(MODE_NORMAL); AttributesImpl atts = new AttributesImpl(); - XMLUtil.addAttribute(atts, "x1", Integer.toString(start.x)); - XMLUtil.addAttribute(atts, "y1", Integer.toString(start.y)); - XMLUtil.addAttribute(atts, "x2", Integer.toString(end.x)); - XMLUtil.addAttribute(atts, "y2", Integer.toString(end.y)); + XMLUtil.addAttribute(atts, "x1", SVGUtil.formatMptToPt(start.x)); + XMLUtil.addAttribute(atts, "y1", SVGUtil.formatMptToPt(start.y)); + XMLUtil.addAttribute(atts, "x2", SVGUtil.formatMptToPt(end.x)); + XMLUtil.addAttribute(atts, "y2", SVGUtil.formatMptToPt(end.y)); XMLUtil.addAttribute(atts, "stroke-width", toString(color)); XMLUtil.addAttribute(atts, "fill", toString(color)); //TODO Handle style parameter @@ -331,16 +330,16 @@ public class SVGPainter extends AbstractIFPainter implements SVGConstants { establish(MODE_TEXT); AttributesImpl atts = new AttributesImpl(); XMLUtil.addAttribute(atts, XMLConstants.XML_SPACE, "preserve"); - XMLUtil.addAttribute(atts, "x", Integer.toString(x)); - XMLUtil.addAttribute(atts, "y", Integer.toString(y)); + XMLUtil.addAttribute(atts, "x", SVGUtil.formatMptToPt(x)); + XMLUtil.addAttribute(atts, "y", SVGUtil.formatMptToPt(y)); if (letterSpacing != 0) { - XMLUtil.addAttribute(atts, "letter-spacing", Integer.toString(letterSpacing)); + XMLUtil.addAttribute(atts, "letter-spacing", SVGUtil.formatMptToPt(letterSpacing)); } if (wordSpacing != 0) { - XMLUtil.addAttribute(atts, "word-spacing", Integer.toString(wordSpacing)); + XMLUtil.addAttribute(atts, "word-spacing", SVGUtil.formatMptToPt(wordSpacing)); } if (dx != null) { - XMLUtil.addAttribute(atts, "dx", IFUtil.toString(dx)); + XMLUtil.addAttribute(atts, "dx", SVGUtil.formatMptArrayToPt(dx)); } handler.startElement("text", atts); char[] chars = text.toCharArray(); @@ -385,7 +384,7 @@ public class SVGPainter extends AbstractIFPainter implements SVGConstants { XMLUtil.addAttribute(atts, "font-style", state.getFontStyle()); XMLUtil.addAttribute(atts, "font-weight", Integer.toString(state.getFontWeight())); XMLUtil.addAttribute(atts, "font-variant", state.getFontVariant()); - XMLUtil.addAttribute(atts, "font-size", Integer.toString(state.getFontSize())); + XMLUtil.addAttribute(atts, "font-size", SVGUtil.formatMptToPt(state.getFontSize())); XMLUtil.addAttribute(atts, "fill", toString(state.getTextColor())); handler.startElement("g", atts); state.resetFontChanged(); diff --git a/src/sandbox/org/apache/fop/render/svg/SVGUtil.java b/src/sandbox/org/apache/fop/render/svg/SVGUtil.java new file mode 100644 index 000000000..2953b0d5a --- /dev/null +++ b/src/sandbox/org/apache/fop/render/svg/SVGUtil.java @@ -0,0 +1,78 @@ +/* + * 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.svg; + +import java.awt.geom.AffineTransform; + +import org.apache.fop.render.intermediate.IFUtil; + +/** + * This class provides utility methods for generating SVG. + */ +public class SVGUtil { + + /** + * Formats a length in millipoints as a point value. + * @param mpt the length in millipoints + * @return the formatted value in points + */ + public static String formatMptToPt(int mpt) { + return Float.toString(mpt / 1000f); + } + + /** + * Formats an array of lengths in millipoints as point values. + * @param lengths the lengths in millipoints + * @return the formatted array in points + */ + public static String formatMptArrayToPt(int[] lengths) { + return IFUtil.toString(lengths); + } + + /** + * Formats a transformation matrix in millipoints with values as points. + * @param transform the transformation matrix in millipoints + * @return the formatted matrix in points + */ + public static String formatAffineTransformMptToPt(AffineTransform transform) { + AffineTransform scaled = new AffineTransform(transform); + scaled.setToTranslation( + transform.getTranslateX() / 1000, + transform.getTranslateY() / 1000); + return IFUtil.toString(scaled); + } + + /** + * Formats an array of transformation matrices in millipoints with values as points. + * @param transforms the transformation matrices in millipoints + * @return the formatted matrices in points + */ + public static String formatAffineTransformsMptToPt(AffineTransform[] transforms) { + StringBuffer sb = new StringBuffer(); + for (int i = 0, c = transforms.length; i < c; i++) { + if (i > 0) { + sb.append(' '); + } + sb.append(formatAffineTransformMptToPt(transforms[i])); + } + return sb.toString(); + } + +} -- 2.39.5