]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Factorized gradient creation out of PDF & PS into GradientFactory
authorVincent Hennebert <vhennebert@apache.org>
Thu, 10 Jul 2014 17:57:09 +0000 (17:57 +0000)
committerVincent Hennebert <vhennebert@apache.org>
Thu, 10 Jul 2014 17:57:09 +0000 (17:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1609529 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/render/ps/svg/PSSVGGraphics2D.java
src/java/org/apache/fop/render/shading/GradientFactory.java
src/java/org/apache/fop/render/shading/PDFGradientFactory.java
src/java/org/apache/fop/render/shading/PSGradientFactory.java
src/java/org/apache/fop/svg/PDFGraphics2D.java

index c4f46ede9aed6cc1e0163dd2f81cc7077364bfc7..7e0dcb496094e98787da96a3da52314daa093308 100644 (file)
 
 package org.apache.fop.render.ps.svg;
 
-import java.awt.Color;
 import java.awt.Graphics;
 import java.awt.Paint;
 import java.awt.geom.AffineTransform;
-import java.awt.geom.Point2D;
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import org.apache.batik.ext.awt.LinearGradientPaint;
-import org.apache.batik.ext.awt.MultipleGradientPaint;
 import org.apache.batik.ext.awt.RadialGradientPaint;
 
 import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
 import org.apache.xmlgraphics.ps.PSGenerator;
 
-import org.apache.fop.pdf.PDFDeviceColorSpace;
 import org.apache.fop.render.shading.Function;
 import org.apache.fop.render.shading.GradientRegistrar;
 import org.apache.fop.render.shading.PSGradientFactory;
@@ -79,124 +73,25 @@ public class PSSVGGraphics2D extends PSGraphics2D implements GradientRegistrar {
 
     protected void applyPaint(Paint paint, boolean fill) {
         super.applyPaint(paint, fill);
-        if (paint instanceof RadialGradientPaint) {
-            RadialGradientPaint rgp = (RadialGradientPaint)paint;
+        if (paint instanceof LinearGradientPaint) {
+            Pattern pattern = new PSGradientFactory()
+                    .createLinearGradient((LinearGradientPaint) paint, getBaseTransform(), getTransform());
             try {
-                handleRadialGradient(rgp, gen);
+                gen.write(pattern.toString());
             } catch (IOException ioe) {
                 handleIOException(ioe);
             }
-        } else if (paint instanceof LinearGradientPaint) {
-            LinearGradientPaint lgp = (LinearGradientPaint)paint;
+        } else if (paint instanceof RadialGradientPaint) {
+            Pattern pattern = new PSGradientFactory()
+                    .createRadialGradient((RadialGradientPaint) paint, getBaseTransform(), getTransform());
             try {
-                handleLinearGradient(lgp, gen);
+                gen.write(pattern.toString());
             } catch (IOException ioe) {
                 handleIOException(ioe);
             }
         }
     }
 
-    private void handleLinearGradient(LinearGradientPaint gp, PSGenerator gen) throws IOException {
-        List<Double> matrix = createGradientTransform(gp);
-
-        Point2D startPoint = gp.getStartPoint();
-        Point2D endPoint = gp.getEndPoint();
-        List<Double> coords = new java.util.ArrayList<Double>(4);
-        coords.add(new Double(startPoint.getX()));
-        coords.add(new Double(startPoint.getY()));
-        coords.add(new Double(endPoint.getX()));
-        coords.add(new Double(endPoint.getY()));
-
-        List<Color> colors = createGradientColors(gp);
-
-        List<Double> bounds = createGradientBounds(gp);
-
-        //Gradients are currently restricted to sRGB
-        PDFDeviceColorSpace colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
-        PSGradientFactory gradientFactory = new PSGradientFactory();
-        PSPattern pattern = gradientFactory.createGradient(false, colSpace, colors, bounds, coords, matrix);
-
-        gen.write(pattern.toString());
-    }
-
-    private void handleRadialGradient(RadialGradientPaint gp, PSGenerator gen) throws IOException {
-        List<Double> matrix = createGradientTransform(gp);
-
-        double ar = gp.getRadius();
-        Point2D ac = gp.getCenterPoint();
-        Point2D af = gp.getFocusPoint();
-        List<Double> theCoords = new java.util.ArrayList<Double>();
-        double dx = af.getX() - ac.getX();
-        double dy = af.getY() - ac.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;
-        }
-
-        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<Color> colors = createGradientColors(gp);
-
-        List<Double> bounds = createGradientBounds(gp);
-
-        //Gradients are currently restricted to sRGB
-        PDFDeviceColorSpace colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
-        PSGradientFactory gradientFactory = new PSGradientFactory();
-        PSPattern pattern = gradientFactory.createGradient(true, colSpace, colors, bounds, theCoords, matrix);
-
-        gen.write(pattern.toString());
-    }
-
-    private List<Double> createGradientTransform(MultipleGradientPaint gradient) {
-        AffineTransform transform = new AffineTransform(getBaseTransform());
-        transform.concatenate(getTransform());
-        transform.concatenate(gradient.getTransform());
-        List<Double> matrix = new ArrayList<Double>(6);
-        double[] m = new double[6];
-        transform.getMatrix(m);
-        for (double d : m) {
-            matrix.add(Double.valueOf(d));
-        }
-        return matrix;
-    }
-
-    private List<Color> createGradientColors(MultipleGradientPaint gradient) {
-        Color[] svgColors = gradient.getColors();
-        List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2);
-        float[] fractions = gradient.getFractions();
-        if (fractions[0] > 0f) {
-            gradientColors.add(svgColors[0]);
-        }
-        for (Color c : svgColors) {
-            gradientColors.add(c);
-        }
-        if (fractions[fractions.length - 1] < 1f) {
-            gradientColors.add(svgColors[svgColors.length - 1]);
-        }
-        return gradientColors;
-    }
-
-    private List<Double> createGradientBounds(MultipleGradientPaint gradient) {
-        // TODO is the conversion to double necessary?
-        float[] fractions = gradient.getFractions();
-        List<Double> bounds = new java.util.ArrayList<Double>(fractions.length);
-        for (float offset : fractions) {
-            if (0f < offset && offset < 1f) {
-                bounds.add(Double.valueOf(offset));
-            }
-        }
-        return bounds;
-    }
-
     protected AffineTransform getBaseTransform() {
         AffineTransform at = new AffineTransform(this.getTransform());
         return at;
index 882faa302faa5d6810da1449c62b496b3e7301ef..2809ec8a16148ff4044d2e00505e792888447027 100644 (file)
 package org.apache.fop.render.shading;
 
 import java.awt.Color;
+import java.awt.geom.AffineTransform;
+import java.awt.geom.Point2D;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.batik.ext.awt.LinearGradientPaint;
+import org.apache.batik.ext.awt.MultipleGradientPaint;
+import org.apache.batik.ext.awt.RadialGradientPaint;
+
 import org.apache.xmlgraphics.java2d.color.ColorUtil;
 
 import org.apache.fop.pdf.PDFDeviceColorSpace;
 
-public abstract class GradientFactory {
+public abstract class GradientFactory<P extends Pattern> {
+
+    public P createLinearGradient(LinearGradientPaint gp,
+            AffineTransform baseTransform, AffineTransform transform) {
+        List<Double> matrix = createGradientTransform(gp, baseTransform, transform);
+
+        Point2D startPoint = gp.getStartPoint();
+        Point2D endPoint = gp.getEndPoint();
+        List<Double> coords = new java.util.ArrayList<Double>(4);
+        coords.add(new Double(startPoint.getX()));
+        coords.add(new Double(startPoint.getY()));
+        coords.add(new Double(endPoint.getX()));
+        coords.add(new Double(endPoint.getY()));
+
+        List<Color> colors = createGradientColors(gp);
+
+        List<Double> bounds = createGradientBounds(gp);
+
+        //Gradients are currently restricted to sRGB
+        PDFDeviceColorSpace colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
+        return createGradient(false, colSpace, colors, bounds, coords, matrix);
+    }
+
+    public P createRadialGradient(RadialGradientPaint gp,
+            AffineTransform baseTransform, AffineTransform transform) {
+        List<Double> matrix = createGradientTransform(gp, baseTransform, transform);
+
+        double ar = gp.getRadius();
+        Point2D ac = gp.getCenterPoint();
+        Point2D af = gp.getFocusPoint();
+        List<Double> theCoords = new java.util.ArrayList<Double>();
+        double dx = af.getX() - ac.getX();
+        double dy = af.getY() - ac.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;
+        }
+
+        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<Color> colors = createGradientColors(gp);
+
+        List<Double> bounds = createGradientBounds(gp);
+
+        //Gradients are currently restricted to sRGB
+        PDFDeviceColorSpace colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
+        return createGradient(true, colSpace, colors, bounds, theCoords, matrix);
+    }
+
+    private List<Double> createGradientTransform(MultipleGradientPaint gradient,
+            AffineTransform baseTransform, AffineTransform transform) {
+        AffineTransform gradientTransform = new AffineTransform(baseTransform);
+        gradientTransform.concatenate(transform);
+        gradientTransform.concatenate(gradient.getTransform());
+        List<Double> matrix = new ArrayList<Double>(6);
+        double[] m = new double[6];
+        gradientTransform.getMatrix(m);
+        for (double d : m) {
+            matrix.add(Double.valueOf(d));
+        }
+        return matrix;
+    }
+
+    private List<Color> createGradientColors(MultipleGradientPaint gradient) {
+        Color[] svgColors = gradient.getColors();
+        List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2);
+        float[] fractions = gradient.getFractions();
+        if (fractions[0] > 0f) {
+            gradientColors.add(svgColors[0]);
+        }
+        for (Color c : svgColors) {
+            gradientColors.add(c);
+        }
+        if (fractions[fractions.length - 1] < 1f) {
+            gradientColors.add(svgColors[svgColors.length - 1]);
+        }
+        return gradientColors;
+    }
+
+    private List<Double> createGradientBounds(MultipleGradientPaint gradient) {
+        // TODO is the conversion to double necessary?
+        float[] fractions = gradient.getFractions();
+        List<Double> bounds = new java.util.ArrayList<Double>(fractions.length);
+        for (float offset : fractions) {
+            if (0f < offset && offset < 1f) {
+                bounds.add(Double.valueOf(offset));
+            }
+        }
+        return bounds;
+    }
 
     /**
      * Creates a new gradient
@@ -39,7 +143,7 @@ public abstract class GradientFactory {
      * @param theMatrix The matrix for any transformations
      * @return Returns the Pattern object of the gradient
      */
-    public abstract Pattern createGradient(boolean radial,
+    public abstract P createGradient(boolean radial,
             PDFDeviceColorSpace theColorspace, List<Color> theColors, List<Double> theBounds,
             List<Double> theCoords, List<Double> theMatrix);
 
index c40ac0671622ce8cfa81620f8a42925b85518a82..7254b16690121148c71a21fb80852d27b9b33ad5 100644 (file)
@@ -26,7 +26,7 @@ import org.apache.fop.pdf.PDFPattern;
 import org.apache.fop.pdf.PDFShading;
 import org.apache.fop.svg.PDFGraphics2D;
 
-public class PDFGradientFactory extends GradientFactory {
+public class PDFGradientFactory extends GradientFactory<PDFPattern> {
 
     private final GradientRegistrar registrar;
 
index fd3ebcfd74a31cd924512e63160c51d8ffee75fe..156ace52ab7747d713f20b9a8ad00245e20fcf20 100644 (file)
@@ -25,7 +25,7 @@ import org.apache.fop.render.ps.svg.PSFunction;
 import org.apache.fop.render.ps.svg.PSPattern;
 import org.apache.fop.render.ps.svg.PSShading;
 
-public class PSGradientFactory extends GradientFactory {
+public class PSGradientFactory extends GradientFactory<PSPattern> {
 
     @Override
     public PSPattern createGradient(boolean radial, PDFDeviceColorSpace theColorspace,
index ac6a279f1969ef6ede436783499ea3c59e339fff..01d416767654b9011f8cae091097cddc9154e0a0 100644 (file)
@@ -36,7 +36,6 @@ import java.awt.Stroke;
 import java.awt.color.ColorSpace;
 import java.awt.geom.AffineTransform;
 import java.awt.geom.PathIterator;
-import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 import java.awt.image.BufferedImage;
 import java.awt.image.ColorModel;
@@ -50,7 +49,6 @@ import java.awt.image.renderable.RenderableImage;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.StringWriter;
-import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -819,11 +817,15 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand
                     gpaint.isCyclic() ? LinearGradientPaint.REPEAT : LinearGradientPaint.NO_CYCLE);
         }
         if (paint instanceof LinearGradientPaint && gradientSupported((LinearGradientPaint) paint)) {
-            applyLinearGradient((LinearGradientPaint) paint, fill);
+            PDFPattern pattern = new PDFGradientFactory(this)
+                    .createLinearGradient((LinearGradientPaint) paint, getBaseTransform(), getTransform());
+            currentStream.write(pattern.getColorSpaceOut(fill));
             return true;
         }
         if (paint instanceof RadialGradientPaint && gradientSupported((RadialGradientPaint) paint)) {
-            applyRadialGradient((RadialGradientPaint) paint, fill);
+            PDFPattern pattern = new PDFGradientFactory(this)
+                    .createRadialGradient((RadialGradientPaint) paint, getBaseTransform(), getTransform());
+            currentStream.write(pattern.getColorSpaceOut(fill));
             return true;
         }
         if (paint instanceof PatternPaint) {
@@ -859,109 +861,6 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand
         return (gradient.getCycleMethod() != MultipleGradientPaint.NO_CYCLE);
     }
 
-    private void applyLinearGradient(LinearGradientPaint gp, boolean fill) {
-        List<Double> matrix = createGradientTransform(gp);
-
-        Point2D startPoint = gp.getStartPoint();
-        Point2D endPoint = gp.getEndPoint();
-        List<Double> coords = new java.util.ArrayList<Double>(4);
-        coords.add(new Double(startPoint.getX()));
-        coords.add(new Double(startPoint.getY()));
-        coords.add(new Double(endPoint.getX()));
-        coords.add(new Double(endPoint.getY()));
-
-        List<Color> colors = createGradientColors(gp);
-
-        List<Double> bounds = createGradientBounds(gp);
-
-        //Gradients are currently restricted to sRGB
-        PDFDeviceColorSpace colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
-        PDFGradientFactory gradientFactory = new PDFGradientFactory(this);
-        PDFPattern pattern = gradientFactory.createGradient(false, colSpace, colors, bounds, coords, matrix);
-
-        currentStream.write(pattern.getColorSpaceOut(fill));
-    }
-
-    private void applyRadialGradient(RadialGradientPaint gp, boolean fill) {
-        List<Double> matrix = createGradientTransform(gp);
-
-        double ar = gp.getRadius();
-        Point2D ac = gp.getCenterPoint();
-        Point2D af = gp.getFocusPoint();
-        List<Double> theCoords = new java.util.ArrayList<Double>();
-        double dx = af.getX() - ac.getX();
-        double dy = af.getY() - ac.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;
-        }
-
-        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<Color> colors = createGradientColors(gp);
-
-        List<Double> bounds = createGradientBounds(gp);
-
-        //Gradients are currently restricted to sRGB
-        PDFDeviceColorSpace colSpace = new PDFDeviceColorSpace(PDFDeviceColorSpace.DEVICE_RGB);
-        PDFGradientFactory gradientFactory = new PDFGradientFactory(this);
-        PDFPattern pattern = gradientFactory.createGradient(true, colSpace, colors, bounds, theCoords, matrix);
-
-        currentStream.write(pattern.getColorSpaceOut(fill));
-    }
-
-    private List<Double> createGradientTransform(MultipleGradientPaint gp) {
-        // Build proper transform from gradient space to page space
-        // ('Patterns' don't get userspace transform).
-        AffineTransform transform = new AffineTransform(getBaseTransform());
-        transform.concatenate(getTransform());
-        transform.concatenate(gp.getTransform());
-        List<Double> matrix = new java.util.ArrayList<Double>(6);
-        double[] m = new double[6];
-        transform.getMatrix(m);
-        for (double d : m) {
-            matrix.add(Double.valueOf(d));
-        }
-        return matrix;
-    }
-
-    private List<Color> createGradientColors(MultipleGradientPaint gradient) {
-        Color[] svgColors = gradient.getColors();
-        List<Color> gradientColors = new ArrayList<Color>(svgColors.length + 2);
-        float[] fractions = gradient.getFractions();
-        if (fractions[0] > 0f) {
-            gradientColors.add(svgColors[0]);
-        }
-        for (Color c : svgColors) {
-            gradientColors.add(c);
-        }
-        if (fractions[fractions.length - 1] < 1f) {
-            gradientColors.add(svgColors[svgColors.length - 1]);
-        }
-        return gradientColors;
-    }
-
-    private List<Double> createGradientBounds(MultipleGradientPaint gradient) {
-        // TODO is the conversion to double necessary?
-        float[] fractions = gradient.getFractions();
-        List<Double> bounds = new java.util.ArrayList<Double>(fractions.length);
-        for (float offset : fractions) {
-            if (0f < offset && offset < 1f) {
-                bounds.add(Double.valueOf(offset));
-            }
-        }
-        return bounds;
-    }
-
     private boolean createPattern(PatternPaint pp, boolean fill) {
         preparePainting();