You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2008/07/28 10:10:00 UTC

svn commit: r680282 - in /xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop: pdf/PDFNumber.java render/AbstractRenderer.java render/intermediate/AbstractXMLWritingIFPainter.java render/pdf/PDFPainter.java util/DecimalFormatCache.java

Author: jeremias
Date: Mon Jul 28 01:09:59 2008
New Revision: 680282

URL: http://svn.apache.org/viewvc?rev=680282&view=rev
Log:
Dealt with some double formatting issues (coordinate round-trips).
Extracted the DecimalFormat cache from PDFNumber into a separate utility class to avoid code duplication.

Added:
    xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/util/DecimalFormatCache.java   (with props)
Modified:
    xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/pdf/PDFNumber.java
    xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/AbstractRenderer.java
    xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java
    xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainter.java

Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/pdf/PDFNumber.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/pdf/PDFNumber.java?rev=680282&r1=680281&r2=680282&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/pdf/PDFNumber.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/pdf/PDFNumber.java Mon Jul 28 01:09:59 2008
@@ -19,13 +19,11 @@
 
 package org.apache.fop.pdf;
 
-import java.text.DecimalFormat;
-import java.text.DecimalFormatSymbols;
-import java.util.Locale;
+import org.apache.fop.util.DecimalFormatCache;
 
 /**
  * This class represents a simple number object. It also contains contains some
- * utility methods for outputing numbers to PDF.
+ * utility methods for outputting numbers to PDF.
  */
 public class PDFNumber extends PDFObject {
 
@@ -67,11 +65,6 @@
         return doubleOut(doubleDown, 6);
     }
 
