From 0a505d2b84aff8ae2be8b7aaa28ef7054794edf3 Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Fri, 18 Jul 2014 13:44:56 +0000 Subject: [PATCH] Added test case for the gradient package git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1611649 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/render/gradient/Function.java | 28 ++- .../apache/fop/render/gradient/Shading.java | 39 ++--- .../fop/render/gradient/GradientTestCase.java | 163 ++++++++++++++++++ .../render/ps/svg/expected-linear-gradient.ps | 4 +- .../render/ps/svg/expected-radial-gradient.ps | 4 +- 5 files changed, 192 insertions(+), 46 deletions(-) create mode 100644 test/java/org/apache/fop/render/gradient/GradientTestCase.java diff --git a/src/java/org/apache/fop/render/gradient/Function.java b/src/java/org/apache/fop/render/gradient/Function.java index f5e1e6ef8..2ac095819 100644 --- a/src/java/org/apache/fop/render/gradient/Function.java +++ b/src/java/org/apache/fop/render/gradient/Function.java @@ -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 domain, List 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 domain, List range, List functions, List bounds, List 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 domain, List 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) { diff --git a/src/java/org/apache/fop/render/gradient/Shading.java b/src/java/org/apache/fop/render/gradient/Shading.java index 5b17db270..a9c9b1072 100644 --- a/src/java/org/apache/fop/render/gradient/Shading.java +++ b/src/java/org/apache/fop/render/gradient/Shading.java @@ -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 index 000000000..b96631db7 --- /dev/null +++ b/test/java/org/apache/fop/render/gradient/GradientTestCase.java @@ -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; + } + +} diff --git a/test/java/org/apache/fop/render/ps/svg/expected-linear-gradient.ps b/test/java/org/apache/fop/render/ps/svg/expected-linear-gradient.ps index a81cd7766..8c753d06a 100644 --- a/test/java/org/apache/fop/render/ps/svg/expected-linear-gradient.ps +++ b/test/java/org/apache/fop/render/ps/svg/expected-linear-gradient.ps @@ -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 ] diff --git a/test/java/org/apache/fop/render/ps/svg/expected-radial-gradient.ps b/test/java/org/apache/fop/render/ps/svg/expected-radial-gradient.ps index e8bbef313..b4af477b2 100644 --- a/test/java/org/apache/fop/render/ps/svg/expected-radial-gradient.ps +++ b/test/java/org/apache/fop/render/ps/svg/expected-radial-gradient.ps @@ -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 ] -- 2.39.5