import org.apache.fop.render.gradient.Function;
import org.apache.fop.render.gradient.Function.SubFunctionRenderer;
+import org.apache.fop.render.gradient.GradientMaker;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
/**
* class representing a PDF Function.
}
};
StringBuilder out = new StringBuilder();
- function.output(out, subFunctionRenderer);
+ GradientMaker.DoubleFormatter doubleFormatter = new DoubleFormatter() {
+
+ public String formatDouble(double d) {
+ return PDFNumber.doubleOut(d);
+ }
+ };
+ function.output(out, doubleFormatter, subFunctionRenderer);
return encode(out.toString());
}
import java.util.List;
+import org.apache.fop.render.gradient.GradientMaker;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
import org.apache.fop.render.gradient.Shading;
}
};
StringBuilder out = new StringBuilder();
- shading.output(out, functionRenderer);
+ GradientMaker.DoubleFormatter doubleFormatter = new DoubleFormatter() {
+
+ public String formatDouble(double d) {
+ return PDFNumber.doubleOut(d);
+ }
+ };
+ shading.output(out, doubleFormatter, functionRenderer);
return out.toString();
}
import java.util.Collections;
import java.util.List;
-import org.apache.fop.pdf.PDFNumber;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
public class Function {
return cOne;
}
- public String output(StringBuilder out, SubFunctionRenderer subFunctionRenderer) {
+ public String output(StringBuilder out, DoubleFormatter doubleFormatter,
+ SubFunctionRenderer subFunctionRenderer) {
out.append("<<\n/FunctionType " + functionType + "\n");
- outputDomain(out);
+ outputDomain(out, doubleFormatter);
if (this.functionType == 0) {
- outputSize(out);
- outputEncode(out);
+ outputSize(out, doubleFormatter);
+ outputEncode(out, doubleFormatter);
outputBitsPerSample(out);
outputOrder(out);
- outputRange(out);
- outputDecode(out);
+ outputRange(out, doubleFormatter);
+ outputDecode(out, doubleFormatter);
if (functionDataStream != null) {
out.append("/Length " + (functionDataStream.length() + 1) + "\n");
}
out.append("\nstream\n" + functionDataStream + "\nendstream");
}
} else if (functionType == 2) {
- outputRange(out);
- outputCZero(out);
- outputCOne(out);
- outputInterpolationExponentN(out);
+ outputRange(out, doubleFormatter);
+ outputCZero(out, doubleFormatter);
+ outputCOne(out, doubleFormatter);
+ outputInterpolationExponentN(out, doubleFormatter);
out.append(">>");
} else if (functionType == 3) {
- outputRange(out);
+ outputRange(out, doubleFormatter);
if (!functions.isEmpty()) {
out.append("/Functions [ ");
for (int i = 0; i < functions.size(); i++) {
}
out.append("]\n");
}
- outputEncode(out);
+ outputEncode(out, doubleFormatter);
out.append("/Bounds ");
if (bounds != null) {
- GradientMaker.outputDoubles(out, bounds);
+ GradientMaker.outputDoubles(out, doubleFormatter, 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(1.0 / numberOfFunctions);
+ String functionsFraction = doubleFormatter.formatDouble(1.0 / numberOfFunctions);
out.append("[ ");
for (int i = 0; i + 1 < numberOfFunctions; i++) {
out.append(functionsFraction);
}
out.append("\n>>");
} else if (functionType == 4) {
- outputRange(out);
+ outputRange(out, doubleFormatter);
if (functionDataStream != null) {
out.append("/Length " + (functionDataStream.length() + 1) + "\n");
}
return out.toString();
}
- private void outputDomain(StringBuilder p) {
+ private void outputDomain(StringBuilder p, DoubleFormatter doubleFormatter) {
if (domain != null) {
p.append("/Domain ");
- GradientMaker.outputDoubles(p, domain);
+ GradientMaker.outputDoubles(p, doubleFormatter, domain);
p.append("\n");
} else {
p.append("/Domain [ 0 1 ]\n");
}
}
- private void outputSize(StringBuilder out) {
+ private void outputSize(StringBuilder out, DoubleFormatter doubleFormatter) {
if (size != null) {
out.append("/Size ");
- GradientMaker.outputDoubles(out, size);
+ GradientMaker.outputDoubles(out, doubleFormatter, size);
out.append("\n");
}
}
}
}
- private void outputRange(StringBuilder out) {
+ private void outputRange(StringBuilder out, DoubleFormatter doubleFormatter) {
if (range != null) {
out.append("/Range ");
- GradientMaker.outputDoubles(out, range);
+ GradientMaker.outputDoubles(out, doubleFormatter, range);
out.append("\n");
}
}
- private void outputEncode(StringBuilder out) {
+ private void outputEncode(StringBuilder out, DoubleFormatter doubleFormatter) {
if (encode != null) {
out.append("/Encode ");
- GradientMaker.outputDoubles(out, encode);
+ GradientMaker.outputDoubles(out, doubleFormatter, encode);
out.append("\n");
} else {
out.append("/Encode [ ");
}
}
- private void outputDecode(StringBuilder out) {
+ private void outputDecode(StringBuilder out, DoubleFormatter doubleFormatter) {
if (decode != null) {
out.append("/Decode ");
- GradientMaker.outputDoubles(out, decode);
+ GradientMaker.outputDoubles(out, doubleFormatter, decode);
out.append("\n");
}
}
}
}
- private void outputCZero(StringBuilder out) {
+ private void outputCZero(StringBuilder out, DoubleFormatter doubleFormatter) {
if (cZero != null) {
out.append("/C0 [ ");
for (float c : cZero) {
- out.append(PDFNumber.doubleOut(c));
+ out.append(doubleFormatter.formatDouble(c));
out.append(" ");
}
out.append("]\n");
}
}
- private void outputCOne(StringBuilder out) {
+ private void outputCOne(StringBuilder out, DoubleFormatter doubleFormatter) {
if (cOne != null) {
out.append("/C1 [ ");
for (float c : cOne) {
- out.append(PDFNumber.doubleOut(c));
+ out.append(doubleFormatter.formatDouble(c));
out.append(" ");
}
out.append("]\n");
}
}
- private void outputInterpolationExponentN(StringBuilder out) {
+ private void outputInterpolationExponentN(StringBuilder out, DoubleFormatter doubleFormatter) {
out.append("/N ");
- out.append(PDFNumber.doubleOut(interpolationExponentN));
+ out.append(doubleFormatter.formatDouble(interpolationExponentN));
out.append("\n");
}
import org.apache.xmlgraphics.java2d.color.ColorUtil;
import org.apache.fop.pdf.PDFDeviceColorSpace;
-import org.apache.fop.pdf.PDFNumber;
public final class GradientMaker {
+ public interface DoubleFormatter {
+
+ String formatDouble(double d);
+ }
+
private GradientMaker() { }
public static Pattern makeLinearGradient(LinearGradientPaint gp,
return gradientColors;
}
- static void outputDoubles(StringBuilder out, List<Double> doubles) {
+ static void outputDoubles(StringBuilder out, DoubleFormatter doubleFormatter, List<Double> doubles) {
out.append("[ ");
for (Double d : doubles) {
- out.append(PDFNumber.doubleOut(d));
+ out.append(doubleFormatter.formatDouble(d));
out.append(" ");
}
out.append("]");
import java.util.List;
import org.apache.fop.pdf.PDFDeviceColorSpace;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
public class Shading {
return verticesPerRow;
}
- public void output(StringBuilder out, FunctionRenderer functionRenderer) {
+ public void output(StringBuilder out, DoubleFormatter doubleFormatter, FunctionRenderer functionRenderer) {
out.append("<<\n/ShadingType " + shadingType + "\n");
if (colorSpace != null) {
out.append("/ColorSpace /" + colorSpace.getName() + "\n");
if (background != null) {
out.append("/Background ");
- GradientMaker.outputDoubles(out, background);
+ GradientMaker.outputDoubles(out, doubleFormatter, background);
out.append("\n");
}
if (bbox != null) {
out.append("/BBox");
- GradientMaker.outputDoubles(out, bbox);
+ GradientMaker.outputDoubles(out, doubleFormatter, bbox);
out.append("\n");
}
switch (shadingType) {
// Function based shading
- case 1: outputShadingType1(out, functionRenderer); break;
+ case 1: outputShadingType1(out, doubleFormatter, functionRenderer); break;
// Axial shading
case 2:
// Radial shading
- case 3: outputShadingType2or3(out, functionRenderer); break;
+ case 3: outputShadingType2or3(out, doubleFormatter, 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;
+ case 7: outputShadingType4or6or7(out, doubleFormatter, functionRenderer); break;
// Lattice Free form gouraud-shaded triangle mesh
- case 5: outputShadingType5(out, functionRenderer); break;
+ case 5: outputShadingType5(out, doubleFormatter, functionRenderer); break;
default: throw new UnsupportedOperationException("Shading type " + shadingType);
}
out.append(">>");
}
- private void outputShadingType1(StringBuilder out, Shading.FunctionRenderer functionRenderer) {
+ private void outputShadingType1(StringBuilder out, DoubleFormatter doubleFormatter,
+ Shading.FunctionRenderer functionRenderer) {
if (domain != null) {
out.append("/Domain ");
- GradientMaker.outputDoubles(out, domain);
+ GradientMaker.outputDoubles(out, doubleFormatter, domain);
out.append("\n");
} else {
out.append("/Domain [ 0 1 ] \n");
if (matrix != null) {
out.append("/Matrix ");
- GradientMaker.outputDoubles(out, matrix);
+ GradientMaker.outputDoubles(out, doubleFormatter, matrix);
out.append("\n");
}
outputFunction(out, functionRenderer);
}
- private void outputShadingType2or3(StringBuilder out, Shading.FunctionRenderer functionRenderer) {
+ private void outputShadingType2or3(StringBuilder out, DoubleFormatter doubleFormatter,
+ Shading.FunctionRenderer functionRenderer) {
if (coords != null) {
out.append("/Coords ");
- GradientMaker.outputDoubles(out, coords);
+ GradientMaker.outputDoubles(out, doubleFormatter, coords);
out.append("\n");
}
if (domain != null) {
out.append("/Domain ");
- GradientMaker.outputDoubles(out, domain);
+ GradientMaker.outputDoubles(out, doubleFormatter, domain);
out.append("\n");
} else {
out.append("/Domain [ 0 1 ] \n");
outputFunction(out, functionRenderer);
}
- private void outputShadingType4or6or7(StringBuilder out, Shading.FunctionRenderer functionRenderer) {
+ private void outputShadingType4or6or7(StringBuilder out, DoubleFormatter doubleFormatter,
+ Shading.FunctionRenderer functionRenderer) {
if (bitsPerCoordinate > 0) {
out.append("/BitsPerCoordinate " + bitsPerCoordinate + "\n");
} else {
if (decode != null) {
out.append("/Decode ");
- GradientMaker.outputDoubles(out, decode);
+ GradientMaker.outputDoubles(out, doubleFormatter, decode);
out.append("\n");
}
outputFunction(out, functionRenderer);
}
- private void outputShadingType5(StringBuilder out, Shading.FunctionRenderer functionRenderer) {
+ private void outputShadingType5(StringBuilder out, DoubleFormatter doubleFormatter,
+ Shading.FunctionRenderer functionRenderer) {
if (bitsPerCoordinate > 0) {
out.append("/BitsPerCoordinate " + bitsPerCoordinate + "\n");
} else {
if (decode != null) {
out.append("/Decode ");
- GradientMaker.outputDoubles(out, decode);
+ GradientMaker.outputDoubles(out, doubleFormatter, decode);
out.append("\n");
}
import org.apache.fop.render.gradient.Function;
import org.apache.fop.render.gradient.Function.SubFunctionRenderer;
import org.apache.fop.render.gradient.GradientMaker;
+import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
import org.apache.fop.render.gradient.Pattern;
import org.apache.fop.render.gradient.Shading;
Pattern pattern = GradientMaker.makeLinearGradient((LinearGradientPaint) paint,
getBaseTransform(), getTransform());
try {
- gen.write(toString(pattern));
+ gen.write(outputPattern(pattern));
} catch (IOException ioe) {
handleIOException(ioe);
}
Pattern pattern = GradientMaker.makeRadialGradient((RadialGradientPaint) paint,
getBaseTransform(), getTransform());
try {
- gen.write(toString(pattern));
+ gen.write(outputPattern(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) {
+ private String outputPattern(Pattern pattern) {
StringBuilder p = new StringBuilder(64);
p.append("/Pattern setcolorspace\n");
p.append("<< \n/Type /Pattern \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(getPSGenerator().formatDouble(m));
p.append(" ");
}
p.append("] ");
}
private void outputShading(StringBuilder out, Shading shading) {
+ final GradientMaker.DoubleFormatter doubleFormatter = new DoubleFormatter() {
+
+ public String formatDouble(double d) {
+ return getPSGenerator().formatDouble(d);
+ }
+ };
final Function function = shading.getFunction();
Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() {
public void outputFunction(StringBuilder out, int functionIndex) {
Function subFunction = function.getFunctions().get(functionIndex);
assert subFunction.getFunctions().isEmpty();
- subFunction.output(out, null);
+ subFunction.output(out, doubleFormatter, null);
}
};
- function.output(out, subFunctionRenderer);
+ function.output(out, doubleFormatter, subFunctionRenderer);
}
};
- shading.output(out, functionRenderer);
+ shading.output(out, doubleFormatter, functionRenderer);
}
protected AffineTransform getBaseTransform() {