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;
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;
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
* @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);
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;
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,
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;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
-import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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) {
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();