KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PDFResources.java,v 1.10.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.Iterator JavaDoc;
56 import java.util.List JavaDoc;
57 import java.util.Map JavaDoc;
58
59 /**
60  * class representing a /Resources object.
61  *
62  * /Resources object contain a list of references to the fonts for the
63  * document
64  */

65 public class PDFResources extends PDFObject {
66
67     /**
68      * /Font objects keyed by their internal name
69      */

70     protected Map JavaDoc fonts = new java.util.HashMap JavaDoc();
71
72     protected List JavaDoc xObjects = null;
73     protected List JavaDoc patterns = new java.util.ArrayList JavaDoc();
74     protected List JavaDoc shadings = new java.util.ArrayList JavaDoc();
75
76     /**
77      * create a /Resources object.
78      *
79      * @param number the object's number
80      */

81     public PDFResources(int number) {
82
83         /* generic creation of object */
84         super(number);
85
86     }
87
88     /**
89      * add font object to resources list.
90      *
91      * @param font the PDFFont to add
92      */

93     public void addFont(PDFFont font) {
94         this.fonts.put(font.getName(), font);
95     }
96
97     public void addShading(PDFShading theShading) {
98         this.shadings.add(theShading);
99     }
100
101     public void addPattern(PDFPattern thePattern) {
102         this.patterns.add(thePattern);
103     }
104
105     public void setXObjects(List JavaDoc xObjects) {
106         this.xObjects = xObjects;
107     }
108
109     /**
110      * represent the object in PDF
111      *
112      * @return the PDF
113      */

114     public byte[] toPDF() {
115         StringBuffer JavaDoc p = new StringBuffer JavaDoc(this.number + " " + this.generation
116                                           + " obj\n<< \n");
117         if (!this.fonts.isEmpty()) {
118             p.append("/Font << ");
119
120             /* construct PDF dictionary of font object references */
121             Iterator JavaDoc fontIterator = this.fonts.keySet().iterator();
122             while (fontIterator.hasNext()) {
123                 String JavaDoc fontName = (String JavaDoc)fontIterator.next();
124                 p.append("/" + fontName + " "
125                          + ((PDFFont)this.fonts.get(fontName)).referencePDF()
126                          + " ");
127             }
128
129             p.append(">> \n");
130         }
131
132         PDFShading currentShading = null;
133         if (!this.shadings.isEmpty()) {
134             p.append("/Shading << ");
135
136             for (int currentShadingNumber = 0;
137                     currentShadingNumber < this.shadings.size();
138                     currentShadingNumber++) {
139                 currentShading =
140                     ((PDFShading)this.shadings.get(currentShadingNumber));
141
142                 p.append("/" + currentShading.getName() + " "
143                          + currentShading.referencePDF() + " "); // \n ??????
144
}
145
146             p.append(">> \n");
147         }
148         // "free" the memory. Sorta.
149
currentShading = null;
150
151         PDFPattern currentPattern = null;
152         if (!this.patterns.isEmpty()) {
153             p.append("/Pattern << ");
154
155             for (int currentPatternNumber = 0;
156                     currentPatternNumber < this.patterns.size();
157                     currentPatternNumber++) {
158                 currentPattern =
159                     ((PDFPattern)this.patterns.get(currentPatternNumber));
160
161                 p.append("/" + currentPattern.getName() + " "
162                          + currentPattern.referencePDF() + " ");
163             }
164
165             p.append(">> \n");
166         }
167         // "free" the memory. Sorta.
168
currentPattern = null;
169
170         p.append("/ProcSet [ /PDF /ImageC /Text ] ");
171
172         if (!this.xObjects.isEmpty()) {
173             p = p.append("/XObject <<");
174             for (int i = 1; i <= this.xObjects.size(); i++) {
175                 p = p.append("/Im" + i + " "
176                              + ((PDFXObject)this.xObjects.get(i - 1)).referencePDF()
177                              + " \n");
178             }
179             p = p.append(" >>\n");
180         }
181
182         p = p.append(">> \nendobj\n");
183
184         try {
185             return p.toString().getBytes(PDFDocument.ENCODING);
186         } catch (UnsupportedEncodingException JavaDoc ue) {
187             return p.toString().getBytes();
188         }
189     }
190
191 }
192
Popular Tags