Browse Source

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
tags/fop-2_0
Vincent Hennebert 9 years ago
parent
commit
f8dd309bf6

+ 3
- 3
src/java/org/apache/fop/render/gradient/Function.java View File

@@ -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<Double> bounds;
private List<Float> 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<Double> domain, List<Double> range, List<Function> functions,
List<Double> bounds, List<Double> encode) {
List<Float> bounds, List<Double> encode) {
this(3, domain, range);
this.functions = functions;
this.bounds = bounds;
@@ -247,7 +247,7 @@ public class Function {
/**
* Gets the function bounds
*/
public List<Double> getBounds() {
public List<Float> getBounds() {
return bounds;
}


+ 8
- 8
src/java/org/apache/fop/render/gradient/GradientMaker.java View File

@@ -82,7 +82,7 @@ public final class GradientMaker {
private static Pattern makeGradient(MultipleGradientPaint gradient, List<Double> coords,
AffineTransform baseTransform, AffineTransform transform) {
List<Double> matrix = makeTransform(gradient, baseTransform, transform);
List<Double> bounds = makeBounds(gradient);
List<Float> bounds = makeBounds(gradient);
List<Function> 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<Double> makeBounds(MultipleGradientPaint gradient) {
// TODO is the conversion to double necessary?
private static List<Float> makeBounds(MultipleGradientPaint gradient) {
float[] fractions = gradient.getFractions();
List<Double> bounds = new java.util.ArrayList<Double>(fractions.length);
List<Float> bounds = new java.util.ArrayList<Float>(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<Double> doubles) {
static void outputDoubles(StringBuilder out, DoubleFormatter doubleFormatter,
List<? extends Number> 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("]");

+ 126
- 4
test/java/org/apache/fop/render/gradient/GradientTestCase.java View File

@@ -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;
}

Loading…
Cancel
Save