KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PDFCIDFont.java,v 1.3.2.2 2003/02/25 14:29:37 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
56 // based on work by Takayuki Takeuchi
57

58 /**
59  * class representing a "character identifier" font (p 210 and onwards).
60  */

61 public class PDFCIDFont extends PDFObject {
62
63     public static final byte CID_TYPE0 = 0;
64     public static final byte CID_TYPE2 = 1;
65     protected static final String JavaDoc[] TYPE_NAMES = {
66         "CIDFontType0", "CIDFontType2"
67     };
68
69     protected String JavaDoc basefont;
70
71     protected String JavaDoc cidtype;
72     protected Integer JavaDoc dw;
73     protected PDFWArray w;
74     protected int[] dw2;
75     protected PDFWArray w2;
76     protected PDFCIDSystemInfo systemInfo;
77     protected PDFCIDFontDescriptor descriptor;
78     protected PDFCMap cmap;
79
80     /**
81      * /CIDToGIDMap (only for CIDFontType2, see p 212)
82      * can be either "Identity" (default) or a PDFStream
83      */

84     protected PDFStream cidMap;
85
86     // compatibility with Takayuki Takeuchi
87

88     /**
89      * create the /Font object
90      */

91     public PDFCIDFont(int number, String JavaDoc basefont, byte cidtype, int dw,
92                       int[] w, String JavaDoc registry, String JavaDoc ordering,
93                       int supplement, PDFCIDFontDescriptor descriptor) {
94
95         super(number);
96
97         this.basefont = basefont;
98         this.cidtype = TYPE_NAMES[(int)cidtype];
99         this.dw = new Integer JavaDoc(dw);
100         this.w = new PDFWArray();
101         this.w.addEntry(0, w);
102         this.dw2 = null;
103         this.w2 = null;
104         this.systemInfo = new PDFCIDSystemInfo(registry, ordering,
105                                                supplement);
106         this.descriptor = descriptor;
107         this.cidMap = null;
108         this.cmap = null;
109     }
110
111     /**
112      * create the /Font object
113      */

114     public PDFCIDFont(int number, String JavaDoc basefont, byte cidtype, int dw,
115                       PDFWArray w, PDFCIDSystemInfo systemInfo,
116                       PDFCIDFontDescriptor descriptor) {
117
118         super(number);
119
120         this.basefont = basefont;
121         this.cidtype = TYPE_NAMES[(int)cidtype];
122         this.dw = new Integer JavaDoc(dw);
123         this.w = w;
124         this.dw2 = null;
125         this.w2 = null;
126         this.systemInfo = systemInfo;
127         this.descriptor = descriptor;
128         this.cidMap = null;
129         this.cmap = null;
130     }
131
132     /**
133      * set the /DW attribute
134      */

135     public void setDW(int dw) {
136         this.dw = new Integer JavaDoc(dw);
137     }
138
139     /**
140      * set the /W array
141      */

142     public void setW(PDFWArray w) {
143         this.w = w;
144     }
145
146     /**
147      * set the (two elements) /DW2 array
148      */

149     public void setDW2(int[] dw2) {
150         this.dw2 = dw2;
151     }
152
153     /**
154      * set the two elements of the /DW2 array
155      */

156     public void setDW2(int posY, int displacementY) {
157         this.dw2 = new int[] {
158             posY, displacementY
159         };
160     }
161
162     /**
163      * Set the CMap used as /ToUnicode cmap
164      */

165     public void setCMAP(PDFCMap cmap) {
166         this.cmap = cmap;
167     }
168
169     /**
170      * set the /W2 array
171      */

172     public void setW2(PDFWArray w2) {
173         this.w2 = w2;
174     }
175
176     /**
177      * set the /CIDToGIDMap (to be used only for CIDFontType2)
178      */

179     public void setCIDMap(PDFStream map) {
180         this.cidMap = map;
181     }
182
183     /**
184      * set the /CIDToGIDMap (to be used only for CIDFontType2) to "Identity"
185      */

186     public void setCIDMapIdentity() {
187         this.cidMap = null; // not an error here, simply use the default
188
}
189
190     /**
191      * produce the PDF representation for the object
192      *
193      * @return the PDF
194      */

195     public byte[] toPDF() {
196         try {
197             return toPDFString().getBytes(PDFDocument.ENCODING);
198         } catch (UnsupportedEncodingException JavaDoc ue) {
199             return toPDFString().getBytes();
200         }
201     }
202
203     public String JavaDoc toPDFString() {
204         StringBuffer JavaDoc p = new StringBuffer JavaDoc();
205         p.append(this.number);
206         p.append(" ");
207         p.append(this.generation);
208         p.append(" obj\n<< /Type /Font");
209         p.append("\n/BaseFont /");
210         p.append(this.basefont);
211         if (cidMap != null) {
212             p.append(" \n/CIDToGIDMap ");
213             p.append(cidMap.referencePDF());
214         }
215         p.append(" \n/Subtype /");
216         p.append(this.cidtype);
217         p.append("\n");
218         p.append(systemInfo.toPDFString());
219         p.append("\n/FontDescriptor ");
220         p.append(this.descriptor.referencePDF());
221
222         if (cmap != null) {
223             p.append("\n/ToUnicode ");
224             p.append(cmap.referencePDF());
225         }
226         if (dw != null) {
227             p.append("\n/DW ");
228             p.append(this.dw);
229         }
230         if (w != null) {
231             p.append("\n/W ");
232             p.append(w.toPDFString());
233         }
234         if (dw2 != null) {
235             p.append("\n/DW2 ["); // always two values, see p 211
236
p.append(this.dw2[0]);
237             p.append(this.dw2[1]);
238             p.append("] \n>>\nendobj\n");
239         }
240         if (w2 != null) {
241             p.append("\n/W2 ");
242             p.append(w2.toPDFString());
243             p.append(" \n>>\nendobj\n");
244         }
245         p.append(" \n>>\nendobj\n");
246         return p.toString();
247     }
248
249 }
250
251
Popular Tags