]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Use the appropriate double formatter depending on the selected output
authorVincent Hennebert <vhennebert@apache.org>
Mon, 14 Jul 2014 21:25:39 +0000 (21:25 +0000)
committerVincent Hennebert <vhennebert@apache.org>
Mon, 14 Jul 2014 21:25:39 +0000 (21:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1610539 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/pdf/PDFFunction.java
src/java/org/apache/fop/pdf/PDFShading.java
src/java/org/apache/fop/render/gradient/Function.java
src/java/org/apache/fop/render/gradient/GradientMaker.java
src/java/org/apache/fop/render/gradient/Shading.java
src/java/org/apache/fop/render/ps/svg/PSSVGGraphics2D.java

index ed2d1d16ca60c2caf7dbc6527089907a61fe0e7e..f84c52609c0a3ba36b1d832a231e69eb188892be 100644 (file)
@@ -26,6 +26,8 @@ import java.util.List;
 
 import org.apache.fop.render.gradient.Function;
 import org.apache.fop.render.gradient.Function.SubFunctionRenderer;
+import org.apache.fop.render.gradient.GradientMaker;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
 
 /**
  * class representing a PDF Function.
@@ -119,7 +121,13 @@ public class PDFFunction extends PDFObject {
             }
         };
         StringBuilder out = new StringBuilder();
-        function.output(out, subFunctionRenderer);
+        GradientMaker.DoubleFormatter doubleFormatter = new DoubleFormatter() {
+
+            public String formatDouble(double d) {
+                return PDFNumber.doubleOut(d);
+            }
+        };
+        function.output(out, doubleFormatter, subFunctionRenderer);
         return encode(out.toString());
     }
 
index 577e10a09ed3e644d6e626549472229a107ccdd8..fa12771b97145820489d210acf60addd7eb31890 100644 (file)
@@ -21,6 +21,8 @@ package org.apache.fop.pdf;
 
 import java.util.List;
 
+import org.apache.fop.render.gradient.GradientMaker;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
 import org.apache.fop.render.gradient.Shading;
 
 
@@ -102,7 +104,13 @@ public class PDFShading extends PDFObject {
             }
         };
         StringBuilder out = new StringBuilder();
-        shading.output(out, functionRenderer);
+        GradientMaker.DoubleFormatter doubleFormatter = new DoubleFormatter() {
+
+            public String formatDouble(double d) {
+                return PDFNumber.doubleOut(d);
+            }
+        };
+        shading.output(out, doubleFormatter, functionRenderer);
         return out.toString();
     }
 
index ca87d509db09cca952ccde1c99bca839b136dcee..f5e1e6ef82cd2a4c9f08db92db895efacc531eab 100644 (file)
@@ -20,7 +20,7 @@ package org.apache.fop.render.gradient;
 import java.util.Collections;
 import java.util.List;
 
-import org.apache.fop.pdf.PDFNumber;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
 
 public class Function {
 
@@ -338,16 +338,17 @@ public class Function {
         return cOne;
     }
 
-    public String output(StringBuilder out, SubFunctionRenderer subFunctionRenderer) {
+    public String output(StringBuilder out, DoubleFormatter doubleFormatter,
+            SubFunctionRenderer subFunctionRenderer) {
         out.append("<<\n/FunctionType " + functionType + "\n");
-        outputDomain(out);
+        outputDomain(out, doubleFormatter);
         if (this.functionType == 0) {
-            outputSize(out);
-            outputEncode(out);
+            outputSize(out, doubleFormatter);
+            outputEncode(out, doubleFormatter);
             outputBitsPerSample(out);
             outputOrder(out);
-            outputRange(out);
-            outputDecode(out);
+            outputRange(out, doubleFormatter);
+            outputDecode(out, doubleFormatter);
             if (functionDataStream != null) {
                 out.append("/Length " + (functionDataStream.length() + 1) + "\n");
             }
@@ -357,13 +358,13 @@ public class Function {
                 out.append("\nstream\n" + functionDataStream + "\nendstream");
             }
         } else if (functionType == 2) {
-            outputRange(out);
-            outputCZero(out);
-            outputCOne(out);
-            outputInterpolationExponentN(out);
+            outputRange(out, doubleFormatter);
+            outputCZero(out, doubleFormatter);
+            outputCOne(out, doubleFormatter);
+            outputInterpolationExponentN(out, doubleFormatter);
             out.append(">>");
         } else if (functionType == 3) {
-            outputRange(out);
+            outputRange(out, doubleFormatter);
             if (!functions.isEmpty()) {
                 out.append("/Functions [ ");
                 for (int i = 0; i < functions.size(); i++) {
@@ -372,17 +373,17 @@ public class Function {
                 }
                 out.append("]\n");
             }
-            outputEncode(out);
+            outputEncode(out, doubleFormatter);
             out.append("/Bounds ");
             if (bounds != null) {
-                GradientMaker.outputDoubles(out, bounds);
+                GradientMaker.outputDoubles(out, doubleFormatter, bounds);
             } else if (!functions.isEmpty()) {
                 // if there are n functions,
                 // there must be n-1 bounds.
                 // so let each function handle an equal portion
                 // of the whole. e.g. if there are 4, then [ 0.25 0.25 0.25 ]
                 int numberOfFunctions = functions.size();
-                String functionsFraction = PDFNumber.doubleOut(1.0 / numberOfFunctions);
+                String functionsFraction = doubleFormatter.formatDouble(1.0 / numberOfFunctions);
                 out.append("[ ");
                 for (int i = 0; i + 1 < numberOfFunctions; i++) {
                     out.append(functionsFraction);
@@ -392,7 +393,7 @@ public class Function {
             }
             out.append("\n>>");
         } else if (functionType == 4) {
-            outputRange(out);
+            outputRange(out, doubleFormatter);
             if (functionDataStream != null) {
                 out.append("/Length " + (functionDataStream.length() + 1) + "\n");
             }
@@ -404,20 +405,20 @@ public class Function {
         return out.toString();
     }
 
-    private void outputDomain(StringBuilder p) {
+    private void outputDomain(StringBuilder p, DoubleFormatter doubleFormatter) {
         if (domain != null) {
             p.append("/Domain ");
-            GradientMaker.outputDoubles(p, domain);
+            GradientMaker.outputDoubles(p, doubleFormatter, domain);
             p.append("\n");
         } else {
             p.append("/Domain [ 0 1 ]\n");
         }
     }
 
-    private void outputSize(StringBuilder out) {
+    private void outputSize(StringBuilder out, DoubleFormatter doubleFormatter) {
         if (size != null) {
             out.append("/Size ");
-            GradientMaker.outputDoubles(out, size);
+            GradientMaker.outputDoubles(out, doubleFormatter, size);
             out.append("\n");
         }
     }
@@ -432,18 +433,18 @@ public class Function {
         }
     }
 
-    private void outputRange(StringBuilder out) {
+    private void outputRange(StringBuilder out, DoubleFormatter doubleFormatter) {
         if (range != null) {
             out.append("/Range ");
-            GradientMaker.outputDoubles(out, range);
+            GradientMaker.outputDoubles(out, doubleFormatter, range);
             out.append("\n");
         }
     }
 
-    private void outputEncode(StringBuilder out) {
+    private void outputEncode(StringBuilder out, DoubleFormatter doubleFormatter) {
         if (encode != null) {
             out.append("/Encode ");
-            GradientMaker.outputDoubles(out, encode);
+            GradientMaker.outputDoubles(out, doubleFormatter, encode);
             out.append("\n");
         } else {
             out.append("/Encode [ ");
@@ -455,10 +456,10 @@ public class Function {
         }
     }
 
-    private void outputDecode(StringBuilder out) {
+    private void outputDecode(StringBuilder out, DoubleFormatter doubleFormatter) {
         if (decode != null) {
             out.append("/Decode ");
-            GradientMaker.outputDoubles(out, decode);
+            GradientMaker.outputDoubles(out, doubleFormatter, decode);
             out.append("\n");
         }
     }
@@ -479,31 +480,31 @@ public class Function {
         }
     }
 
-    private void outputCZero(StringBuilder out) {
+    private void outputCZero(StringBuilder out, DoubleFormatter doubleFormatter) {
         if (cZero != null) {
             out.append("/C0 [ ");
             for (float c : cZero) {
-                out.append(PDFNumber.doubleOut(c));
+                out.append(doubleFormatter.formatDouble(c));
                 out.append(" ");
             }
             out.append("]\n");
         }
     }
 
-    private void outputCOne(StringBuilder out) {
+    private void outputCOne(StringBuilder out, DoubleFormatter doubleFormatter) {
         if (cOne != null) {
             out.append("/C1 [ ");
             for (float c : cOne) {
-                out.append(PDFNumber.doubleOut(c));
+                out.append(doubleFormatter.formatDouble(c));
                 out.append(" ");
             }
             out.append("]\n");
         }
     }
 
-    private void outputInterpolationExponentN(StringBuilder out) {
+    private void outputInterpolationExponentN(StringBuilder out, DoubleFormatter doubleFormatter) {
         out.append("/N ");
-        out.append(PDFNumber.doubleOut(interpolationExponentN));
+        out.append(doubleFormatter.formatDouble(interpolationExponentN));
         out.append("\n");
     }
 
index 1f378e465fb312e0b7c1452aad1cb0613cb20049..7c6b5c27d42000b8aea3003914dd779346fefae7 100644 (file)
@@ -32,10 +32,14 @@ import org.apache.batik.ext.awt.RadialGradientPaint;
 import org.apache.xmlgraphics.java2d.color.ColorUtil;
 
 import org.apache.fop.pdf.PDFDeviceColorSpace;
-import org.apache.fop.pdf.PDFNumber;
 
 public final class GradientMaker {
 
+    public interface DoubleFormatter {
+
+        String formatDouble(double d);
+    }
+
     private GradientMaker() { }
 
     public static Pattern makeLinearGradient(LinearGradientPaint gp,
@@ -152,10 +156,10 @@ public final class GradientMaker {
         return gradientColors;
     }
 
-    static void outputDoubles(StringBuilder out, List<Double> doubles) {
+    static void outputDoubles(StringBuilder out, DoubleFormatter doubleFormatter, List<Double> doubles) {
         out.append("[ ");
         for (Double d : doubles) {
-            out.append(PDFNumber.doubleOut(d));
+            out.append(doubleFormatter.formatDouble(d));
             out.append(" ");
         }
         out.append("]");
index 593a22dd22c74ba298705172bd76cd6c89859465..5b17db2707c7151c71175eda05fbd3f94099d48c 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.fop.render.gradient;
 import java.util.List;
 
 import org.apache.fop.pdf.PDFDeviceColorSpace;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
 
 
 public class Shading {
@@ -211,7 +212,7 @@ public class Shading {
         return verticesPerRow;
     }
 
-    public void output(StringBuilder out, FunctionRenderer functionRenderer) {
+    public void output(StringBuilder out, DoubleFormatter doubleFormatter, FunctionRenderer functionRenderer) {
         out.append("<<\n/ShadingType " + shadingType + "\n");
         if (colorSpace != null) {
             out.append("/ColorSpace /" + colorSpace.getName() + "\n");
@@ -219,13 +220,13 @@ public class Shading {
 
         if (background != null) {
             out.append("/Background ");
-            GradientMaker.outputDoubles(out, background);
+            GradientMaker.outputDoubles(out, doubleFormatter, background);
             out.append("\n");
         }
 
         if (bbox != null) {
             out.append("/BBox");
-            GradientMaker.outputDoubles(out, bbox);
+            GradientMaker.outputDoubles(out, doubleFormatter, bbox);
             out.append("\n");
         }
 
@@ -235,29 +236,30 @@ public class Shading {
 
         switch (shadingType) {
         // Function based shading
-        case 1: outputShadingType1(out, functionRenderer); break;
+        case 1: outputShadingType1(out, doubleFormatter, functionRenderer); break;
         // Axial shading
         case 2:
         // Radial shading
-        case 3: outputShadingType2or3(out, functionRenderer); break;
+        case 3: outputShadingType2or3(out, doubleFormatter, functionRenderer); break;
         // Free-form Gouraud-shaded triangle meshes
         case 4:
         // Coons patch meshes
         case 6:
         // Tensor product patch meshes
-        case 7: outputShadingType4or6or7(out, functionRenderer); break;
+        case 7: outputShadingType4or6or7(out, doubleFormatter, functionRenderer); break;
         // Lattice Free form gouraud-shaded triangle mesh
-        case 5: outputShadingType5(out, functionRenderer); break;
+        case 5: outputShadingType5(out, doubleFormatter, functionRenderer); break;
         default: throw new UnsupportedOperationException("Shading type " + shadingType);
         }
 
         out.append(">>");
     }
 
-    private void outputShadingType1(StringBuilder out, Shading.FunctionRenderer functionRenderer) {
+    private void outputShadingType1(StringBuilder out, DoubleFormatter doubleFormatter,
+            Shading.FunctionRenderer functionRenderer) {
         if (domain != null) {
             out.append("/Domain ");
-            GradientMaker.outputDoubles(out, domain);
+            GradientMaker.outputDoubles(out, doubleFormatter, domain);
             out.append("\n");
         } else {
             out.append("/Domain [ 0 1 ] \n");
@@ -265,22 +267,23 @@ public class Shading {
 
         if (matrix != null) {
             out.append("/Matrix ");
-            GradientMaker.outputDoubles(out, matrix);
+            GradientMaker.outputDoubles(out, doubleFormatter, matrix);
             out.append("\n");
         }
         outputFunction(out, functionRenderer);
     }
 
-    private void outputShadingType2or3(StringBuilder out, Shading.FunctionRenderer functionRenderer) {
+    private void outputShadingType2or3(StringBuilder out, DoubleFormatter doubleFormatter,
+            Shading.FunctionRenderer functionRenderer) {
         if (coords != null) {
             out.append("/Coords ");
-            GradientMaker.outputDoubles(out, coords);
+            GradientMaker.outputDoubles(out, doubleFormatter, coords);
             out.append("\n");
         }
 
         if (domain != null) {
             out.append("/Domain ");
-            GradientMaker.outputDoubles(out, domain);
+            GradientMaker.outputDoubles(out, doubleFormatter, domain);
             out.append("\n");
         } else {
             out.append("/Domain [ 0 1 ] \n");
@@ -300,7 +303,8 @@ public class Shading {
         outputFunction(out, functionRenderer);
     }
 
-    private void outputShadingType4or6or7(StringBuilder out, Shading.FunctionRenderer functionRenderer) {
+    private void outputShadingType4or6or7(StringBuilder out, DoubleFormatter doubleFormatter,
+            Shading.FunctionRenderer functionRenderer) {
         if (bitsPerCoordinate > 0) {
             out.append("/BitsPerCoordinate " + bitsPerCoordinate + "\n");
         } else {
@@ -321,14 +325,15 @@ public class Shading {
 
         if (decode != null) {
             out.append("/Decode ");
-            GradientMaker.outputDoubles(out, decode);
+            GradientMaker.outputDoubles(out, doubleFormatter, decode);
             out.append("\n");
         }
 
         outputFunction(out, functionRenderer);
     }
 
-    private void outputShadingType5(StringBuilder out, Shading.FunctionRenderer functionRenderer) {
+    private void outputShadingType5(StringBuilder out, DoubleFormatter doubleFormatter,
+            Shading.FunctionRenderer functionRenderer) {
         if (bitsPerCoordinate > 0) {
             out.append("/BitsPerCoordinate " + bitsPerCoordinate + "\n");
         } else {
@@ -343,7 +348,7 @@ public class Shading {
 
         if (decode != null) {
             out.append("/Decode ");
-            GradientMaker.outputDoubles(out, decode);
+            GradientMaker.outputDoubles(out, doubleFormatter, decode);
             out.append("\n");
         }
 
index a89b16bde57bae6fdfd1a37f3f797e77ae0d3666..28fa4ec9c62dff1cce109394d293a6a9b3de48eb 100644 (file)
@@ -36,6 +36,7 @@ import org.apache.xmlgraphics.ps.PSGenerator;
 import org.apache.fop.render.gradient.Function;
 import org.apache.fop.render.gradient.Function.SubFunctionRenderer;
 import org.apache.fop.render.gradient.GradientMaker;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
 import org.apache.fop.render.gradient.Pattern;
 import org.apache.fop.render.gradient.Shading;
 
@@ -77,7 +78,7 @@ public class PSSVGGraphics2D extends PSGraphics2D {
             Pattern pattern = GradientMaker.makeLinearGradient((LinearGradientPaint) paint,
                     getBaseTransform(), getTransform());
             try {
-                gen.write(toString(pattern));
+                gen.write(outputPattern(pattern));
             } catch (IOException ioe) {
                 handleIOException(ioe);
             }
@@ -85,19 +86,14 @@ public class PSSVGGraphics2D extends PSGraphics2D {
             Pattern pattern = GradientMaker.makeRadialGradient((RadialGradientPaint) paint,
                     getBaseTransform(), getTransform());
             try {
-                gen.write(toString(pattern));
+                gen.write(outputPattern(pattern));
             } catch (IOException ioe) {
                 handleIOException(ioe);
             }
         }
     }
 
-    /**
-     * Outputs the radial or axial pattern as a string dictionary to insert
-     * into a postscript document.
-     * @param pattern
-     */
-    private String toString(Pattern pattern) {
+    private String outputPattern(Pattern pattern) {
         StringBuilder p = new StringBuilder(64);
         p.append("/Pattern setcolorspace\n");
         p.append("<< \n/Type /Pattern \n");
@@ -112,7 +108,7 @@ public class PSSVGGraphics2D extends PSGraphics2D {
         p.append(">> \n");
         p.append("[ ");
         for (double m : pattern.getMatrix()) {
-            p.append(Double.toString(m)); // TODO refactor so that PSGenerator.formatDouble can be used
+            p.append(getPSGenerator().formatDouble(m));
             p.append(" ");
         }
         p.append("] ");
@@ -122,6 +118,12 @@ public class PSSVGGraphics2D extends PSGraphics2D {
     }
 
     private void outputShading(StringBuilder out, Shading shading) {
+        final GradientMaker.DoubleFormatter doubleFormatter = new DoubleFormatter() {
+
+            public String formatDouble(double d) {
+                return getPSGenerator().formatDouble(d);
+            }
+        };
         final Function function = shading.getFunction();
         Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() {
 
@@ -131,13 +133,13 @@ public class PSSVGGraphics2D extends PSGraphics2D {
                     public void outputFunction(StringBuilder out, int functionIndex) {
                         Function subFunction = function.getFunctions().get(functionIndex);
                         assert subFunction.getFunctions().isEmpty();
-                        subFunction.output(out, null);
+                        subFunction.output(out, doubleFormatter, null);
                     }
                 };
-                function.output(out, subFunctionRenderer);
+                function.output(out, doubleFormatter, subFunctionRenderer);
             }
         };
-        shading.output(out, functionRenderer);
+        shading.output(out, doubleFormatter, functionRenderer);
     }
 
     protected AffineTransform getBaseTransform() {