aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/shading/Shading.java
blob: 89b4added0100f4f359dd6dee91af8271ae32503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * 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;


public class Shading {

    public interface FunctionRenderer {

        void outputFunction(StringBuilder out);
    }

    /**
     * Required: The Type of shading (1,2,3,4,5,6,7)
     */
    private final int shadingType;

    /**
     * A ColorSpace representing the colorspace. "DeviceRGB" is an example.
     */
    private final PDFDeviceColorSpace colorSpace;

    /**
     * Required for Type 2: An Array of four numbers specifying
     *                      the starting and ending coordinate pairs
     * Required for Type 3: An Array of six numbers [x0,y0,r0,x1,y1,r1]
     *                      specifying the centers and radii of
     *                      the starting and ending circles.
     */
    private final List<Double> coords;

    /**
     * Required for Type 1, 2, and 3:
     * The object of the color mapping function (usually type 2 or 3).
     * Optional for Type 4,5,6, and 7: When it's nearly the same thing.
     */
    private final Function function;

    /**
     * Optional: A List specifying the clipping rectangle
     */
    private final List<Double> bbox;

    /**
     * Optional for Type 1: A transformation matrix
     */
    private final List<Double> matrix;

    /**
     * The background color. Since shading is opaque,
     * this is very rarely used.
     */
    private final List<Double> background;

    /**
     * Optional for Type 1: Array of four numbers, xmin, xmax, ymin, ymax.
     *                      Default is [0 1 0 1]
     * Optional for Type 2: An array of two numbers between which the blend
     *                      varies between start and end points. Default is 0, 1.
     * Optional for Type 3: An array of two numbers between which the blend
     *                      varies between start and end points. Default is 0, 1.
     */
    private final List<Double> domain;

    /**
     * Required for Type 4,5,6, and 7: Array of Doubles which specifies
     * how to decode coordinate and color component values.
     * Each type has a differing number of decode array members, so check
     * the spec.
     * Page 303 in PDF Spec 1.3
     */
    private final List<Double> decode;

    /**
     * Required for Type 2+3: An Array of two boolean values specifying
     * whether to extend the start and end colors past the start
     * and end points, respectively.
     * Default is false, false.
     */
    private final List<Boolean> extend;

    /**
     * Required for Type 4,5,6, and 7: Specifies the number of bits used
     * to represent each vertex coordinate.
     * Allowed to be 1,2,4,8,12,16,24, or 32.
     */
    private final int bitsPerCoordinate;

    /**
     * Required for Type 4,5,6, and 7: Specifies the number of bits used
     * to represent the edge flag for each vertex.
     * Allowed to be 2,4,or 8, while the Edge flag itself is allowed to
     * be 0,1 or 2.
     */
    private final int bitsPerFlag;

    /**
     * Optional: A flag whether or not to filter the shading function
     * to prevent aliasing artifacts. Default is false.
     */
    private final boolean antiAlias;

    /**
     * Required for Type 4,5,6, and 7: Specifies the number of bits used
     * to represent each color coordinate.
     * Allowed to be 1,2,4,8,12, or 16
     */
    private final int bitsPerComponent;

    /**
     * Required for Type 5:The number of vertices in each "row" of
     * the lattice; it must be greater than or equal to 2.
     */
    private final int verticesPerRow;

    public Shading(int shadingType, PDFDeviceColorSpace colorSpace,
            List<Double> coords, Function function) {
        this.shadingType = shadingType;
        this.colorSpace = colorSpace;
        this.background = null;
        this.bbox = null;
        this.antiAlias = false;
        this.coords = coords;
        this.domain = null;
        this.function = function;
        this.extend = null;
        this.matrix = null;
        this.decode = null;
        this.bitsPerCoordinate = 0;
        this.bitsPerFlag = 0;
        this.bitsPerComponent = 0;
        this.verticesPerRow = 0;
    }

    public int getShadingType() {
        return shadingType;
    }

    public PDFDeviceColorSpace getColorSpace() {
        return colorSpace;
    }

    public List<Double> getCoords() {
        return coords;
    }

    public Function getFunction() {
        return function;
    }

    public List<Double> getBBox() {
        return bbox;
    }

    public List<Double> getMatrix() {
        return matrix;
    }

    public List<Double> getBackground() {
        return background;
    }

    public List<Double> getDomain() {
        return domain;
    }

    public List<Double> getDecode() {
        return decode;
    }

    public List<Boolean> getExtend() {
        return extend;
    }

    public int getBitsPerCoordinate() {
        return bitsPerCoordinate;
    }

    public int getBitsPerFlag() {
        return bitsPerFlag;
    }

    public boolean isAntiAlias() {
        return antiAlias;
    }

    public int getBitsPerComponent() {
        return bitsPerComponent;
    }

    public int getVerticesPerRow() {
        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<Double> 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");
        }
    }
}