From decb63c9dff4fa1dcf65b9cc3faaa4bf54050301 Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Fri, 11 Jul 2014 18:05:02 +0000 Subject: [PATCH] Moved content of ShadingPattern into Shading git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1609760 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/pdf/PDFShading.java | 21 +- .../apache/fop/render/ps/svg/PSPattern.java | 16 +- .../apache/fop/render/shading/Shading.java | 167 +++++++++++ .../fop/render/shading/ShadingPattern.java | 270 ------------------ 4 files changed, 179 insertions(+), 295 deletions(-) delete mode 100644 src/java/org/apache/fop/render/shading/ShadingPattern.java diff --git a/src/java/org/apache/fop/pdf/PDFShading.java b/src/java/org/apache/fop/pdf/PDFShading.java index a262f4d29..a45c14e66 100644 --- a/src/java/org/apache/fop/pdf/PDFShading.java +++ b/src/java/org/apache/fop/pdf/PDFShading.java @@ -21,10 +21,7 @@ package org.apache.fop.pdf; import java.util.List; -import org.apache.fop.render.shading.Function; import org.apache.fop.render.shading.Shading; -import org.apache.fop.render.shading.ShadingPattern; -import org.apache.fop.render.shading.ShadingPattern.ShadingRenderer; /** @@ -98,19 +95,15 @@ public class PDFShading extends PDFObject { * @return the PDF string. */ public String toPDFString() { - final ShadingRenderer shadingRenderer = new ShadingRenderer() { - - public void outputFunction(StringBuffer out) { - Function function = shading.getFunction(); - if (function != null) { - out.append("/Function "); - out.append(pdfFunction.referencePDF() + " \n"); - } + Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() { + + public void outputFunction(StringBuilder out) { + out.append(pdfFunction.referencePDF()); } }; - ShadingPattern pattern = new ShadingPattern(shading, shadingRenderer); - return pattern.toString(shading.getColorSpace(), shading.getShadingType(), shading.getBackground(), - shading.getBBox(), shading.isAntiAlias()); + StringBuilder out = new StringBuilder(); + shading.output(out, functionRenderer); + return out.toString(); } /** {@inheritDoc} */ 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 3418a5740..7c28ffcf7 100644 --- a/src/java/org/apache/fop/render/ps/svg/PSPattern.java +++ b/src/java/org/apache/fop/render/ps/svg/PSPattern.java @@ -26,8 +26,6 @@ import org.apache.fop.render.shading.Function; import org.apache.fop.render.shading.FunctionPattern; import org.apache.fop.render.shading.Pattern; import org.apache.fop.render.shading.Shading; -import org.apache.fop.render.shading.ShadingPattern; -import org.apache.fop.render.shading.ShadingPattern.ShadingRenderer; public class PSPattern implements Pattern { @@ -79,7 +77,7 @@ public class PSPattern implements Pattern { public String toString() { int vectorSize = 0; int tempInt = 0; - StringBuffer p = new StringBuffer(64); + StringBuilder p = new StringBuilder(64); p.append("/Pattern setcolorspace\n"); p.append("<< \n/Type /Pattern \n"); @@ -116,24 +114,20 @@ public class PSPattern implements Pattern { return p.toString(); } - private void outputShading(StringBuffer out) { + private void outputShading(StringBuilder p) { final Function function = shading.getFunction(); - final ShadingRenderer shadingRenderer = new ShadingRenderer() { + Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() { - public void outputFunction(StringBuffer out) { - out.append("/Function "); + public void outputFunction(StringBuilder out) { FunctionPattern pattern = new FunctionPattern(function); List functionsStrings = new ArrayList(function.getFunctions().size()); for (Function f : function.getFunctions()) { functionsStrings.add(functionToString(f)); } out.append(pattern.toWriteableString(functionsStrings)); - out.append("\n"); } }; - ShadingPattern pattern = new ShadingPattern(shading, shadingRenderer); - out.append(pattern.toString(shading.getColorSpace(), shading.getShadingType(), shading.getBackground(), - shading.getBBox(), shading.isAntiAlias())); + shading.output(p, functionRenderer); } private String functionToString(Function function) { diff --git a/src/java/org/apache/fop/render/shading/Shading.java b/src/java/org/apache/fop/render/shading/Shading.java index e27b61a7d..89b4added 100644 --- a/src/java/org/apache/fop/render/shading/Shading.java +++ b/src/java/org/apache/fop/render/shading/Shading.java @@ -20,10 +20,16 @@ package org.apache.fop.render.shading; import java.util.List; import org.apache.fop.pdf.PDFDeviceColorSpace; +import org.apache.fop.pdf.PDFNumber; public class Shading { + public interface FunctionRenderer { + + void outputFunction(StringBuilder out); + } + /** * Required: The Type of shading (1,2,3,4,5,6,7) */ @@ -206,4 +212,165 @@ public class Shading { return verticesPerRow; } + public void output(StringBuilder out, FunctionRenderer functionRenderer) { + out.append("<<\n/ShadingType " + shadingType + "\n"); + if (colorSpace != null) { + out.append("/ColorSpace /" + colorSpace.getName() + "\n"); + } + + if (background != null) { + out.append("/Background "); + outputDoubles(out, background); + out.append("\n"); + } + + if (bbox != null) { + out.append("/BBox"); + outputDoubles(out, bbox); + out.append("\n"); + } + + if (antiAlias) { + out.append("/AntiAlias " + antiAlias + "\n"); + } + + switch (shadingType) { + // Function based shading + case 1: outputShadingType1(out, functionRenderer); break; + // Axial shading + case 2: + // Radial shading + case 3: outputShadingType2or3(out, functionRenderer); break; + // Free-form Gouraud-shaded triangle meshes + case 4: + // Coons patch meshes + case 6: + // Tensor product patch meshes + case 7: outputShadingType4or6or7(out, functionRenderer); break; + // Lattice Free form gouraud-shaded triangle mesh + case 5: outputShadingType5(out, functionRenderer); break; + default: throw new UnsupportedOperationException("Shading type " + shadingType); + } + + out.append(">>"); + } + + private void outputDoubles(StringBuilder out, List doubles) { + out.append("[ "); + for (Double d : doubles) { + out.append(PDFNumber.doubleOut(d)); + out.append(" "); + } + out.append("]"); + } + + private void outputShadingType1(StringBuilder out, Shading.FunctionRenderer functionRenderer) { + if (domain != null) { + out.append("/Domain "); + outputDoubles(out, domain); + out.append("\n"); + } else { + out.append("/Domain [ 0 1 ] \n"); + } + + if (matrix != null) { + out.append("/Matrix "); + outputDoubles(out, matrix); + out.append("\n"); + } + outputFunction(out, functionRenderer); + } + + private void outputShadingType2or3(StringBuilder out, Shading.FunctionRenderer functionRenderer) { + if (coords != null) { + out.append("/Coords "); + outputDoubles(out, coords); + out.append("\n"); + } + + if (domain != null) { + out.append("/Domain "); + outputDoubles(out, domain); + out.append("\n"); + } else { + out.append("/Domain [ 0 1 ] \n"); + } + + if (extend != null) { + out.append("/Extend ["); + for (Boolean b : extend) { + out.append(b); + out.append(" "); + } + out.append("\n"); + } else { + out.append("/Extend [ true true ] \n"); + } + + outputFunction(out, functionRenderer); + } + + private void outputShadingType4or6or7(StringBuilder out, Shading.FunctionRenderer functionRenderer) { + if (bitsPerCoordinate > 0) { + out.append("/BitsPerCoordinate " + bitsPerCoordinate + "\n"); + } else { + out.append("/BitsPerCoordinate 1 \n"); + } + + if (bitsPerComponent > 0) { + out.append("/BitsPerComponent " + bitsPerComponent + "\n"); + } else { + out.append("/BitsPerComponent 1 \n"); + } + + if (bitsPerFlag > 0) { + out.append("/BitsPerFlag " + bitsPerFlag + "\n"); + } else { + out.append("/BitsPerFlag 2 \n"); + } + + if (decode != null) { + out.append("/Decode "); + outputDoubles(out, decode); + out.append("\n"); + } + + outputFunction(out, functionRenderer); + } + + private void outputShadingType5(StringBuilder out, Shading.FunctionRenderer functionRenderer) { + if (bitsPerCoordinate > 0) { + out.append("/BitsPerCoordinate " + bitsPerCoordinate + "\n"); + } else { + out.append("/BitsPerCoordinate 1 \n"); + } + + if (bitsPerComponent > 0) { + out.append("/BitsPerComponent " + bitsPerComponent + "\n"); + } else { + out.append("/BitsPerComponent 1 \n"); + } + + if (decode != null) { + out.append("/Decode "); + outputDoubles(out, decode); + out.append("\n"); + } + + outputFunction(out, functionRenderer); + + if (verticesPerRow > 0) { + out.append("/VerticesPerRow " + verticesPerRow + "\n"); + } else { + out.append("/VerticesPerRow 2 \n"); + } + } + + private void outputFunction(StringBuilder out, FunctionRenderer functionRenderer) { + if (function != null) { + out.append("/Function "); + functionRenderer.outputFunction(out); + out.append("\n"); + } + } } diff --git a/src/java/org/apache/fop/render/shading/ShadingPattern.java b/src/java/org/apache/fop/render/shading/ShadingPattern.java deleted file mode 100644 index 43e030bc8..000000000 --- a/src/java/org/apache/fop/render/shading/ShadingPattern.java +++ /dev/null @@ -1,270 +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.shading; - -import java.util.List; - -import org.apache.fop.pdf.PDFDeviceColorSpace; -import org.apache.fop.pdf.PDFNumber; - -/** - * A class for writing shading objects for different output formats - */ -public class ShadingPattern { - - public interface ShadingRenderer { - - void outputFunction(StringBuffer out); - } - - private Shading shading; - - private final ShadingRenderer shadingRenderer; - - /** - * Constructor - * @param shading The shading object from which to write the output - */ - public ShadingPattern(Shading shading, ShadingRenderer shadingRenderer) { - this.shading = shading; - this.shadingRenderer = shadingRenderer; - } - - /** - * Outputs the given shading object to a String - * @param colorSpace The Colospace (PDF and Postscript) - * @param shadingType The shading type - * @param background The background - * @param bBox The bounding box - * @param antiAlias Anti-aliasing - * @return Returns the output string - */ - public String toString(PDFDeviceColorSpace colorSpace, int shadingType, List background, - List bBox, boolean antiAlias) { - StringBuffer p = new StringBuffer(128); - p.append("<<\n/ShadingType " + shadingType + " \n"); - if (colorSpace != null) { - p.append("/ColorSpace /" - + colorSpace.getName() + " \n"); - } - - if (background != null) { - p.append("/Background [ "); - for (int bgIndex = 0; bgIndex < background.size(); bgIndex++) { - p.append(PDFNumber.doubleOut((Double)background.get(bgIndex)) - + " "); - } - p.append("] \n"); - } - - if (bBox - != null) { // I've never seen an example, so I guess this is right. - p.append("/BBox [ "); - for (int bboxIndex = 0; bboxIndex < bBox.size(); bboxIndex++) { - p.append(PDFNumber.doubleOut((Double)bBox.get(bboxIndex)) - + " "); - } - p.append("] \n"); - } - - if (antiAlias) { - p.append("/AntiAlias " + antiAlias + " \n"); - } - - // Here's where we differentiate based on what type it is. - switch (shadingType) { - //Function based shading - case 1: p = handleShadingType1(p); break; - //Axial shading - case 2: - //Radial shading - case 3: p = handleShadingType2or3(p); break; - //Free-form Gouraud-shaded triangle meshes - case 4: - //Coons patch meshes - case 6: - //Tensor product patch meshes - case 7: p = handleShadingType4or6or7(p); break; - //Lattice Free form gouraud-shaded triangle mesh - case 5: p = handleShadingType5(p); break; - default: //Shading pattern outside expecting values - break; - } - - p.append(">>"); - - return (p.toString()); - } - - - /** - * A method to write a type 1 shading object - * @param p The StringBuffer to write the shading object - * @return Returns the StringBuffer to which the shading object was written - */ - public StringBuffer handleShadingType1(StringBuffer p) { - if (shading.getDomain() != null) { - p.append("/Domain [ "); - for (int domainIndex = 0; domainIndex < shading.getDomain().size(); domainIndex++) { - p.append(PDFNumber.doubleOut((Double)shading.getDomain().get(domainIndex)) - + " "); - } - p.append("] \n"); - } else { - p.append("/Domain [ 0 1 ] \n"); - } - - if (shading.getMatrix() != null) { - p.append("/Matrix [ "); - for (int matrixIndex = 0; matrixIndex < shading.getMatrix().size(); matrixIndex++) { - p.append(PDFNumber.doubleOut((Double)shading.getMatrix().get(matrixIndex)) - + " "); - } - p.append("] \n"); - } - shadingRenderer.outputFunction(p); - return p; - } - - /** - * A method to write a type 2 or 3 shading object - * @param p The StringBuffer to write the shading object - * @return Returns the StringBuffer to which the shading object was written - */ - public StringBuffer handleShadingType2or3(StringBuffer p) { - // 3 is radial shading (circular gradient) - if (shading.getCoords() != null) { - p.append("/Coords [ "); - for (int coordIndex = 0; coordIndex < shading.getCoords().size(); coordIndex++) { - p.append(PDFNumber.doubleOut((Double)shading.getCoords().get(coordIndex)) - + " "); - } - p.append("] \n"); - } - - // DOMAIN - if (shading.getDomain() != null) { - p.append("/Domain [ "); - for (int domainIndex = 0; domainIndex < shading.getDomain().size(); domainIndex++) { - p.append(PDFNumber.doubleOut((Double)shading.getDomain().get(domainIndex)) - + " "); - } - p.append("] \n"); - } else { - p.append("/Domain [ 0 1 ] \n"); - } - - if (shading.getExtend() != null) { - p.append("/Extend [ "); - for (int extendIndex = 0; extendIndex < shading.getExtend().size(); extendIndex++) { - p.append((shading.getExtend().get(extendIndex)) + " "); - } - - p.append("] \n"); - } else { - p.append("/Extend [ true true ] \n"); - } - - shadingRenderer.outputFunction(p); - - return p; - } - - /** - * A method to write a type 4, 6 or 7 shading object - * @param p The StringBuffer to write the shading object - * @return Returns the StringBuffer to which the shading object was written - */ - public StringBuffer handleShadingType4or6or7(StringBuffer p) { - // 6:coons patch meshes - // 7://tensor product patch meshes (which no one ever uses) - if (shading.getBitsPerCoordinate() > 0) { - p.append("/BitsPerCoordinate " + shading.getBitsPerCoordinate() - + " \n"); - } else { - p.append("/BitsPerCoordinate 1 \n"); - } - - if (shading.getBitsPerComponent() > 0) { - p.append("/BitsPerComponent " + shading.getBitsPerComponent() - + " \n"); - } else { - p.append("/BitsPerComponent 1 \n"); - } - - if (shading.getBitsPerFlag() > 0) { - p.append("/BitsPerFlag " + shading.getBitsPerFlag() + " \n"); - } else { - p.append("/BitsPerFlag 2 \n"); - } - - if (shading.getDecode() != null) { - p.append("/Decode [ "); - for (int decodeIndex = 0; decodeIndex < shading.getDecode().size(); decodeIndex++) { - p.append((shading.getDecode().get(decodeIndex)) + " "); - } - - p.append("] \n"); - } - - shadingRenderer.outputFunction(p); - - return p; - } - - /** - * A method to write a type 5 shading object - * @param p The StringBuffer to write the shading object - * @return Returns the StringBuffer to which the shading object was written - */ - public StringBuffer handleShadingType5(StringBuffer p) { - if (shading.getBitsPerCoordinate() > 0) { - p.append("/BitsPerCoordinate " + shading.getBitsPerCoordinate() - + " \n"); - } else { - p.append("/BitsPerCoordinate 1 \n"); - } - - if (shading.getBitsPerComponent() > 0) { - p.append("/BitsPerComponent " + shading.getBitsPerComponent() - + " \n"); - } else { - p.append("/BitsPerComponent 1 \n"); - } - - if (shading.getDecode() != null) { - p.append("/Decode [ "); - for (int decodeIndex = 0; decodeIndex < shading.getDecode().size(); decodeIndex++) { - p.append((shading.getDecode().get(decodeIndex)) + " "); - } - - p.append("] \n"); - } - - shadingRenderer.outputFunction(p); - - if (shading.getVerticesPerRow() > 0) { - p.append("/VerticesPerRow " + shading.getVerticesPerRow() + " \n"); - } else { - p.append("/VerticesPerRow 2 \n"); - } - - return p; - } - -} -- 2.39.5