From 290077cc211419f8ddae314657704da1a7204cdc Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Thu, 10 Jul 2014 17:53:22 +0000 Subject: [PATCH] Moved checks for repeated gradient out of gradient-creation methods Will ease factorizing of common code between PDF and PostScript git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1609527 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/render/ps/svg/PSSVGGraphics2D.java | 10 ---- .../org/apache/fop/svg/PDFGraphics2D.java | 56 ++++++++----------- 2 files changed, 23 insertions(+), 43 deletions(-) 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 12a482c94..c4f46ede9 100644 --- a/src/java/org/apache/fop/render/ps/svg/PSSVGGraphics2D.java +++ b/src/java/org/apache/fop/render/ps/svg/PSSVGGraphics2D.java @@ -97,11 +97,6 @@ public class PSSVGGraphics2D extends PSGraphics2D implements GradientRegistrar { } private void handleLinearGradient(LinearGradientPaint gp, PSGenerator gen) throws IOException { - MultipleGradientPaint.CycleMethodEnum cycle = gp.getCycleMethod(); - if (cycle != MultipleGradientPaint.NO_CYCLE) { - return; - } - List matrix = createGradientTransform(gp); Point2D startPoint = gp.getStartPoint(); @@ -125,11 +120,6 @@ public class PSSVGGraphics2D extends PSGraphics2D implements GradientRegistrar { } private void handleRadialGradient(RadialGradientPaint gp, PSGenerator gen) throws IOException { - MultipleGradientPaint.CycleMethodEnum cycle = gp.getCycleMethod(); - if (cycle != MultipleGradientPaint.NO_CYCLE) { - return; - } - List matrix = createGradientTransform(gp); double ar = gp.getRadius(); diff --git a/src/java/org/apache/fop/svg/PDFGraphics2D.java b/src/java/org/apache/fop/svg/PDFGraphics2D.java index d3c72f76e..ac6a279f1 100644 --- a/src/java/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/java/org/apache/fop/svg/PDFGraphics2D.java @@ -818,11 +818,13 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand new Color[] {gpaint.getColor1(), gpaint.getColor2()}, gpaint.isCyclic() ? LinearGradientPaint.REPEAT : LinearGradientPaint.NO_CYCLE); } - if (paint instanceof LinearGradientPaint && !gradientContainsTransparency((LinearGradientPaint) paint)) { - return applyLinearGradient(paint, fill); + if (paint instanceof LinearGradientPaint && gradientSupported((LinearGradientPaint) paint)) { + applyLinearGradient((LinearGradientPaint) paint, fill); + return true; } - if (paint instanceof RadialGradientPaint && !gradientContainsTransparency((RadialGradientPaint) paint)) { - return applyRadialGradient(paint, fill); + if (paint instanceof RadialGradientPaint && gradientSupported((RadialGradientPaint) paint)) { + applyRadialGradient((RadialGradientPaint) paint, fill); + return true; } if (paint instanceof PatternPaint) { PatternPaint pp = (PatternPaint)paint; @@ -831,6 +833,10 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand return false; // unknown paint } + private boolean gradientSupported(MultipleGradientPaint gradient) { + return !(gradientContainsTransparency(gradient) || gradientIsRepeated(gradient)); + } + private boolean gradientContainsTransparency(MultipleGradientPaint gradient) { for (Color color : gradient.getColors()) { if (color.getAlpha() != 255) { @@ -840,20 +846,20 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand return false; } - private boolean applyLinearGradient(Paint paint, boolean fill) { - LinearGradientPaint gp = (LinearGradientPaint)paint; + private boolean gradientIsRepeated(MultipleGradientPaint gradient) { + // For linear gradients it is possible to construct a 'tile' that is repeated with + // a PDF pattern, but it would be very tricky as the coordinate system would have + // to be rotated so the repeat is axially aligned. - // This code currently doesn't support 'repeat'. - // For linear gradients it is possible to construct - // a 'tile' that is repeated with a PDF pattern, but - // it would be very tricky as you would have to rotate - // the coordinate system so the repeat was axially - // aligned. At this point I'm just going to rasterize it. - MultipleGradientPaint.CycleMethodEnum cycle = gp.getCycleMethod(); - if (cycle != MultipleGradientPaint.NO_CYCLE) { - return false; - } + // For radial gradients there is essentially no way to support repeats in PDF (the + // one option would be to 'grow' the outer circle until it fully covers the + // bounds and then grow the stops accordingly, the problem is that this may + // require an extremely large number of stops for cases where the focus is near + // the edge of the outer circle). + return (gradient.getCycleMethod() != MultipleGradientPaint.NO_CYCLE); + } + private void applyLinearGradient(LinearGradientPaint gp, boolean fill) { List matrix = createGradientTransform(gp); Point2D startPoint = gp.getStartPoint(); @@ -874,24 +880,9 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand PDFPattern pattern = gradientFactory.createGradient(false, colSpace, colors, bounds, coords, matrix); currentStream.write(pattern.getColorSpaceOut(fill)); - return true; } - private boolean applyRadialGradient(Paint paint, boolean fill) { - RadialGradientPaint gp = (RadialGradientPaint)paint; - - // There is essentially no way to support repeats - // in PDF for radial gradients (the one option would - // be to 'grow' the outer circle until it fully covered - // the bounds and then grow the stops accordingly, the - // problem is that this may require an extremely large - // number of stops for cases where the focus is near - // the edge of the outer circle). so we rasterize. - MultipleGradientPaint.CycleMethodEnum cycle = gp.getCycleMethod(); - if (cycle != MultipleGradientPaint.NO_CYCLE) { - return false; - } - + private void applyRadialGradient(RadialGradientPaint gp, boolean fill) { List matrix = createGradientTransform(gp); double ar = gp.getRadius(); @@ -926,7 +917,6 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand PDFPattern pattern = gradientFactory.createGradient(true, colSpace, colors, bounds, theCoords, matrix); currentStream.write(pattern.getColorSpaceOut(fill)); - return true; } private List createGradientTransform(MultipleGradientPaint gp) { -- 2.39.5