From 41cfa73d4b08d30009ecfafb9ece19c712296539 Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Mon, 14 Jul 2014 21:21:35 +0000 Subject: [PATCH] Moved stuff that is common to PDF and PS to Pattern Removed PSPattern class and no longer necessary PDF and PS sub-classes of GradientMaker git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1610532 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/pdf/PDFPattern.java | 4 +- .../fop/render/pdf/svg/PDFGradientMaker.java | 54 ------- .../fop/render/ps/svg/PSGradientMaker.java | 31 ---- .../apache/fop/render/ps/svg/PSPattern.java | 139 ------------------ .../fop/render/ps/svg/PSSVGGraphics2D.java | 69 ++++++++- .../fop/render/shading/GradientMaker.java | 24 +-- .../apache/fop/render/shading/Pattern.java | 31 +++- .../org/apache/fop/svg/PDFGraphics2D.java | 35 ++++- 8 files changed, 134 insertions(+), 253 deletions(-) delete mode 100644 src/java/org/apache/fop/render/pdf/svg/PDFGradientMaker.java delete mode 100644 src/java/org/apache/fop/render/ps/svg/PSGradientMaker.java delete mode 100644 src/java/org/apache/fop/render/ps/svg/PSPattern.java diff --git a/src/java/org/apache/fop/pdf/PDFPattern.java b/src/java/org/apache/fop/pdf/PDFPattern.java index a449c8f60..49f1a0d0b 100644 --- a/src/java/org/apache/fop/pdf/PDFPattern.java +++ b/src/java/org/apache/fop/pdf/PDFPattern.java @@ -23,8 +23,6 @@ import java.io.IOException; import java.io.OutputStream; import java.util.List; -import org.apache.fop.render.shading.Pattern; - /** * class representing a PDF Function. * @@ -35,7 +33,7 @@ import org.apache.fop.render.shading.Pattern; * * All PDF Functions have a FunctionType (0,2,3, or 4), a Domain, and a Range. */ -public class PDFPattern extends PDFPathPaint implements Pattern { +public class PDFPattern extends PDFPathPaint { /** * The resources associated with this pattern diff --git a/src/java/org/apache/fop/render/pdf/svg/PDFGradientMaker.java b/src/java/org/apache/fop/render/pdf/svg/PDFGradientMaker.java deleted file mode 100644 index f34a61d19..000000000 --- a/src/java/org/apache/fop/render/pdf/svg/PDFGradientMaker.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.fop.render.pdf.svg; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.fop.pdf.PDFFunction; -import org.apache.fop.pdf.PDFPattern; -import org.apache.fop.pdf.PDFShading; -import org.apache.fop.render.shading.Function; -import org.apache.fop.render.shading.GradientMaker; -import org.apache.fop.render.shading.Shading; -import org.apache.fop.svg.PDFGraphics2D; - -public class PDFGradientMaker extends GradientMaker { - - private final PDFGraphics2D graphics2D; - - public PDFGradientMaker(PDFGraphics2D pdfGraphics2D) { - this.graphics2D = pdfGraphics2D; - } - - @Override - protected PDFPattern makePattern(int patternType, Shading shading, List matrix) { - Function function = shading.getFunction(); - List pdfFunctions = new ArrayList(function.getFunctions().size()); - for (Function f : function.getFunctions()) { - pdfFunctions.add(graphics2D.registerFunction(new PDFFunction(f))); - } - PDFFunction pdfFunction = graphics2D.registerFunction(new PDFFunction(function, pdfFunctions)); - PDFShading pdfShading = new PDFShading(shading.getShadingType(), shading.getColorSpace(), shading.getCoords(), - pdfFunction); - pdfShading = graphics2D.registerShading(pdfShading); - PDFPattern pattern = new PDFPattern(patternType, pdfShading, null, null, matrix); - return graphics2D.registerPattern(pattern); - } - -} diff --git a/src/java/org/apache/fop/render/ps/svg/PSGradientMaker.java b/src/java/org/apache/fop/render/ps/svg/PSGradientMaker.java deleted file mode 100644 index 5c75fdbed..000000000 --- a/src/java/org/apache/fop/render/ps/svg/PSGradientMaker.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.fop.render.ps.svg; - -import java.util.List; - -import org.apache.fop.render.shading.GradientMaker; -import org.apache.fop.render.shading.Shading; - -public class PSGradientMaker extends GradientMaker { - - @Override - protected PSPattern makePattern(int patternType, Shading shading, List matrix) { - return new PSPattern(patternType, shading, null, null, matrix); - } -} diff --git a/src/java/org/apache/fop/render/ps/svg/PSPattern.java b/src/java/org/apache/fop/render/ps/svg/PSPattern.java deleted file mode 100644 index c9ffbd7c6..000000000 --- a/src/java/org/apache/fop/render/ps/svg/PSPattern.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* $Id$ */ - -package org.apache.fop.render.ps.svg; - -import java.util.ArrayList; -import java.util.List; - -import org.apache.fop.render.shading.Function; -import org.apache.fop.render.shading.Pattern; -import org.apache.fop.render.shading.Shading; - -public class PSPattern implements Pattern { - - /** - * Either one (1) for tiling, or two (2) for shading. - */ - protected int patternType = 2; // Default - - /** - * The Shading object comprising the Type 2 pattern - */ - protected Shading shading; - - /** - * List of Integers represetning the Extended unique Identifier - */ - protected List xUID = null; - - /** - * TODO use PDFGState - * String representing the extended Graphics state. - * Probably will never be used like this. - */ - 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 - * @param theShading The shading object to determine how the gradient - * is drawn - * @param theXUID The XUID - * @param theExtGState The exit state - */ - public PSPattern(int thePatternType, Shading theShading, List theXUID, - StringBuffer theExtGState, List matrix) { - this.patternType = 2; // thePatternType; - this.shading = theShading; - this.xUID = theXUID; - this.extGState = theExtGState; // always null - this.matrix = matrix; - } - - /** - * Outputs the radial or axial pattern as a string dictionary to insert - * into a postscript document. - */ - public String toString() { - int vectorSize = 0; - int tempInt = 0; - StringBuilder p = new StringBuilder(64); - p.append("/Pattern setcolorspace\n"); - p.append("<< \n/Type /Pattern \n"); - - p.append("/PatternType " + this.patternType + " \n"); - - if (this.shading != null) { - p.append("/Shading "); - outputShading(p); - p.append(" \n"); - } - - if (this.xUID != null) { - vectorSize = this.xUID.size(); - p.append("/XUID [ "); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append((this.xUID.get(tempInt)) + " "); - } - p.append("] \n"); - } - - if (this.extGState != null) { - p.append("/ExtGState " + this.extGState + " \n"); - } - - p.append(">> \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(); - } - - private void outputShading(StringBuilder p) { - final Function function = shading.getFunction(); - Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() { - - public void outputFunction(StringBuilder out) { - List functionsStrings = new ArrayList(function.getFunctions().size()); - for (Function f : function.getFunctions()) { - functionsStrings.add(functionToString(f)); - } - out.append(function.toWriteableString(functionsStrings)); - } - }; - shading.output(p, functionRenderer); - } - - private String functionToString(Function function) { - List functionsStrings = new ArrayList(function.getFunctions().size()); - for (Function f : function.getFunctions()) { - functionsStrings.add(functionToString(f)); - } - return function.toWriteableString(functionsStrings); - } - -} 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 ebc7ef8a0..9f428dca6 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,8 @@ import java.awt.Graphics; import java.awt.Paint; import java.awt.geom.AffineTransform; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -33,7 +35,10 @@ import org.apache.batik.ext.awt.RadialGradientPaint; import org.apache.xmlgraphics.java2d.ps.PSGraphics2D; import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.fop.render.shading.Function; +import org.apache.fop.render.shading.GradientMaker; import org.apache.fop.render.shading.Pattern; +import org.apache.fop.render.shading.Shading; public class PSSVGGraphics2D extends PSGraphics2D { @@ -70,24 +75,76 @@ public class PSSVGGraphics2D extends PSGraphics2D { protected void applyPaint(Paint paint, boolean fill) { super.applyPaint(paint, fill); if (paint instanceof LinearGradientPaint) { - Pattern pattern = new PSGradientMaker() - .makeLinearGradient((LinearGradientPaint) paint, getBaseTransform(), getTransform()); + Pattern pattern = GradientMaker.makeLinearGradient((LinearGradientPaint) paint, + getBaseTransform(), getTransform()); try { - gen.write(pattern.toString()); + gen.write(toString(pattern)); } catch (IOException ioe) { handleIOException(ioe); } } else if (paint instanceof RadialGradientPaint) { - Pattern pattern = new PSGradientMaker() - .makeRadialGradient((RadialGradientPaint) paint, getBaseTransform(), getTransform()); + Pattern pattern = GradientMaker.makeRadialGradient((RadialGradientPaint) paint, + getBaseTransform(), getTransform()); try { - gen.write(pattern.toString()); + gen.write(toString(pattern)); } catch (IOException ioe) { handleIOException(ioe); } } } + /** + * Outputs the radial or axial pattern as a string dictionary to insert + * into a postscript document. + * @param pattern + */ + private String toString(Pattern pattern) { + StringBuilder p = new StringBuilder(64); + p.append("/Pattern setcolorspace\n"); + p.append("<< \n/Type /Pattern \n"); + + p.append("/PatternType " + pattern.getPatternType() + " \n"); + + if (pattern.getShading() != null) { + p.append("/Shading "); + outputShading(p, pattern.getShading()); + p.append(" \n"); + } + p.append(">> \n"); + p.append("[ "); + for (double m : pattern.getMatrix()) { + 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(); + } + + private void outputShading(StringBuilder p, Shading shading) { + final Function function = shading.getFunction(); + Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() { + + public void outputFunction(StringBuilder out) { + List functionsStrings = new ArrayList(function.getFunctions().size()); + for (Function f : function.getFunctions()) { + functionsStrings.add(functionToString(f)); + } + out.append(function.toWriteableString(functionsStrings)); + } + }; + shading.output(p, functionRenderer); + } + + private String functionToString(Function function) { + List functionsStrings = new ArrayList(function.getFunctions().size()); + for (Function f : function.getFunctions()) { + functionsStrings.add(functionToString(f)); + } + return function.toWriteableString(functionsStrings); + } + protected AffineTransform getBaseTransform() { AffineTransform at = new AffineTransform(this.getTransform()); return at; diff --git a/src/java/org/apache/fop/render/shading/GradientMaker.java b/src/java/org/apache/fop/render/shading/GradientMaker.java index 99afe1f08..d9c55ea75 100644 --- a/src/java/org/apache/fop/render/shading/GradientMaker.java +++ b/src/java/org/apache/fop/render/shading/GradientMaker.java @@ -33,9 +33,11 @@ import org.apache.xmlgraphics.java2d.color.ColorUtil; import org.apache.fop.pdf.PDFDeviceColorSpace; -public abstract class GradientMaker

{ +public final class GradientMaker { - public P makeLinearGradient(LinearGradientPaint gp, + private GradientMaker() { } + + public static Pattern makeLinearGradient(LinearGradientPaint gp, AffineTransform baseTransform, AffineTransform transform) { Point2D startPoint = gp.getStartPoint(); Point2D endPoint = gp.getEndPoint(); @@ -47,7 +49,7 @@ public abstract class GradientMaker

{ return makeGradient(gp, coords, baseTransform, transform); } - public P makeRadialGradient(RadialGradientPaint gradient, + public static Pattern makeRadialGradient(RadialGradientPaint gradient, AffineTransform baseTransform, AffineTransform transform) { double radius = gradient.getRadius(); Point2D center = gradient.getCenterPoint(); @@ -72,7 +74,7 @@ public abstract class GradientMaker

{ return makeGradient(gradient, coords, baseTransform, transform); } - private P makeGradient(MultipleGradientPaint gradient, List coords, + private static Pattern makeGradient(MultipleGradientPaint gradient, List coords, AffineTransform baseTransform, AffineTransform transform) { List matrix = makeTransform(gradient, baseTransform, transform); List bounds = makeBounds(gradient); @@ -82,10 +84,10 @@ public abstract class GradientMaker

{ Function function = new Function(3, null, null, functions, bounds, null); int shadingType = gradient instanceof LinearGradientPaint ? 2 : 3; Shading shading = new Shading(shadingType, colorSpace, coords, function); - return makePattern(2, shading, matrix); + return new Pattern(2, shading, matrix); } - private List makeTransform(MultipleGradientPaint gradient, + private static List makeTransform(MultipleGradientPaint gradient, AffineTransform baseTransform, AffineTransform transform) { AffineTransform gradientTransform = new AffineTransform(baseTransform); gradientTransform.concatenate(transform); @@ -99,13 +101,13 @@ public abstract class GradientMaker

{ return matrix; } - private Color getsRGBColor(Color c) { + private static Color getsRGBColor(Color c) { // Color space must be consistent, so convert to sRGB if necessary // TODO really? return c.getColorSpace().isCS_sRGB() ? c : ColorUtil.toSRGBColor(c); } - private List makeBounds(MultipleGradientPaint gradient) { + private static List makeBounds(MultipleGradientPaint gradient) { // TODO is the conversion to double necessary? float[] fractions = gradient.getFractions(); List bounds = new java.util.ArrayList(fractions.length); @@ -117,7 +119,7 @@ public abstract class GradientMaker

{ return bounds; } - private List makeFunctions(MultipleGradientPaint gradient) { + private static List makeFunctions(MultipleGradientPaint gradient) { List colors = makeColors(gradient); List functions = new ArrayList(); for (int currentPosition = 0, lastPosition = colors.size() - 1; @@ -133,7 +135,7 @@ public abstract class GradientMaker

{ return functions; } - private List makeColors(MultipleGradientPaint gradient) { + private static List makeColors(MultipleGradientPaint gradient) { Color[] svgColors = gradient.getColors(); List gradientColors = new ArrayList(svgColors.length + 2); float[] fractions = gradient.getFractions(); @@ -149,6 +151,4 @@ public abstract class GradientMaker

{ return gradientColors; } - protected abstract P makePattern(int patternType, Shading shading, List matrix); - } diff --git a/src/java/org/apache/fop/render/shading/Pattern.java b/src/java/org/apache/fop/render/shading/Pattern.java index b66926e53..91101a324 100644 --- a/src/java/org/apache/fop/render/shading/Pattern.java +++ b/src/java/org/apache/fop/render/shading/Pattern.java @@ -17,6 +17,35 @@ package org.apache.fop.render.shading; -public interface Pattern { +import java.util.List; + +public class Pattern { + + private final int patternType; + + private final Shading shading; + + private final List matrix; + + Pattern(int patternType, Shading shading, List matrix) { + this.patternType = patternType; + this.shading = shading; + this.matrix = matrix; + } + + /** + * Either one (1) for tiling, or two (2) for shading. + */ + public int getPatternType() { + return patternType; + } + + public Shading getShading() { + return shading; + } + + public List getMatrix() { + return matrix; + } } diff --git a/src/java/org/apache/fop/svg/PDFGraphics2D.java b/src/java/org/apache/fop/svg/PDFGraphics2D.java index 0e4134d95..359c1c0aa 100644 --- a/src/java/org/apache/fop/svg/PDFGraphics2D.java +++ b/src/java/org/apache/fop/svg/PDFGraphics2D.java @@ -49,6 +49,7 @@ 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; @@ -95,7 +96,10 @@ import org.apache.fop.pdf.PDFXObject; import org.apache.fop.render.pdf.ImageRawCCITTFaxAdapter; import org.apache.fop.render.pdf.ImageRawJPEGAdapter; import org.apache.fop.render.pdf.ImageRenderedAdapter; -import org.apache.fop.render.pdf.svg.PDFGradientMaker; +import org.apache.fop.render.shading.Function; +import org.apache.fop.render.shading.GradientMaker; +import org.apache.fop.render.shading.Pattern; +import org.apache.fop.render.shading.Shading; /** *

PDF Graphics 2D. @@ -813,15 +817,17 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand gpaint.isCyclic() ? LinearGradientPaint.REPEAT : LinearGradientPaint.NO_CYCLE); } if (paint instanceof LinearGradientPaint && gradientSupported((LinearGradientPaint) paint)) { - PDFPattern pattern = new PDFGradientMaker(this) - .makeLinearGradient((LinearGradientPaint) paint, getBaseTransform(), getTransform()); - currentStream.write(pattern.getColorSpaceOut(fill)); + Pattern pattern = GradientMaker.makeLinearGradient((LinearGradientPaint) paint, + getBaseTransform(), getTransform()); + PDFPattern pdfPattern = createPDFPattern(pattern); + currentStream.write(pdfPattern.getColorSpaceOut(fill)); return true; } if (paint instanceof RadialGradientPaint && gradientSupported((RadialGradientPaint) paint)) { - PDFPattern pattern = new PDFGradientMaker(this) - .makeRadialGradient((RadialGradientPaint) paint, getBaseTransform(), getTransform()); - currentStream.write(pattern.getColorSpaceOut(fill)); + Pattern pattern = GradientMaker.makeRadialGradient((RadialGradientPaint) paint, + getBaseTransform(), getTransform()); + PDFPattern pdfPattern = createPDFPattern(pattern); + currentStream.write(pdfPattern.getColorSpaceOut(fill)); return true; } if (paint instanceof PatternPaint) { @@ -831,6 +837,21 @@ public class PDFGraphics2D extends AbstractGraphics2D implements NativeImageHand return false; // unknown paint } + PDFPattern createPDFPattern(Pattern pattern) { + Shading shading = pattern.getShading(); + Function function = shading.getFunction(); + List pdfFunctions = new ArrayList(function.getFunctions().size()); + for (Function f : function.getFunctions()) { + pdfFunctions.add(registerFunction(new PDFFunction(f))); + } + PDFFunction pdfFunction = registerFunction(new PDFFunction(function, pdfFunctions)); + PDFShading pdfShading = new PDFShading(shading.getShadingType(), shading.getColorSpace(), shading.getCoords(), + pdfFunction); + pdfShading = registerShading(pdfShading); + PDFPattern pdfPattern = new PDFPattern(pattern.getPatternType(), pdfShading, null, null, pattern.getMatrix()); + return registerPattern(pdfPattern); + } + private boolean gradientSupported(MultipleGradientPaint gradient) { return !(gradientContainsTransparency(gradient) || gradientIsRepeated(gradient)); } -- 2.39.5