From f8dd309bf637ecd62762fdfd889be1e7b210b755 Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Fri, 18 Jul 2014 13:49:05 +0000 Subject: [PATCH] Added more complete tests Keep floats for bounds instead of converting them to doubles git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1611653 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/render/gradient/Function.java | 6 +- .../fop/render/gradient/GradientMaker.java | 16 +-- .../fop/render/gradient/GradientTestCase.java | 130 +++++++++++++++++- 3 files changed, 137 insertions(+), 15 deletions(-) diff --git a/src/java/org/apache/fop/render/gradient/Function.java b/src/java/org/apache/fop/render/gradient/Function.java index e0dc210d0..38aec8064 100644 --- a/src/java/org/apache/fop/render/gradient/Function.java +++ b/src/java/org/apache/fop/render/gradient/Function.java @@ -143,7 +143,7 @@ public class Function { * This makes each function responsible for an equal amount of the stitching function. * It makes the gradient even. */ - private List bounds; + private List bounds; /** * create an complete Function object of Type 2, an Exponential Interpolation function. @@ -211,7 +211,7 @@ public class Function { * See page 270 in the PDF 1.3 spec. */ public Function(List domain, List range, List functions, - List bounds, List encode) { + List bounds, List encode) { this(3, domain, range); this.functions = functions; this.bounds = bounds; @@ -247,7 +247,7 @@ public class Function { /** * Gets the function bounds */ - public List getBounds() { + public List getBounds() { return bounds; } diff --git a/src/java/org/apache/fop/render/gradient/GradientMaker.java b/src/java/org/apache/fop/render/gradient/GradientMaker.java index d8307c300..4c8734d9f 100644 --- a/src/java/org/apache/fop/render/gradient/GradientMaker.java +++ b/src/java/org/apache/fop/render/gradient/GradientMaker.java @@ -82,7 +82,7 @@ public final class GradientMaker { private static Pattern makeGradient(MultipleGradientPaint gradient, List coords, AffineTransform baseTransform, AffineTransform transform) { List matrix = makeTransform(gradient, baseTransform, transform); - List bounds = makeBounds(gradient); + List bounds = makeBounds(gradient); List functions = makeFunctions(gradient); // Gradients are currently restricted to sRGB PDFDeviceColorSpace colorSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB); @@ -112,13 +112,12 @@ public final class GradientMaker { return c.getColorSpace().isCS_sRGB() ? c : ColorUtil.toSRGBColor(c); } - private static List makeBounds(MultipleGradientPaint gradient) { - // TODO is the conversion to double necessary? + private static List makeBounds(MultipleGradientPaint gradient) { float[] fractions = gradient.getFractions(); - List bounds = new java.util.ArrayList(fractions.length); + List bounds = new java.util.ArrayList(fractions.length); for (float offset : fractions) { if (0f < offset && offset < 1f) { - bounds.add(Double.valueOf(offset)); + bounds.add(offset); } } return bounds; @@ -156,10 +155,11 @@ public final class GradientMaker { return gradientColors; } - static void outputDoubles(StringBuilder out, DoubleFormatter doubleFormatter, List doubles) { + static void outputDoubles(StringBuilder out, DoubleFormatter doubleFormatter, + List numbers) { out.append("[ "); - for (Double d : doubles) { - out.append(doubleFormatter.formatDouble(d)); + for (Number n : numbers) { + out.append(doubleFormatter.formatDouble(n.doubleValue())); out.append(" "); } out.append("]"); diff --git a/test/java/org/apache/fop/render/gradient/GradientTestCase.java b/test/java/org/apache/fop/render/gradient/GradientTestCase.java index 13a1c173d..51a9c6c37 100644 --- a/test/java/org/apache/fop/render/gradient/GradientTestCase.java +++ b/test/java/org/apache/fop/render/gradient/GradientTestCase.java @@ -28,6 +28,7 @@ import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import org.apache.batik.ext.awt.LinearGradientPaint; +import org.apache.batik.ext.awt.RadialGradientPaint; public class GradientTestCase { @@ -67,8 +68,13 @@ public class GradientTestCase { return this; } - ShadingChecker coords(Double... expectedCoords) { - assertArrayEquals(expectedCoords, shading.getCoords().toArray()); + ShadingChecker coords(double... expectedCoords) { + double[] coords = new double[shading.getCoords().size()]; + int index = 0; + for (Double d : shading.getCoords()) { + coords[index++] = d; + } + assertArrayEquals(expectedCoords, coords, 0.0001); return this; } @@ -100,7 +106,7 @@ public class GradientTestCase { return this; } - FunctionChecker bounds(Double... expectedBounds) { + FunctionChecker bounds(Float... expectedBounds) { assertArrayEquals(expectedBounds, function.getBounds().toArray()); return this; } @@ -131,7 +137,7 @@ public class GradientTestCase { } @Test - public void testGradient() { + public void simpleLinearGradient() { LinearGradientPaint gradient = new LinearGradientPaint(0f, 0f, 100f, 100f, fractions(0f, 1f), colors(Color.BLUE, Color.RED)); Pattern pattern = GradientMaker.makeLinearGradient(gradient, @@ -158,6 +164,122 @@ public class GradientTestCase { .functions(0); } + @Test + public void simpleRadialGradient() { + RadialGradientPaint gradient = new RadialGradientPaint(100, 200, 50, + fractions(0f, 1f), colors(Color.BLUE, Color.RED)); + Pattern pattern = GradientMaker.makeRadialGradient(gradient, new AffineTransform(), new AffineTransform()); + PatternChecker patternChecker = new PatternChecker(pattern).type(2); + ShadingChecker shadingChecker = patternChecker.shading() + .shadingType(3) + .coords(100.0, 200.0, 0.0, 100.0, 200.0, 50.0) + .extend(true, true); + FunctionChecker functionChecker = shadingChecker.function() + .functionType(3) + .domain(0.0, 1.0) + .bounds() + .encode(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); + } + + @Test + public void threeColorLinearGradient() { + LinearGradientPaint gradient = new LinearGradientPaint(0f, 10f, 20f, 30f, + fractions(0f, 0.5f, 1f), colors(Color.BLUE, Color.RED, Color.GREEN)); + Pattern pattern = GradientMaker.makeLinearGradient(gradient, new AffineTransform(), new AffineTransform()); + PatternChecker patternChecker = new PatternChecker(pattern) + .type(2) + .matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0); + ShadingChecker shadingChecker = patternChecker.shading() + .shadingType(2) + .coords(0.0, 10.0, 20.0, 30.0) + .extend(true, true); + FunctionChecker functionChecker = shadingChecker.function() + .functionType(3) + .domain(0.0, 1.0) + .bounds(0.5f) + .encode(0.0, 1.0, 0.0, 1.0) + .functions(2); + functionChecker.function(0) + .functionType(2) + .domain(0.0, 1.0) + .cZero(0f, 0f, 1f) + .cOne(1f, 0f, 0f) + .functions(0); + functionChecker.function(1) + .functionType(2) + .domain(0.0, 1.0) + .cZero(1f, 0f, 0f) + .cOne(0f, 1f, 0f) + .functions(0); + } + + @Test + public void fourColorRadialGradientNonZeroFirstStop() { + RadialGradientPaint gradient = new RadialGradientPaint(100, 200, 50, 110, 220, + fractions(0.2f, 0.5f, 0.7f, 1f), colors(Color.BLUE, Color.RED, Color.GREEN, Color.WHITE)); + Pattern pattern = GradientMaker.makeRadialGradient(gradient, new AffineTransform(), new AffineTransform()); + ShadingChecker shadingChecker = new PatternChecker(pattern).shading() + .coords(110.0, 220.0, 0.0, 100.0, 200.0, 50.0); + FunctionChecker functionChecker = shadingChecker.function() + .functionType(3) + .bounds(0.2f, 0.5f, 0.7f) + .encode(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0) + .functions(4); + functionChecker.function(0) + .functionType(2) + .cZero(0f, 0f, 1f) + .cOne(0f, 0f, 1f); + functionChecker.function(1) + .functionType(2) + .cZero(0f, 0f, 1f) + .cOne(1f, 0f, 0f); + functionChecker.function(2) + .functionType(2) + .cZero(1f, 0f, 0f) + .cOne(0f, 1f, 0f); + functionChecker.function(3) + .functionType(2) + .cZero(0f, 1f, 0f) + .cOne(1f, 1f, 1f); + } + + @Test + public void fourColorRadialGradientNonZeroLastStopFocalOut() { + RadialGradientPaint gradient = new RadialGradientPaint(0, 0, 100, 100, 100, + fractions(0f, 0.3f, 0.6f, 0.9f), colors(Color.WHITE, Color.RED, Color.GREEN, Color.BLUE)); + Pattern pattern = GradientMaker.makeRadialGradient(gradient, new AffineTransform(), new AffineTransform()); + ShadingChecker shadingChecker = new PatternChecker(pattern).shading() + .coords(70.7036, 70.7036, 0.0, 0.0, 0.0, 100.0); + FunctionChecker functionChecker = shadingChecker.function() + .functionType(3) + .bounds(0.3f, 0.6f, 0.9f) + .encode(0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0) + .functions(4); + functionChecker.function(0) + .functionType(2) + .cZero(1f, 1f, 1f) + .cOne(1f, 0f, 0f); + functionChecker.function(1) + .functionType(2) + .cZero(1f, 0f, 0f) + .cOne(0f, 1f, 0f); + functionChecker.function(2) + .functionType(2) + .cZero(0f, 1f, 0f) + .cOne(0f, 0f, 1f); + functionChecker.function(3) + .functionType(2) + .cZero(0f, 0f, 1f) + .cOne(0f, 0f, 1f); + } + private float[] fractions(float... fractions) { return fractions; } -- 2.39.5