KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > PDFPattern


1 /*
2  * $Id: PDFPattern.java,v 1.8.2.4 2003/02/25 14:29:38 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.pdf;
52
53 // Java...
54
import java.io.UnsupportedEncodingException JavaDoc;
55 import java.util.List JavaDoc;
56
57 /**
58  * class representing a PDF Function.
59  *
60  * PDF Functions represent parameterized mathematical formulas and sampled representations with
61  * arbitrary resolution. Functions are used in two areas: device-dependent
62  * rasterization information for halftoning and transfer
63  * functions, and color specification for smooth shading (a PDF 1.3 feature).
64  *
65  * All PDF Functions have a FunctionType (0,2,3, or 4), a Domain, and a Range.
66  */

67 public class PDFPattern extends PDFPathPaint {
68
69     /**
70      * The resources associated with this pattern
71      */

72     // Guts common to all function types
73

74     protected PDFResources resources = null;
75
76     /**
77      * Either one (1) for tiling, or two (2) for shading.
78      */

79     protected int patternType = 2; // Default
80

81     /**
82      * The name of the pattern such as "Pa1" or "Pattern1"
83      */

84     protected String JavaDoc patternName = null;
85
86     /**
87      * 1 for colored pattern, 2 for uncolored
88      */

89     protected int paintType = 2;
90
91     /**
92      * 1 for constant spacing, 2 for no distortion, and 3 for fast rendering
93      */

94     protected int tilingType = 1;
95
96     /**
97      * List of Doubles representing the Bounding box rectangle
98      */

99     protected List JavaDoc bBox = null;
100
101     /**
102      * Horizontal spacing
103      */

104     protected double xStep = -1;
105
106     /**
107      * Vertical spacing
108      */

109     protected double yStep = -1;
110
111     /**
112      * The Shading object comprising the Type 2 pattern
113      */

114     protected PDFShading shading = null;
115
116     /**
117      * List of Integers represetning the Extended unique Identifier
118      */

119     protected List JavaDoc xUID = null;
120
121     /**
122      * String representing the extended Graphics state.
123      * Probably will never be used like this.
124      */

125     protected StringBuffer JavaDoc extGState =
126         null; // eventually, need a PDFExtGSState object... but not now.
127

128     /**
129      * List of Doubles representing the Transformation matrix.
130      */

131     protected List JavaDoc matrix = null;
132
133     /**
134      * The stream of a pattern
135      */

136     protected StringBuffer JavaDoc patternDataStream = null;
137
138
139     /**
140      * Create a tiling pattern (type 1).
141      *
142      * @param theNumber The object number of this PDF Object
143      * @param thePatternName The name of the pattern such as "Pa1" or "Pattern1"
144      * @param theResources the resources associated with this pattern
145      * @param thePatternType the type of pattern, which is 1 for tiling.
146      * @param thePaintType 1 or 2, colored or uncolored.
147      * @param theTilingType 1, 2, or 3, constant spacing, no distortion, or faster tiling
148      * @param theBBox List of Doubles: The pattern cell bounding box
149      * @param theXStep horizontal spacing
150      * @param theYStep vertical spacing
151      * @param theMatrix Optional List of Doubles transformation matrix
152      * @param theXUID Optional List of Integers that uniquely identify the pattern
153      * @param thePatternDataStream The stream of pattern data to be tiled.
154      */

155     public PDFPattern(int theNumber, String JavaDoc thePatternName,
156                       PDFResources theResources, int thePatternType, // 1
157
int thePaintType, int theTilingType, List JavaDoc theBBox,
158                         double theXStep, double theYStep,
159                         List JavaDoc theMatrix, List JavaDoc theXUID,
160                         StringBuffer JavaDoc thePatternDataStream) {
161         super(theNumber);
162         this.patternName = thePatternName;
163
164         this.resources = theResources;
165         // This next parameter is implicit to all constructors, and is
166
// not directly passed.
167

168         this.patternType = 1; // thePatternType;
169
this.paintType = thePaintType;
170         this.tilingType = theTilingType;
171         this.bBox = theBBox;
172         this.xStep = theXStep;
173         this.yStep = theYStep;
174         this.matrix = theMatrix;
175         this.xUID = theXUID;
176         this.patternDataStream = thePatternDataStream;
177     }
178
179     /**
180      * Create a type 2 pattern (smooth shading)
181      *
182      * @param theNumber the object number of this PDF object
183      * @param thePatternName the name of the pattern
184      * @param thePatternType the type of the pattern, which is 2, smooth shading
185      * @param theShading the PDF Shading object that comprises this pattern
186      * @param theXUID optional:the extended unique Identifier if used.
187      * @param theExtGState optional: the extended graphics state, if used.
188      * @param theMatrix Optional:List of Doubles that specify the matrix.
189      */

190     public PDFPattern(int theNumber, String JavaDoc thePatternName,
191                       int thePatternType, PDFShading theShading,
192                       List JavaDoc theXUID, StringBuffer JavaDoc theExtGState,
193                       List JavaDoc theMatrix) {
194         super(theNumber);
195
196         this.patternName = thePatternName;
197
198         this.patternType = 2; // thePatternType;
199
this.shading = theShading;
200         this.xUID = theXUID;
201         // this isn't really implemented, so it should always be null.
202
// I just don't want to have to add a new parameter once it is implemented.
203
this.extGState = theExtGState; // always null
204
this.matrix = theMatrix;
205     }
206
207     /**
208      * Get the name of the pattern
209      *
210      * @return String representing the name of the pattern.
211      */

212     public String JavaDoc getName() {
213         return (this.patternName);
214     }
215
216     public String JavaDoc getColorSpaceOut(boolean fillNotStroke) {
217         if (fillNotStroke) { // fill but no stroke
218
return ("/Pattern cs /" + this.getName() + " scn \n");
219         } else { // stroke (or border)
220
return ("/Pattern CS /" + this.getName() + " SCN \n");
221         }
222     }
223
224
225     /**
226      * represent as PDF. Whatever the FunctionType is, the correct
227      * representation spits out. The sets of required and optional
228      * attributes are different for each type, but if a required
229      * attribute's object was constructed as null, then no error
230      * is raised. Instead, the malformed PDF that was requested
231      * by the construction is dutifully output.
232      * This policy should be reviewed.
233      *
234      * @return the PDF string.
235      */

236     public byte[] toPDF() {
237
238
239         int vectorSize = 0;
240         int tempInt = 0;
241         StringBuffer JavaDoc p = new StringBuffer JavaDoc();
242         p.append(this.number + " " + this.generation
243                  + " obj\n<< \n/Type /Pattern \n");
244
245         if (this.resources != null) {
246             p.append("/Resources " + this.resources.referencePDF() + " \n");
247         }
248
249         p.append("/PatternType " + this.patternType + " \n");
250
251         if (this.patternType == 1) {
252             p.append("/PaintType " + this.paintType + " \n");
253             p.append("/TilingType " + this.tilingType + " \n");
254
255             if (this.bBox != null) {
256                 vectorSize = this.bBox.size();
257                 p.append("/BBox [ ");
258                 for (tempInt = 0; tempInt < vectorSize; tempInt++) {
259                     p.append(PDFNumber.doubleOut((Double JavaDoc)this.bBox.get(tempInt)));
260                     p.append(" ");
261                 }
262                 p.append("] \n");
263             }
264             p.append("/XStep " + PDFNumber.doubleOut(new Double JavaDoc(this.xStep))
265                      + " \n");
266             p.append("/YStep " + PDFNumber.doubleOut(new Double JavaDoc(this.yStep))
267                      + " \n");
268
269             if (this.matrix != null) {
270                 vectorSize = this.matrix.size();
271                 p.append("/Matrix [ ");
272                 for (tempInt = 0; tempInt < vectorSize; tempInt++) {
273                     p.append(PDFNumber.doubleOut((Double JavaDoc)this.matrix.get(tempInt)));
274                     p.append(" ");
275                 }
276                 p.append("] \n");
277             }
278
279             if (this.xUID != null) {
280                 vectorSize = this.xUID.size();
281                 p.append("/XUID [ ");
282                 for (tempInt = 0; tempInt < vectorSize; tempInt++) {
283                     p.append(((Integer JavaDoc)this.xUID.get(tempInt)) + " ");
284                 }
285                 p.append("] \n");
286             }
287             // don't forget the length of the stream.
288
if (this.patternDataStream != null) {
289                 p.append("/Length " + (this.patternDataStream.length() + 1)
290                          + " \n");
291             }
292
293         } else // if (this.patternType ==2)
294
{ // Smooth Shading...
295
if (this.shading != null) {
296                 p.append("/Shading " + this.shading.referencePDF() + " \n");
297             }
298
299             if (this.xUID != null) {
300                 vectorSize = this.xUID.size();
301                 p.append("/XUID [ ");
302                 for (tempInt = 0; tempInt < vectorSize; tempInt++) {
303                     p.append(((Integer JavaDoc)this.xUID.get(tempInt)) + " ");
304                 }
305                 p.append("] \n");
306             }
307
308             if (this.extGState
309                     != null) { // will probably have to change this if it's used.
310
p.append("/ExtGState " + this.extGState + " \n");
311             }
312
313             if (this.matrix != null) {
314                 vectorSize = this.matrix.size();
315                 p.append("/Matrix [ ");
316                 for (tempInt = 0; tempInt < vectorSize; tempInt++) {
317                     p.append(PDFNumber.doubleOut((Double JavaDoc)this.matrix.get(tempInt)));
318                     p.append(" ");
319                 }
320                 p.append("] \n");
321             }
322         } // end of if patterntype =1...else 2.
323

324         p.append(">> \n");
325
326         // stream representing the function
327
if (this.patternDataStream != null) {
328             p.append("stream\n" + this.patternDataStream + "\nendstream\n");
329         }
330
331         p.append("endobj\n");
332
333         try {
334             return p.toString().getBytes(PDFDocument.ENCODING);
335         } catch (UnsupportedEncodingException JavaDoc ue) {
336             return p.toString().getBytes();
337         }
338     }
339
340 }
341
Popular Tags