From 512823a2da8362390bd8a48c050b3ea389833454 Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Thu, 10 Jul 2014 17:06:13 +0000 Subject: [PATCH] Fixed gradients in PostScript output Linear gradients were not rendered properly in non-square shapes Gradial gradients don't need to be rasterized in ellipses git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1609508 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/render/ps/PSImageHandlerSVG.java | 15 ------ .../apache/fop/render/ps/svg/PSPattern.java | 13 ++++- .../fop/render/ps/svg/PSSVGGraphics2D.java | 53 +++++++++---------- .../fop/render/shading/PSGradientFactory.java | 2 +- 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/src/java/org/apache/fop/render/ps/PSImageHandlerSVG.java b/src/java/org/apache/fop/render/ps/PSImageHandlerSVG.java index 4d215926b..be479ec31 100644 --- a/src/java/org/apache/fop/render/ps/PSImageHandlerSVG.java +++ b/src/java/org/apache/fop/render/ps/PSImageHandlerSVG.java @@ -33,8 +33,6 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javax.imageio.ImageIO; @@ -314,19 +312,6 @@ public class PSImageHandlerSVG implements ImageHandler { } } String nodeName = curNode.getLocalName(); - //Special case where rasterization needed for radial gradients - if (nodeName != null && nodeName.equals("ellipse")) { - String found = ""; - String ellipseFill = curNode.getAttributes().getNamedItem("fill").getNodeValue(); - Pattern pattern = Pattern.compile("#(.*?)\\)"); - Matcher matcher = pattern.matcher(ellipseFill); - if (matcher.find()) { - found = matcher.group(1); - } - if (gradientsFound.get(found) != null) { - return true; - } - } boolean inMatch = false; if (!isMatched) { inMatch = nodeName != null && gradMatches.contains(nodeName); diff --git a/src/java/org/apache/fop/render/ps/svg/PSPattern.java b/src/java/org/apache/fop/render/ps/svg/PSPattern.java index 46f976672..d29c84674 100644 --- a/src/java/org/apache/fop/render/ps/svg/PSPattern.java +++ b/src/java/org/apache/fop/render/ps/svg/PSPattern.java @@ -48,6 +48,8 @@ public class PSPattern implements Pattern { */ protected StringBuffer extGState = null; + private final List matrix; + /** * Creates a radial or axial shading pattern * @param thePatternType The pattern type which will be 3 for radial and 2 for axial @@ -57,12 +59,13 @@ public class PSPattern implements Pattern { * @param theExtGState The exit state */ public PSPattern(int thePatternType, Shading theShading, List theXUID, - StringBuffer theExtGState) { + StringBuffer theExtGState, List matrix) { this.patternType = 2; // thePatternType; assert theShading instanceof PSShading; this.shading = (PSShading)theShading; this.xUID = theXUID; this.extGState = theExtGState; // always null + this.matrix = matrix; } /** @@ -96,7 +99,13 @@ public class PSPattern implements Pattern { } p.append(">> \n"); - p.append("matrix makepattern setcolor\n"); + p.append("[ "); + for (double m : matrix) { + p.append(Double.toString(m)); // TODO refactor so that PSGenerator.formatDouble can be used + p.append(" "); + } + p.append("] "); + p.append("makepattern setcolor\n"); return p.toString(); } diff --git a/src/java/org/apache/fop/render/ps/svg/PSSVGGraphics2D.java b/src/java/org/apache/fop/render/ps/svg/PSSVGGraphics2D.java index 5aca45a10..4f897dad6 100644 --- a/src/java/org/apache/fop/render/ps/svg/PSSVGGraphics2D.java +++ b/src/java/org/apache/fop/render/ps/svg/PSSVGGraphics2D.java @@ -23,6 +23,7 @@ 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; @@ -117,20 +118,10 @@ public class PSSVGGraphics2D extends PSGraphics2D implements GradientRegistrar { List theCoords = new java.util.ArrayList(); - - - AffineTransform start = applyTransform(lgp.getTransform(), - lgp.getStartPoint().getX(), lgp.getStartPoint().getY()); - AffineTransform end = applyTransform(lgp.getTransform(), lgp.getEndPoint().getX(), lgp.getEndPoint().getY()); - double startX = start.getTranslateX(); - double startY = start.getTranslateY(); - double endX = end.getTranslateX(); - double endY = end.getTranslateY(); - - theCoords.add(startX); - theCoords.add(startY); - theCoords.add(endX); - theCoords.add(endY); + theCoords.add(lgp.getStartPoint().getX()); + theCoords.add(lgp.getStartPoint().getX()); + theCoords.add(lgp.getEndPoint().getX()); + theCoords.add(lgp.getEndPoint().getY()); List someColors = new java.util.ArrayList(); @@ -182,12 +173,6 @@ public class PSSVGGraphics2D extends PSGraphics2D implements GradientRegistrar { rgp.getCenterPoint().getX(), rgp.getCenterPoint().getY()); AffineTransform resultFocus = applyTransform(rgp.getTransform(), rgp.getFocusPoint().getX(), rgp.getFocusPoint().getY()); - double scale = Math.sqrt(rgp.getTransform().getDeterminant()); - double radius = rgp.getRadius() * scale; - double centreX = resultCentre.getTranslateX(); - double centreY = resultCentre.getTranslateY(); - double focusX = resultFocus.getTranslateX(); - double focusY = resultFocus.getTranslateY(); List theMatrix = new java.util.ArrayList(); double [] mat = new double[6]; @@ -196,15 +181,29 @@ public class PSSVGGraphics2D extends PSGraphics2D implements GradientRegistrar { theMatrix.add(Double.valueOf(mat[idx])); } - List theCoords = new java.util.ArrayList(); float[] fractions = rgp.getFractions(); - theCoords.add(centreX); - theCoords.add(centreY); - theCoords.add(0d); - theCoords.add(focusX); - theCoords.add(focusY); - theCoords.add(radius); + double ar = rgp.getRadius(); + Point2D ac = rgp.getCenterPoint(); + Point2D af = rgp.getFocusPoint(); + List theCoords = new java.util.ArrayList(); + 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)); Color[] cols = rgp.getColors(); List someColors = new java.util.ArrayList(); diff --git a/src/java/org/apache/fop/render/shading/PSGradientFactory.java b/src/java/org/apache/fop/render/shading/PSGradientFactory.java index cd47de93a..fd3ebcfd7 100644 --- a/src/java/org/apache/fop/render/shading/PSGradientFactory.java +++ b/src/java/org/apache/fop/render/shading/PSGradientFactory.java @@ -65,6 +65,6 @@ public class PSGradientFactory extends GradientFactory { @Override public Pattern makePattern(int thePatternType, Shading theShading, List theXUID, StringBuffer theExtGState, List theMatrix) { - return new PSPattern(thePatternType, theShading, theXUID, theExtGState); + return new PSPattern(thePatternType, theShading, theXUID, theExtGState, theMatrix); } } -- 2.39.5