-    // Static cache. Possible concurrency implications. See comment in doubleOut(double, int).
-    private static DecimalFormat[] decimalFormatCache = new DecimalFormat[17];
-
-    private static final String BASE_FORMAT = "0.################";
-
     /**
      * Output a double value to a string suitable for PDF.
      * In this method it is possible to set the maximum
@@ -82,29 +75,10 @@
      * @return the value as a string
      */
     public static String doubleOut(double doubleDown, int dec) {
-        if (dec < 0 || dec >= decimalFormatCache.length) {
-            throw new IllegalArgumentException("Parameter dec must be between 1 and "
-                    + (decimalFormatCache.length + 1));
-        }
-        if (decimalFormatCache[dec] == null) {
-            //We don't care about the rare case where a DecimalFormat might be replaced in
-            //a multi-threaded environment, so we don't synchronize the access to the static
-            //array (mainly for performance reasons). After all, the DecimalFormat instances
-            //read-only objects so it doesn't matter which instance is used as long as one
-            //is available.
-            String s = "0";
-            if (dec > 0) {
-                s = BASE_FORMAT.substring(0, dec + 2);
-            }
-            DecimalFormat df = new DecimalFormat(s, new DecimalFormatSymbols(Locale.US));
-            decimalFormatCache[dec] = df;
-        }
-        return decimalFormatCache[dec].format(doubleDown);
+        return DecimalFormatCache.getDecimalFormat(dec).format(doubleDown);
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     protected String toPDFString() {
         if (getNumber() == null) {
             throw new IllegalArgumentException(

Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/AbstractRenderer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/AbstractRenderer.java?rev=680282&r1=680281&r2=680282&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/AbstractRenderer.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/AbstractRenderer.java Mon Jul 28 01:09:59 2008
@@ -860,8 +860,10 @@
         double[] matrix = new double[6];
         at.getMatrix(matrix);
         //Convert to millipoints
-        matrix[4] = matrix[4] * 1000;
-        matrix[5] = matrix[5] * 1000;
+        //Math.round() because things like this can happen: 65.6 * 1000 = 65.599999999999999
+        //which is bad for testing
+        matrix[4] = Math.round(matrix[4] * 1000);
+        matrix[5] = Math.round(matrix[5] * 1000);
         return new AffineTransform(matrix);
     }
 }

Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java?rev=680282&r1=680281&r2=680282&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/intermediate/AbstractXMLWritingIFPainter.java Mon Jul 28 01:09:59 2008
@@ -21,9 +21,6 @@
 
 import java.awt.Rectangle;
 import java.awt.geom.AffineTransform;
-import java.text.DecimalFormat;
-import java.text.DecimalFormatSymbols;
-import java.util.Locale;
 
 import javax.xml.transform.OutputKeys;
 import javax.xml.transform.Result;
@@ -39,6 +36,7 @@
 import org.xml.sax.helpers.AttributesImpl;
 
 import org.apache.fop.fonts.FontInfo;
+import org.apache.fop.util.DecimalFormatCache;
 
 /**
  * Abstract base class for XML-writing IFPainter implementations.
@@ -107,22 +105,13 @@
 
     /* ---=== helper methods ===--- */
 
-    private static final String BASE_FORMAT = "0.################";
-
-    private static class DecimalFormatThreadLocal extends ThreadLocal {
-
-        protected synchronized Object initialValue() {
-            DecimalFormat df = new DecimalFormat(BASE_FORMAT, new DecimalFormatSymbols(Locale.US));
-            return df;
-        }
-    };
-
-    //DecimalFormat is not thread-safe!
-    private static final ThreadLocal DECIMAL_FORMAT = new DecimalFormatThreadLocal();
-
     private static String format(double value) {
-        DecimalFormat df = (DecimalFormat)DECIMAL_FORMAT.get();
-        return df.format(value);
+        if (value == -0.0) {
+            //Don't allow negative zero because of testing
+            //See http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3
+            value = 0.0;
+        }
+        return DecimalFormatCache.getDecimalFormat(6).format(value);
     }
 
     /**
@@ -156,7 +145,6 @@
         return sb;
     }
 
-
     /**
      * Convenience method to generate a startElement SAX event.
      * @param localName the local name of the element

Modified: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainter.java?rev=680282&r1=680281&r2=680282&view=diff
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainter.java (original)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/render/pdf/PDFPainter.java Mon Jul 28 01:09:59 2008
@@ -490,8 +490,6 @@
                         ch = (char)(ch % 256);
                     }
                 }
-                //int tls = (i < l - 1 ? parentArea.getTextLetterSpaceAdjust() : 0);
-                //glyphAdjust -= tls;
             } else {
                 if (CharUtilities.isFixedWidthSpace(orgChar)) {
                     //Fixed width space are rendered as spaces so copy/paste works in a reader

Added: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/util/DecimalFormatCache.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/util/DecimalFormatCache.java?rev=680282&view=auto
==============================================================================
--- xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/util/DecimalFormatCache.java (added)
+++ xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/util/DecimalFormatCache.java Mon Jul 28 01:09:59 2008
@@ -0,0 +1,74 @@
+/*
+ * 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.util;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.util.Locale;
+
+/**
+ * This class provides a cache for {@code DecimalFormat} instance. {@code DecimalFormat} itself
+ * is not thread-safe but since FOP needs to format a lot of numbers the same way, it shall
+ * be cached in a {@code ThreadLocal}.
+ */
+public class DecimalFormatCache {
+
+    private static final String BASE_FORMAT = "0.################";
+
+    private static class DecimalFormatThreadLocal extends ThreadLocal {
+
+        private int dec;
+
+        public DecimalFormatThreadLocal(int dec) {
+            this.dec = dec;
+        }
+
+        protected synchronized Object initialValue() {
+            String s = "0";
+            if (dec > 0) {
+                s = BASE_FORMAT.substring(0, dec + 2);
+            }
+            DecimalFormat df = new DecimalFormat(s, new DecimalFormatSymbols(Locale.US));
+            return df;
+        }
+    };
+
+    //DecimalFormat is not thread-safe!
+    private static final ThreadLocal[] DECIMAL_FORMAT_CACHE = new DecimalFormatThreadLocal[17];
+    static {
+        for (int i = 0, c = DECIMAL_FORMAT_CACHE.length; i < c; i++) {
+            DECIMAL_FORMAT_CACHE[i] = new DecimalFormatThreadLocal(i);
+        }
+    }
+
+    /**
+     * Returns a cached {@code DecimalFormat} instance for the given number of decimal digits.
+     * @param dec the number of decimal digits.
+     * @return the DecimalFormat instance
+     */
+    public static DecimalFormat getDecimalFormat(int dec) {
+        if (dec < 0 || dec >= DECIMAL_FORMAT_CACHE.length) {
+            throw new IllegalArgumentException("Parameter dec must be between 1 and "
+                    + (DECIMAL_FORMAT_CACHE.length + 1));
+        }
+        return (DecimalFormat)DECIMAL_FORMAT_CACHE[dec].get();
+    }
+
+}

Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/util/DecimalFormatCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/fop/branches/Temp_AreaTreeNewDesign/src/java/org/apache/fop/util/DecimalFormatCache.java
------------------------------------------------------------------------------
    svn:keywords = Id



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org