From 46b7a2ef190c109eda8220ada3b98b5b497b8c88 Mon Sep 17 00:00:00 2001 From: Keiron Liddle Date: Tue, 9 Jul 2002 14:59:03 +0000 Subject: [PATCH] improved linear and radial gradients git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@194990 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/fop/svg/PDFDocumentGraphics2D.java | 3 +- src/org/apache/fop/svg/PDFGraphics2D.java | 62 +++++++++++-------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/src/org/apache/fop/svg/PDFDocumentGraphics2D.java b/src/org/apache/fop/svg/PDFDocumentGraphics2D.java index 03cd602ea..3894d1140 100644 --- a/src/org/apache/fop/svg/PDFDocumentGraphics2D.java +++ b/src/org/apache/fop/svg/PDFDocumentGraphics2D.java @@ -20,6 +20,7 @@ import java.awt.Color; import java.awt.Shape; import java.awt.font.FontRenderContext; import java.awt.font.GlyphVector; +import java.awt.geom.AffineTransform; import java.io.OutputStream; import java.io.IOException; @@ -92,7 +93,7 @@ public class PDFDocumentGraphics2D extends PDFGraphics2D { resourceContext = currentPage; pageRef = currentPage.referencePDF(); currentStream.write("1 0 0 -1 0 " + height + " cm\n"); - + graphicsState.setTransform(new AffineTransform(1.0, 0.0, 0.0, -1.0, 0.0, (double)height)); pdfDoc.outputHeader(stream); setOutputStream(stream); diff --git a/src/org/apache/fop/svg/PDFGraphics2D.java b/src/org/apache/fop/svg/PDFGraphics2D.java index 72686fb68..98736e5ab 100644 --- a/src/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/org/apache/fop/svg/PDFGraphics2D.java @@ -639,10 +639,16 @@ public class PDFGraphics2D extends AbstractGraphics2D { if (paint instanceof LinearGradientPaint) { LinearGradientPaint gp = (LinearGradientPaint)paint; Color[] cols = gp.getColors(); + float[] fractions = gp.getFractions(); Point2D p1 = gp.getStartPoint(); Point2D p2 = gp.getEndPoint(); MultipleGradientPaint.CycleMethodEnum cycenum = gp.getCycleMethod(); boolean cyclic = cycenum == MultipleGradientPaint.REPEAT; + AffineTransform transform = graphicsState.getTransform(); + transform.concatenate(gp.getTransform()); + + p1 = transform.transform(p1, null); + p2 = transform.transform(p2, null); ArrayList theCoords = new ArrayList(); theCoords.add(new Double(p1.getX())); @@ -665,44 +671,51 @@ public class PDFGraphics2D extends AbstractGraphics2D { theEncode.add(new Double(1)); ArrayList theBounds = new ArrayList(); - theBounds.add(new Double(0)); - theBounds.add(new Double(1)); - - ArrayList theFunctions = new ArrayList(); ArrayList someColors = new ArrayList(); - Color c1 = cols[0]; - Color c2 = cols[1]; - - PDFColor color1 = new PDFColor(c1.getRed(), c1.getGreen(), - c1.getBlue()); - someColors.add(color1); - PDFColor color2 = new PDFColor(c2.getRed(), c2.getGreen(), - c2.getBlue()); - someColors.add(color2); - - PDFFunction myfunc = this.pdfDoc.makeFunction(2, theDomain, null, - color1.getVector(), color2.getVector(), 1.0); + for(int count = 0; count < cols.length; count++) { + Color c1 = cols[count]; + PDFColor color1 = new PDFColor(c1.getRed(), c1.getGreen(), + c1.getBlue()); + someColors.add(color1); + if(count > 0 && count < cols.length - 1) { + theBounds.add(new Double(fractions[count])); + } + } PDFColorSpace aColorSpace = new PDFColorSpace(PDFColorSpace.DEVICE_RGB); - PDFPattern myPat = this.pdfDoc.createGradient(resourceContext, false, aColorSpace, - someColors, null, theCoords); + PDFPattern myPat = pdfDoc.createGradient(resourceContext, false, aColorSpace, + someColors, theBounds, theCoords); currentStream.write(myPat.getColorSpaceOut(fill)); } else if (paint instanceof RadialGradientPaint) { RadialGradientPaint rgp = (RadialGradientPaint)paint; - float ar = rgp.getRadius(); + double ar = rgp.getRadius(); Point2D ac = rgp.getCenterPoint(); Point2D af = rgp.getFocusPoint(); + AffineTransform transform = graphicsState.getTransform(); + AffineTransform gradt = rgp.getTransform(); + transform.concatenate(gradt); + + // find largest scaling for the radius + double scale = gradt.getScaleX(); + if(gradt.getScaleY() > scale) { + scale = gradt.getScaleY(); + } + ar = ar * scale; + ac = transform.transform(ac, null); + af = transform.transform(af, null); ArrayList theCoords = new ArrayList(); - theCoords.add( new Double(currentXPosition + ac.getX())); - theCoords.add( new Double(currentYPosition - ac.getY())); + // the center point af must be within the circle with + // radius ar centered at ac + theCoords.add(new Double(af.getX())); + theCoords.add(new Double(af.getY())); theCoords.add(new Double(0)); - theCoords.add( new Double(currentXPosition + af.getX())); // Fx - theCoords.add(new Double(currentYPosition - af.getY())); // Fy + theCoords.add(new Double(ac.getX())); // Fx + theCoords.add(new Double(ac.getY())); // Fy theCoords.add(new Double(ar)); Color[] cols = rgp.getColors(); @@ -713,11 +726,8 @@ public class PDFGraphics2D extends AbstractGraphics2D { float[] fractions = rgp.getFractions(); ArrayList theBounds = new ArrayList(); - float lastoffset = 0; for(int count = 1; count < fractions.length - 1; count++) { float offset = fractions[count]; - // create bounds from last to offset - lastoffset = offset; theBounds.add(new Double(offset)); } PDFColorSpace colSpace = new PDFColorSpace(PDFColorSpace.DEVICE_RGB); -- 2.39.5