]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Added test case for the gradient package
authorVincent Hennebert <vhennebert@apache.org>
Fri, 18 Jul 2014 13:44:56 +0000 (13:44 +0000)
committerVincent Hennebert <vhennebert@apache.org>
Fri, 18 Jul 2014 13:44:56 +0000 (13:44 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1611649 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/render/gradient/Function.java
src/java/org/apache/fop/render/gradient/Shading.java
test/java/org/apache/fop/render/gradient/GradientTestCase.java [new file with mode: 0644]
test/java/org/apache/fop/render/ps/svg/expected-linear-gradient.ps
test/java/org/apache/fop/render/ps/svg/expected-radial-gradient.ps

index f5e1e6ef82cd2a4c9f08db92db895efacc531eab..2ac0958198b55768fe5a983934c9fa4eac816be8 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.fop.render.gradient;
 
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 
@@ -171,15 +172,10 @@ public class Function {
      */
     public Function(int functionType, List<Double> domain, List<Double> range,
                        float[] cZero, float[] cOne, double interpolationExponentN) {
-        this.functionType = 2;    // dang well better be 2;
-
+        this(2, domain, range);
         this.cZero = cZero;
         this.cOne = cOne;
         this.interpolationExponentN = interpolationExponentN;
-
-        this.domain = domain;
-        this.range = range;
-
     }
 
     /**
@@ -219,14 +215,16 @@ public class Function {
     public Function(int functionType, List<Double> domain, List<Double> range,
                        List<Function> functions, List<Double> bounds,
                        List<Double> encode) {
-        this.functionType = 3;    // dang well better be 3;
-
+        this(3, domain, range);
         this.functions = functions;
         this.bounds = bounds;
         this.encode = encode;
-        this.domain = domain;
-        this.range = range;
+    }
 
+    private Function(int functionType, List<Double> domain, List<Double> range) {
+        this.functionType = functionType;
+        this.domain = (domain == null) ? Arrays.asList(0.0, 1.0) : domain;
+        this.range = range;
     }
 
     /**
@@ -406,13 +404,9 @@ public class Function {
     }
 
     private void outputDomain(StringBuilder p, DoubleFormatter doubleFormatter) {
-        if (domain != null) {
-            p.append("/Domain ");
-            GradientMaker.outputDoubles(p, doubleFormatter, domain);
-            p.append("\n");
-        } else {
-            p.append("/Domain [ 0 1 ]\n");
-        }
+        p.append("/Domain ");
+        GradientMaker.outputDoubles(p, doubleFormatter, domain);
+        p.append("\n");
     }
 
     private void outputSize(StringBuilder out, DoubleFormatter doubleFormatter) {
index 5b17db2707c7151c71175eda05fbd3f94099d48c..a9c9b10729dbe9632760f27aa2dc3466a37fe874 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.apache.fop.render.gradient;
 
+import java.util.Arrays;
 import java.util.List;
 
 import org.apache.fop.pdf.PDFDeviceColorSpace;
@@ -141,9 +142,9 @@ public class Shading {
         this.bbox = null;
         this.antiAlias = false;
         this.coords = coords;
-        this.domain = null;
+        this.domain = Arrays.asList(0.0, 1.0);
         this.function = function;
-        this.extend = null;
+        this.extend = Arrays.asList(true, true);
         this.matrix = null;
         this.decode = null;
         this.bitsPerCoordinate = 0;
@@ -257,13 +258,9 @@ public class Shading {
 
     private void outputShadingType1(StringBuilder out, DoubleFormatter doubleFormatter,
             Shading.FunctionRenderer functionRenderer) {
-        if (domain != null) {
-            out.append("/Domain ");
-            GradientMaker.outputDoubles(out, doubleFormatter, domain);
-            out.append("\n");
-        } else {
-            out.append("/Domain [ 0 1 ] \n");
-        }
+        out.append("/Domain ");
+        GradientMaker.outputDoubles(out, doubleFormatter, domain);
+        out.append("\n");
 
         if (matrix != null) {
             out.append("/Matrix ");
@@ -281,24 +278,16 @@ public class Shading {
             out.append("\n");
         }
 
-        if (domain != null) {
-            out.append("/Domain ");
-            GradientMaker.outputDoubles(out, doubleFormatter, domain);
-            out.append("\n");
-        } else {
-            out.append("/Domain [ 0 1 ] \n");
-        }
+        out.append("/Domain ");
+        GradientMaker.outputDoubles(out, doubleFormatter, domain);
+        out.append("\n");
 
-        if (extend != null) {
-            out.append("/Extend [");
-            for (Boolean b : extend) {
-                out.append(b);
-                out.append(" ");
-            }
-            out.append("\n");
-        } else {
-            out.append("/Extend [ true true ] \n");
+        out.append("/Extend [ ");
+        for (Boolean b : extend) {
+            out.append(b);
+            out.append(" ");
         }
+        out.append("]\n");
 
         outputFunction(out, functionRenderer);
     }
diff --git a/test/java/org/apache/fop/render/gradient/GradientTestCase.java b/test/java/org/apache/fop/render/gradient/GradientTestCase.java
new file mode 100644 (file)
index 0000000..b96631d
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * 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.gradient;
+
+import java.awt.Color;
+import java.awt.geom.AffineTransform;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import org.apache.batik.ext.awt.LinearGradientPaint;
+
+public class GradientTestCase {
+
+    private static class PatternChecker {
+
+        private final Pattern pattern;
+
+        PatternChecker(Pattern pattern) {
+            this.pattern = pattern;
+        }
+
+        public PatternChecker type(int expectedType) {
+            assertEquals(expectedType, pattern.getPatternType());
+            return this;
+        }
+
+        public PatternChecker matrix(Double... expectedMatrix) {
+            assertArrayEquals(expectedMatrix, pattern.getMatrix().toArray());
+            return this;
+        }
+
+        public ShadingChecker shading() {
+            return new ShadingChecker(pattern.getShading());
+        }
+    }
+
+    private static class ShadingChecker {
+
+        private final Shading shading;
+
+        ShadingChecker(Shading shading) {
+            this.shading = shading;
+        }
+
+        ShadingChecker shadingType(int expectedShadingType) {
+            assertEquals(expectedShadingType, shading.getShadingType());
+            return this;
+        }
+
+        ShadingChecker coords(Double... expectedCoords) {
+            assertArrayEquals(expectedCoords, shading.getCoords().toArray());
+            return this;
+        }
+
+        ShadingChecker domain(Double... expectedDomain) {
+            assertArrayEquals(expectedDomain, shading.getDomain().toArray());
+            return this;
+        }
+
+        ShadingChecker extend(Boolean... expectedExtend) {
+            assertArrayEquals(expectedExtend, shading.getExtend().toArray());
+            return this;
+        }
+
+        FunctionChecker function() {
+            return new FunctionChecker(shading.getFunction());
+        }
+    }
+
+    private static class FunctionChecker {
+
+        private final Function function;
+
+        FunctionChecker(Function function) {
+            this.function = function;
+        }
+
+        FunctionChecker functionType(int expectedFunctionType) {
+            assertEquals(expectedFunctionType, function.getFunctionType());
+            return this;
+        }
+
+        FunctionChecker domain(Double... expectedDomain) {
+            assertArrayEquals(expectedDomain, function.getDomain().toArray());
+            return this;
+        }
+
+        FunctionChecker cZero(float... expectedCZero) {
+            assertArrayEquals(expectedCZero, function.getCZero(), 0f);
+            return this;
+        }
+
+        FunctionChecker cOne(float... expectedCOne) {
+            assertArrayEquals(expectedCOne, function.getCOne(), 0f);
+            return this;
+        }
+
+        FunctionChecker functions(int expectedFunctionCount) {
+            assertEquals(expectedFunctionCount, function.getFunctions().size());
+            return this;
+        }
+
+        FunctionChecker function(int index) {
+            return new FunctionChecker(function.getFunctions().get(index));
+        }
+    }
+
+    @Test
+    public void testGradient() {
+        LinearGradientPaint gradient = new LinearGradientPaint(0f, 0f, 100f, 100f,
+                fractions(0f, 1f), colors(Color.BLUE, Color.RED));
+        Pattern pattern = GradientMaker.makeLinearGradient(gradient,
+                AffineTransform.getTranslateInstance(10.0, 20.0),
+                AffineTransform.getScaleInstance(100.0, 1000.0));
+        PatternChecker patternChecker = new PatternChecker(pattern)
+                .type(2)
+                .matrix(100.0, 0.0, 0.0, 1000.0, 10.0, 20.0);
+        ShadingChecker shadingChecker = patternChecker.shading()
+                .shadingType(2)
+                .coords(0.0, 0.0, 100.0, 100.0)
+                .domain(0.0, 1.0)
+                .extend(true, true);
+        FunctionChecker functionChecker = shadingChecker.function()
+                .functionType(3)
+                .domain(0.0, 1.0)
+                .functions(1);
+        functionChecker.function(0)
+                .functionType(2)
+                .domain(0.0, 1.0)
+                .cZero(0f, 0f, 1f)
+                .cOne(1f, 0f, 0f)
+                .functions(0);
+    }
+
+    private float[] fractions(float... fractions) {
+        return fractions;
+    }
+
+    private Color[] colors(Color... colors) {
+        return colors;
+    }
+
+}
index a81cd77668bf69d208ce57c36860aaf1eee9abec..8c753d06a5789cce6f3834388a4394d1c07f1c41 100644 (file)
@@ -6,8 +6,8 @@
 /ShadingType 2
 /ColorSpace /DeviceRGB
 /Coords [ 115 285 15 15 ]
-/Domain [ 0 1 ] 
-/Extend [ true true ] 
+/Domain [ 0 1 ]
+/Extend [ true true ]
 /Function <<
 /FunctionType 3
 /Domain [ 0 1 ]
index e8bbef313e3816d08865ddee40bef40d7dc0ba8c..b4af477b26bc30f120431c2b4091174ba2f8972a 100644 (file)
@@ -6,8 +6,8 @@
 /ShadingType 3
 /ColorSpace /DeviceRGB
 /Coords [ 840 180 0 840 180 16 ]
-/Domain [ 0 1 ] 
-/Extend [ true true ] 
+/Domain [ 0 1 ]
+/Extend [ true true ]
 /Function <<
 /FunctionType 3
 /Domain [ 0 1 ]