From 40af8ffb2a374456d335f96770bba02fc17ae0d8 Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Wed, 12 Aug 2009 10:46:39 +0000 Subject: [PATCH] Renamed PageScaleAttributes class into PageScale. Simplified and improved its behaviour: - return null for an empty string - allow for an arbitrary sequence of white spaces between the scales - check that number of arguments is no more than 2 - other small improvements git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@803440 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/render/awt/AWTRenderer.java | 6 +-- ...ageScaleAttributes.java => PageScale.java} | 46 ++++++++++++------- .../fop/render/java2d/Java2DRenderer.java | 6 +-- .../fop/render/pdf/PDFDocumentHandler.java | 6 +-- .../extensions/prepress/PageScaleTest.java | 10 ++-- 5 files changed, 44 insertions(+), 30 deletions(-) rename src/java/org/apache/fop/render/extensions/prepress/{PageScaleAttributes.java => PageScale.java} (66%) diff --git a/src/java/org/apache/fop/render/awt/AWTRenderer.java b/src/java/org/apache/fop/render/awt/AWTRenderer.java index 170df3f4d..b50708112 100644 --- a/src/java/org/apache/fop/render/awt/AWTRenderer.java +++ b/src/java/org/apache/fop/render/awt/AWTRenderer.java @@ -47,7 +47,7 @@ import org.apache.fop.render.awt.viewer.PreviewDialog; import org.apache.fop.render.awt.viewer.Renderable; import org.apache.fop.render.awt.viewer.StatusListener; import org.apache.fop.render.java2d.Java2DRenderer; -import org.apache.fop.render.extensions.prepress.PageScaleAttributes; +import org.apache.fop.render.extensions.prepress.PageScale; /** * The AWTRender outputs the pages generated by the layout engine to a Swing @@ -159,8 +159,8 @@ public class AWTRenderer extends Java2DRenderer implements Pageable { / userAgent.getTargetPixelUnitToMillimeter(); if (getPageViewport(pageNum).getForeignAttributes() != null) { String scale = (String) getPageViewport(pageNum).getForeignAttributes().get( - PageScaleAttributes.EXT_PAGE_SCALE); - Point2D scales = PageScaleAttributes.getScaleAttributes(scale); + PageScale.EXT_PAGE_SCALE); + Point2D scales = PageScale.getScale(scale); if (scales != null) { scaleX *= scales.getX(); scaleY *= scales.getY(); diff --git a/src/java/org/apache/fop/render/extensions/prepress/PageScaleAttributes.java b/src/java/org/apache/fop/render/extensions/prepress/PageScale.java similarity index 66% rename from src/java/org/apache/fop/render/extensions/prepress/PageScaleAttributes.java rename to src/java/org/apache/fop/render/extensions/prepress/PageScale.java index d57ea708b..361423753 100644 --- a/src/java/org/apache/fop/render/extensions/prepress/PageScaleAttributes.java +++ b/src/java/org/apache/fop/render/extensions/prepress/PageScale.java @@ -21,6 +21,7 @@ package org.apache.fop.render.extensions.prepress; import java.awt.geom.Point2D; import java.text.MessageFormat; +import java.util.regex.Pattern; import org.apache.xmlgraphics.util.QName; @@ -29,7 +30,7 @@ import org.apache.fop.fo.extensions.ExtensionElementMapping; /** * This class provides utility methods to parse the 'fox:scale' extension attribute. */ -public final class PageScaleAttributes { +public final class PageScale { /** * The extension 'scale' attribute for the simple-page-master element. @@ -37,11 +38,12 @@ public final class PageScaleAttributes { public static final QName EXT_PAGE_SCALE = new QName(ExtensionElementMapping.URI, null, "scale"); + private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+"); /** * Utility classes should not have a public or default constructor */ - private PageScaleAttributes() { + private PageScale() { } /** @@ -50,31 +52,41 @@ public final class PageScaleAttributes { * @param scale scale attribute, input format: scaleX [scaleY] * @return the pair of (sx, sy) values */ - public static Point2D.Double getScaleAttributes(String scale) { + public static Point2D getScale(String scale) { + // TODO throw appropriate exceptions that can be caught by the event + // notification mechanism final String err = "Extension 'scale' attribute has incorrect value(s): {0}"; - if (scale == null) { + if (scale == null || scale.equals("")) { return null; } - Point2D.Double result = null; - + String[] scales = WHITESPACE_PATTERN.split(scale); + double scaleX; try { - String[] scales = scale.split(" "); - if (scales.length > 0) { - result = new Point2D.Double(Double.parseDouble(scales[0]), - Double.parseDouble(scales[0])); - } - if (scales.length > 1) { - result.y = Double.parseDouble(scales[1]); - } - if (result.x <= 0 || result.y <= 0) { + scaleX = Double.parseDouble(scales[0]); + } catch (NumberFormatException nfe) { + throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale})); + } + double scaleY; + switch (scales.length) { + case 1: + scaleY = scaleX; + break; + case 2: + try { + scaleY = Double.parseDouble(scales[1]); + } catch (NumberFormatException nfe) { throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale})); } - } catch (NumberFormatException nfe) { + break; + default: + throw new IllegalArgumentException("Too many arguments"); + } + if (scaleX <= 0 || scaleY <= 0) { throw new IllegalArgumentException(MessageFormat.format(err, new Object[]{scale})); } - return result; + return new Point2D.Double(scaleX, scaleY); } } diff --git a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java index aa25b3325..f09794ff2 100644 --- a/src/java/org/apache/fop/render/java2d/Java2DRenderer.java +++ b/src/java/org/apache/fop/render/java2d/Java2DRenderer.java @@ -77,7 +77,7 @@ import org.apache.fop.render.AbstractPathOrientedRenderer; import org.apache.fop.render.Graphics2DAdapter; import org.apache.fop.render.RendererContext; import org.apache.fop.render.extensions.prepress.PageBoundaries; -import org.apache.fop.render.extensions.prepress.PageScaleAttributes; +import org.apache.fop.render.extensions.prepress.PageScale; import org.apache.fop.render.pdf.CTMHelper; import org.apache.fop.util.CharUtilities; import org.apache.fop.util.ColorUtil; @@ -309,8 +309,8 @@ public abstract class Java2DRenderer extends AbstractPathOrientedRenderer implem double scaleX = scaleFactor; double scaleY = scaleFactor; String scale = (String) currentPageViewport.getForeignAttributes().get( - PageScaleAttributes.EXT_PAGE_SCALE); - Point2D scales = PageScaleAttributes.getScaleAttributes(scale); + PageScale.EXT_PAGE_SCALE); + Point2D scales = PageScale.getScale(scale); if (scales != null) { scaleX *= scales.getX(); scaleY *= scales.getY(); diff --git a/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java b/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java index fab93e3c4..12f99780b 100644 --- a/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java +++ b/src/java/org/apache/fop/render/pdf/PDFDocumentHandler.java @@ -42,7 +42,7 @@ import org.apache.fop.pdf.PDFReference; import org.apache.fop.pdf.PDFResourceContext; import org.apache.fop.pdf.PDFResources; import org.apache.fop.render.extensions.prepress.PageBoundaries; -import org.apache.fop.render.extensions.prepress.PageScaleAttributes; +import org.apache.fop.render.extensions.prepress.PageScale; import org.apache.fop.render.intermediate.AbstractBinaryWritingIFDocumentHandler; import org.apache.fop.render.intermediate.IFContext; import org.apache.fop.render.intermediate.IFDocumentHandlerConfigurator; @@ -183,8 +183,8 @@ public class PDFDocumentHandler extends AbstractBinaryWritingIFDocumentHandler { double scaleX = 1; double scaleY = 1; String scale = (String) getContext().getForeignAttribute( - PageScaleAttributes.EXT_PAGE_SCALE); - Point2D scales = PageScaleAttributes.getScaleAttributes(scale); + PageScale.EXT_PAGE_SCALE); + Point2D scales = PageScale.getScale(scale); if (scales != null) { scaleX = scales.getX(); scaleY = scales.getY(); diff --git a/test/java/org/apache/fop/render/extensions/prepress/PageScaleTest.java b/test/java/org/apache/fop/render/extensions/prepress/PageScaleTest.java index fc95e854d..cbf6f5226 100644 --- a/test/java/org/apache/fop/render/extensions/prepress/PageScaleTest.java +++ b/test/java/org/apache/fop/render/extensions/prepress/PageScaleTest.java @@ -46,14 +46,14 @@ public class PageScaleTest extends TestCase { /** 1 value is used for both x and y. */ public void testScale1() { - Point2D res = PageScaleAttributes.getScaleAttributes(".5"); + Point2D res = PageScale.getScale(".5"); assertEquals(0.5, res.getX(), 0.0); assertEquals(0.5, res.getY(), 0.0); } /** Two values, used resp. for x and y. */ public void testScale2() { - Point2D res = PageScaleAttributes.getScaleAttributes("1. 1.2"); + Point2D res = PageScale.getScale("1. \t \n 1.2"); assertEquals(1.0, res.getX(), 0.0); assertEquals(1.2, res.getY(), 0.0); } @@ -61,7 +61,7 @@ public class PageScaleTest extends TestCase { /** Scale must not contain units. */ public void testScaleFail() { try { - PageScaleAttributes.getScaleAttributes("0.5mm 0.5cm"); + PageScale.getScale("0.5mm 0.5cm"); fail("Expected IllegalArgumentException. Scale shouldn't contain units"); } catch (IllegalArgumentException iae) { // Good! @@ -70,7 +70,9 @@ public class PageScaleTest extends TestCase { /** @{code null} is returned when scale is unspecified. */ public void testScaleNull() { - Point2D res = PageScaleAttributes.getScaleAttributes(null); + Point2D res = PageScale.getScale(null); + assertNull("Result should be null", res); + res = PageScale.getScale(""); assertNull("Result should be null", res); } -- 2.39.5