From 48d4ad499361b0d269a90d3c357b05d081e3798d Mon Sep 17 00:00:00 2001 From: Vincent Hennebert Date: Mon, 14 Jul 2014 21:08:42 +0000 Subject: [PATCH] Moved content of FunctionPattern into Function git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/FOP-2393_gradient-rendering@1610528 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/fop/pdf/PDFFunction.java | 4 +- .../apache/fop/render/ps/svg/PSPattern.java | 7 +- .../apache/fop/render/shading/Function.java | 181 +++++++++ .../fop/render/shading/FunctionPattern.java | 350 ------------------ 4 files changed, 184 insertions(+), 358 deletions(-) delete mode 100644 src/java/org/apache/fop/render/shading/FunctionPattern.java diff --git a/src/java/org/apache/fop/pdf/PDFFunction.java b/src/java/org/apache/fop/pdf/PDFFunction.java index 39942fc21..bdd61a86d 100644 --- a/src/java/org/apache/fop/pdf/PDFFunction.java +++ b/src/java/org/apache/fop/pdf/PDFFunction.java @@ -25,7 +25,6 @@ import java.util.Collections; import java.util.List; import org.apache.fop.render.shading.Function; -import org.apache.fop.render.shading.FunctionPattern; /** * class representing a PDF Function. @@ -108,12 +107,11 @@ public class PDFFunction extends PDFObject { public byte[] toByteString() { - FunctionPattern pattern = new FunctionPattern(function); List functionsStrings = new ArrayList(function.getFunctions().size()); for (PDFFunction f : pdfFunctions) { functionsStrings.add(f.referencePDF()); } - return encode(pattern.toWriteableString(functionsStrings)); + return encode(function.toWriteableString(functionsStrings)); } /** {@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 7c28ffcf7..c9ffbd7c6 100644 --- a/src/java/org/apache/fop/render/ps/svg/PSPattern.java +++ b/src/java/org/apache/fop/render/ps/svg/PSPattern.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.List; 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; @@ -119,24 +118,22 @@ public class PSPattern implements Pattern { Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() { 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(function.toWriteableString(functionsStrings)); } }; shading.output(p, functionRenderer); } private String functionToString(Function function) { - FunctionPattern pattern = new FunctionPattern(function); List functionsStrings = new ArrayList(function.getFunctions().size()); for (Function f : function.getFunctions()) { functionsStrings.add(functionToString(f)); } - return pattern.toWriteableString(functionsStrings); + return function.toWriteableString(functionsStrings); } } diff --git a/src/java/org/apache/fop/render/shading/Function.java b/src/java/org/apache/fop/render/shading/Function.java index 2f12c581d..8fb6528cb 100644 --- a/src/java/org/apache/fop/render/shading/Function.java +++ b/src/java/org/apache/fop/render/shading/Function.java @@ -20,6 +20,8 @@ package org.apache.fop.render.shading; import java.util.Collections; import java.util.List; +import org.apache.fop.pdf.PDFNumber; + public class Function { /** @@ -331,4 +333,183 @@ public class Function { return cOne; } + public String toWriteableString(List functionsStrings) { + StringBuilder out = new StringBuilder(256); + out.append("<<\n/FunctionType " + functionType + "\n"); + outputDomain(out); + if (this.functionType == 0) { + outputSize(out); + outputEncode(out); + outputBitsPerSample(out); + outputOrder(out); + outputRange(out); + outputDecode(out); + if (functionDataStream != null) { + out.append("/Length " + (functionDataStream.length() + 1) + "\n"); + } + outputFilter(out); + out.append(">>"); + if (functionDataStream != null) { + out.append("\nstream\n" + functionDataStream + "\nendstream"); + } + } else if (functionType == 2) { + outputRange(out); + outputCZero(out); + outputCOne(out); + outputInterpolationExponentN(out); + out.append(">>"); + } else if (functionType == 3) { + outputRange(out); + if (!functions.isEmpty()) { + out.append("/Functions [ "); + for (String f : functionsStrings) { + out.append(f); + out.append(' '); + } + out.append("]\n"); + } + outputEncode(out); + out.append("/Bounds "); + if (bounds != null) { + outputDoubles(out, bounds); + } else if (!functions.isEmpty()) { + // if there are n functions, + // there must be n-1 bounds. + // so let each function handle an equal portion + // of the whole. e.g. if there are 4, then [ 0.25 0.25 0.25 ] + int numberOfFunctions = functions.size(); + String functionsFraction = PDFNumber.doubleOut(Double.valueOf(1.0 / (numberOfFunctions))); + out.append("[ "); + for (int i = 0; i + 1 < numberOfFunctions; i++) { + out.append(functionsFraction); + out.append(" "); + } + out.append("]"); + } + out.append("\n>>"); + } else if (functionType == 4) { + outputRange(out); + if (functionDataStream != null) { + out.append("/Length " + (functionDataStream.length() + 1) + "\n"); + } + out.append(">>"); + if (functionDataStream != null) { + out.append("\nstream\n{ " + functionDataStream + " }\nendstream"); + } + } + return out.toString(); + } + + private void outputDomain(StringBuilder p) { + if (domain != null) { + p.append("/Domain "); + outputDoubles(p, domain); + p.append("\n"); + } else { + p.append("/Domain [ 0 1 ]\n"); + } + } + + private void outputSize(StringBuilder out) { + if (size != null) { + out.append("/Size "); + outputDoubles(out, size); + out.append("\n"); + } + } + + private void outputBitsPerSample(StringBuilder out) { + out.append("/BitsPerSample " + bitsPerSample + "\n"); + } + + private void outputOrder(StringBuilder out) { + if (order == 1 || order == 3) { + out.append("\n/Order " + order + "\n"); + } + } + + private void outputRange(StringBuilder out) { + if (range != null) { + out.append("/Range "); + outputDoubles(out, range); + out.append("\n"); + } + } + + private void outputEncode(StringBuilder out) { + if (encode != null) { + out.append("/Encode "); + outputDoubles(out, encode); + out.append("\n"); + } else { + out.append("/Encode [ "); + int size = functions.size(); + for (int i = 0; i < size; i++) { + out.append("0 1 "); + } + out.append("]\n"); + } + } + + private void outputDecode(StringBuilder out) { + if (decode != null) { + out.append("/Decode "); + outputDoubles(out, decode); + out.append("\n"); + } + } + + private void outputFilter(StringBuilder out) { + if (filter != null) { + int size = filter.size(); + out.append("/Filter "); + if (size == 1) { + out.append("/" + filter.get(0) + "\n"); + } else { + out.append("[ "); + for (int i = 0; i < size; i++) { + out.append("/" + filter.get(0) + " "); + } + out.append("]\n"); + } + } + } + + private void outputCZero(StringBuilder out) { + if (cZero != null) { + out.append("/C0 [ "); + for (float c : cZero) { + out.append(PDFNumber.doubleOut(c)); + out.append(" "); + } + out.append("]\n"); + } + } + + private void outputCOne(StringBuilder out) { + if (cOne != null) { + out.append("/C1 [ "); + for (float c : cOne) { + out.append(PDFNumber.doubleOut(c)); + out.append(" "); + } + out.append("]\n"); + } + } + + private void outputInterpolationExponentN(StringBuilder out) { + out.append("/N "); + out.append(PDFNumber.doubleOut(Double.valueOf(interpolationExponentN))); + out.append("\n"); + } + + private void outputDoubles(StringBuilder out, List doubles) { + out.append("[ "); + for (Double d : doubles) { + out.append(PDFNumber.doubleOut(d)); + out.append(" "); + } + out.append("]"); + } + } diff --git a/src/java/org/apache/fop/render/shading/FunctionPattern.java b/src/java/org/apache/fop/render/shading/FunctionPattern.java deleted file mode 100644 index f7b735dd1..000000000 --- a/src/java/org/apache/fop/render/shading/FunctionPattern.java +++ /dev/null @@ -1,350 +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.shading; - -import java.util.List; - -import org.apache.fop.pdf.PDFNumber; - -/** - * A class for writing function objects for different output formats - */ -public class FunctionPattern { - - private Function function; - - /** - * Constructor - * @param function The function from which to write the output - */ - public FunctionPattern(Function function) { - this.function = function; - } - - /** - * Outputs the function to a byte array - */ - public String toWriteableString(List functionsStrings) { - int vectorSize = 0; - int numberOfFunctions = 0; - int tempInt = 0; - StringBuffer p = new StringBuffer(256); - p.append("<< \n/FunctionType " + function.getFunctionType() + " \n"); - - // FunctionType 0 - if (this.function.getFunctionType() == 0) { - if (function.getDomain() != null) { - // DOMAIN - p.append("/Domain [ "); - vectorSize = function.getDomain().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getDomain().get(tempInt)) - + " "); - } - - p.append("] \n"); - } else { - p.append("/Domain [ 0 1 ] \n"); - } - - // SIZE - if (function.getSize() != null) { - p.append("/Size [ "); - vectorSize = function.getSize().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getSize().get(tempInt)) - + " "); - } - p.append("] \n"); - } - // ENCODE - if (function.getEncode() != null) { - p.append("/Encode [ "); - vectorSize = function.getEncode().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getEncode().get(tempInt)) - + " "); - } - p.append("] \n"); - } else { - p.append("/Encode [ "); - vectorSize = function.getFunctions().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append("0 1 "); - } - p.append("] \n"); - - } - - // BITSPERSAMPLE - p.append("/BitsPerSample " + function.getBitsPerSample()); - - // ORDER (optional) - if (function.getOrder() == 1 || function.getOrder() == 3) { - p.append(" \n/Order " + function.getOrder() + " \n"); - } - - // RANGE - if (function.getRange() != null) { - p.append("/Range [ "); - vectorSize = function.getRange().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getRange().get(tempInt)) - + " "); - } - - p.append("] \n"); - } - - // DECODE - if (function.getDecode() != null) { - p.append("/Decode [ "); - vectorSize = function.getDecode().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getDecode().get(tempInt)) - + " "); - } - - p.append("] \n"); - } - - // LENGTH - if (function.getDataStream() != null) { - p.append("/Length " + (function.getDataStream().length() + 1) - + " \n"); - } - - // FILTER? - if (function.getFilter() != null) { // if there's a filter - vectorSize = function.getFilter().size(); - p.append("/Filter "); - if (vectorSize == 1) { - p.append("/" + (function.getFilter().get(0)) - + " \n"); - } else { - p.append("[ "); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append("/" + (function.getFilter().get(0)) - + " "); - } - p.append("] \n"); - } - } - p.append(">>"); - - // stream representing the function - if (function.getDataStream() != null) { - p.append("\nstream\n" + function.getDataStream() - + "\nendstream"); - } - - // end of if FunctionType 0 - - } else if (function.getFunctionType() == 2) { - // DOMAIN - if (function.getDomain() != null) { - p.append("/Domain [ "); - vectorSize = function.getDomain().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getDomain().get(tempInt)) - + " "); - } - - p.append("] \n"); - } else { - p.append("/Domain [ 0 1 ] \n"); - } - - - // RANGE - if (function.getRange() != null) { - p.append("/Range [ "); - vectorSize = function.getRange().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getRange().get(tempInt)) - + " "); - } - - p.append("] \n"); - } - - // FunctionType, C0, C1, N are required in PDF - - // C0 - if (function.getCZero() != null) { - p.append("/C0 [ "); - for (float c : function.getCZero()) { - p.append(PDFNumber.doubleOut(c)); - p.append(" "); - } - p.append("] \n"); - } - - // C1 - if (function.getCOne() != null) { - p.append("/C1 [ "); - for (float c : function.getCOne()) { - p.append(PDFNumber.doubleOut(c)); - p.append(" "); - } - p.append("] \n"); - } - - // N: The interpolation Exponent - p.append("/N " - + PDFNumber.doubleOut(Double.valueOf(function.getInterpolationExponentN())) - + " \n"); - - p.append(">>"); - - } else if (function.getFunctionType() - == 3) { // fix this up when my eyes uncross - // DOMAIN - if (function.getDomain() != null) { - p.append("/Domain [ "); - vectorSize = function.getDomain().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getDomain().get(tempInt)) - + " "); - } - p.append("] \n"); - } else { - p.append("/Domain [ 0 1 ] \n"); - } - - // RANGE - if (function.getRange() != null) { - p.append("/Range [ "); - vectorSize = function.getRange().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getRange().get(tempInt)) - + " "); - } - - p.append("] \n"); - } - - // FUNCTIONS - if (!function.getFunctions().isEmpty()) { - p.append("/Functions [ "); - numberOfFunctions = function.getFunctions().size(); - for (String f : functionsStrings) { - p.append(f); - p.append(' '); - } - p.append("] \n"); - } - - - // ENCODE - if (function.getEncode() != null) { - p.append("/Encode [ "); - vectorSize = function.getEncode().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getEncode().get(tempInt)) - + " "); - } - - p.append("] \n"); - } else { - p.append("/Encode [ "); - vectorSize = function.getFunctions().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append("0 1 "); - } - p.append("] \n"); - - } - - - // BOUNDS, required, but can be empty - p.append("/Bounds [ "); - if (function.getBounds() != null) { - - vectorSize = function.getBounds().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getBounds().get(tempInt)) - + " "); - } - - } else { - if (!function.getFunctions().isEmpty()) { - // if there are n functions, - // there must be n-1 bounds. - // so let each function handle an equal portion - // of the whole. e.g. if there are 4, then [ 0.25 0.25 0.25 ] - - String functionsFraction = PDFNumber.doubleOut(Double.valueOf(1.0 - / (numberOfFunctions))); - - for (tempInt = 0; tempInt + 1 < numberOfFunctions; - tempInt++) { - - p.append(functionsFraction + " "); - } - } - - } - p.append("]\n>>"); - } else if (function.getFunctionType() - == 4) { // fix this up when my eyes uncross - // DOMAIN - if (function.getDomain() != null) { - p.append("/Domain [ "); - vectorSize = function.getDomain().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getDomain().get(tempInt)) - + " "); - } - - p.append("] \n"); - } else { - p.append("/Domain [ 0 1 ] \n"); - } - - // RANGE - if (function.getRange() != null) { - p.append("/Range [ "); - vectorSize = function.getRange().size(); - for (tempInt = 0; tempInt < vectorSize; tempInt++) { - p.append(PDFNumber.doubleOut(function.getRange().get(tempInt)) - + " "); - } - - p.append("] \n"); - } - - // LENGTH - if (function.getDataStream() != null) { - p.append("/Length " + (function.getDataStream().length() + 1) - + " \n"); - } - - p.append(">>"); - - // stream representing the function - if (function.getDataStream() != null) { - p.append("\nstream\n{ " + function.getDataStream() - + " }\nendstream"); - } - } - return p.toString(); - } -} -- 2.39.5