From 1cc7422fab91ce2f6dfb07762b6bed6c4913793f Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Thu, 10 Jul 2014 17:59:14 +0000 Subject: [PATCH] Factorized code that is common to linear and radial gradient creation git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1609530 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/render/shading/GradientFactory.java | 71 ++++++++----------- 1 file changed, 31 insertions(+), 40 deletions(-) diff --git a/src/java/org/apache/fop/render/shading/GradientFactory.java b/src/java/org/apache/fop/render/shading/GradientFactory.java index 2809ec8a1..9a68cdf7d 100644 --- a/src/java/org/apache/fop/render/shading/GradientFactory.java +++ b/src/java/org/apache/fop/render/shading/GradientFactory.java @@ -37,8 +37,6 @@ public abstract class GradientFactory

{ public P createLinearGradient(LinearGradientPaint gp, AffineTransform baseTransform, AffineTransform transform) { - List matrix = createGradientTransform(gp, baseTransform, transform); - Point2D startPoint = gp.getStartPoint(); Point2D endPoint = gp.getEndPoint(); List coords = new java.util.ArrayList(4); @@ -46,52 +44,45 @@ public abstract class GradientFactory

{ coords.add(new Double(startPoint.getY())); coords.add(new Double(endPoint.getX())); coords.add(new Double(endPoint.getY())); - - List colors = createGradientColors(gp); - - List bounds = createGradientBounds(gp); - - //Gradients are currently restricted to sRGB - PDFDeviceColorSpace colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB); - return createGradient(false, colSpace, colors, bounds, coords, matrix); + return createGradient(gp, coords, baseTransform, transform); } - public P createRadialGradient(RadialGradientPaint gp, + public P createRadialGradient(RadialGradientPaint gradient, AffineTransform baseTransform, AffineTransform transform) { - List matrix = createGradientTransform(gp, baseTransform, transform); - - double ar = gp.getRadius(); - Point2D ac = gp.getCenterPoint(); - Point2D af = gp.getFocusPoint(); - List theCoords = new java.util.ArrayList(); - double dx = af.getX() - ac.getX(); - double dy = af.getY() - ac.getY(); + double radius = gradient.getRadius(); + Point2D center = gradient.getCenterPoint(); + Point2D focus = gradient.getFocusPoint(); + double dx = focus.getX() - center.getX(); + double dy = focus.getY() - center.getY(); double d = Math.sqrt(dx * dx + dy * dy); - if (d > ar) { - // the center point af must be within the circle with - // radius ar centered at ac so limit it to that. - double scale = (ar * .9999) / d; - dx = dx * scale; - dy = dy * scale; + if (d > radius) { + // The center point must be within the circle with + // radius radius centered at center so limit it to that. + double scale = (radius * .9999) / d; + dx *= scale; + dy *= scale; } + List coords = new java.util.ArrayList(); + coords.add(Double.valueOf(center.getX() + dx)); + coords.add(Double.valueOf(center.getY() + dy)); + coords.add(Double.valueOf(0)); + coords.add(Double.valueOf(center.getX())); + coords.add(Double.valueOf(center.getY())); + coords.add(Double.valueOf(radius)); + return createGradient(gradient, coords, baseTransform, transform); + } - theCoords.add(new Double(ac.getX() + dx)); // Fx - theCoords.add(new Double(ac.getY() + dy)); // Fy - theCoords.add(new Double(0)); - theCoords.add(new Double(ac.getX())); - theCoords.add(new Double(ac.getY())); - theCoords.add(new Double(ar)); - - List colors = createGradientColors(gp); - - List bounds = createGradientBounds(gp); - + private P createGradient(MultipleGradientPaint gradient, List coords, + AffineTransform baseTransform, AffineTransform transform) { + List matrix = createTransform(gradient, baseTransform, transform); + List colors = createColors(gradient); + List bounds = createBounds(gradient); //Gradients are currently restricted to sRGB PDFDeviceColorSpace colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB); - return createGradient(true, colSpace, colors, bounds, theCoords, matrix); + return createGradient(gradient instanceof RadialGradientPaint, colSpace, colors, bounds, coords, matrix); } - private List createGradientTransform(MultipleGradientPaint gradient, + private List createTransform(MultipleGradientPaint gradient, AffineTransform baseTransform, AffineTransform transform) { AffineTransform gradientTransform = new AffineTransform(baseTransform); gradientTransform.concatenate(transform); @@ -105,7 +96,7 @@ public abstract class GradientFactory

{ return matrix; } - private List createGradientColors(MultipleGradientPaint gradient) { + private List createColors(MultipleGradientPaint gradient) { Color[] svgColors = gradient.getColors(); List gradientColors = new ArrayList(svgColors.length + 2); float[] fractions = gradient.getFractions(); @@ -121,7 +112,7 @@ public abstract class GradientFactory

{ return gradientColors; } - private List createGradientBounds(MultipleGradientPaint gradient) { + private List createBounds(MultipleGradientPaint gradient) { // TODO is the conversion to double necessary? float[] fractions = gradient.getFractions(); List bounds = new java.util.ArrayList(fractions.length); -- 2.39.5