1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id$ */
19 
20 package org.apache.fop.render.ps;
21 
22 import java.util.List;
23 
24 import org.apache.fop.render.gradient.Function;
25 import org.apache.fop.render.gradient.Function.SubFunctionRenderer;
26 import org.apache.fop.render.gradient.GradientMaker.DoubleFormatter;
27 import org.apache.fop.render.gradient.Pattern;
28 import org.apache.fop.render.gradient.Shading;
29 
30 /**
31  * Helper class to draw gradients in PostScript.
32  */
33 public final class Gradient {
34 
Gradient()35     private Gradient() { }
36 
outputPattern(Pattern pattern, DoubleFormatter doubleFormatter)37     public static String outputPattern(Pattern pattern, DoubleFormatter doubleFormatter) {
38         StringBuilder p = new StringBuilder(64);
39         p.append("/Pattern setcolorspace\n");
40         p.append("<< \n/Type /Pattern \n");
41 
42         p.append("/PatternType " + pattern.getPatternType() + " \n");
43 
44         if (pattern.getShading() != null) {
45             p.append("/Shading ");
46             outputShading(p, pattern.getShading(), doubleFormatter);
47             p.append(" \n");
48         }
49         p.append(">> \n");
50         List<Double> matrix = pattern.getMatrix();
51         if (matrix == null) {
52             p.append("matrix ");
53         } else {
54             p.append("[ ");
55             for (double m : pattern.getMatrix()) {
56                 p.append(doubleFormatter.formatDouble(m));
57                 p.append(" ");
58             }
59             p.append("] ");
60         }
61         p.append("makepattern setcolor\n");
62 
63         return p.toString();
64     }
65 
outputShading(StringBuilder out, Shading shading, final DoubleFormatter doubleFormatter)66     private static void outputShading(StringBuilder out, Shading shading, final DoubleFormatter doubleFormatter) {
67         final Function function = shading.getFunction();
68         Shading.FunctionRenderer functionRenderer = new Shading.FunctionRenderer() {
69 
70             public void outputFunction(StringBuilder out) {
71                 SubFunctionRenderer subFunctionRenderer = new Function.SubFunctionRenderer() {
72 
73                     public void outputFunction(StringBuilder out, int functionIndex) {
74                         Function subFunction = function.getFunctions().get(functionIndex);
75                         assert subFunction.getFunctions().isEmpty();
76                         subFunction.output(out, doubleFormatter, null);
77                     }
78                 };
79                 function.output(out, doubleFormatter, subFunctionRenderer);
80             }
81         };
82         shading.output(out, doubleFormatter, functionRenderer);
83     }
84 
85 }
86