KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PDFFont.java,v 1.10.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 /**
57  * class representing a /Font object.
58  *
59  * A more complete object expressing the base font name and encoding of a
60  * font along with an internal name for the font used within
61  * streams of content.
62  *
63  * Fonts are specified on page 198 and onwards of the PDF 1.3 spec.
64  */

65 public class PDFFont extends PDFObject {
66
67     /**
68      * font subtype to be used as parameter to createFont()
69      */

70     public static final byte TYPE0 = 0;
71
72     /**
73      * font subtype to be used as parameter to createFont()
74      */

75     public static final byte TYPE1 = 1;
76
77     /**
78      * font subtype to be used as parameter to createFont()
79      */

80     public static final byte MMTYPE1 = 2;
81
82     /**
83      * font subtype to be used as parameter to createFont()
84      */

85     public static final byte TYPE3 = 3;
86
87     /**
88      * font subtype to be used as parameter to createFont()
89      */

90     public static final byte TRUETYPE = 4;
91
92     /**
93      * font subtype names as output in the PDF
94      */

95     protected static final String JavaDoc[] TYPE_NAMES =
96         new String JavaDoc[] // take care of the order
97
{
98         "Type0", "Type1", "MMType1", "Type3", "TrueType"
99     };
100
101     /**
102      * the internal name for the font (eg "F1")
103      */

104     protected String JavaDoc fontname;
105
106     /**
107      * the font's subtype
108      * (as defined by the constants TYPE0, TYPE1, MMTYPE1, TYPE3, TRUETYPE)
109      */

110     protected byte subtype;
111
112     /**
113      * the base font name (eg "Helvetica")
114      */

115     protected String JavaDoc basefont;
116
117     /**
118      * the character encoding scheme used by the font.
119      * It can be a String for standard encodings, or
120      * a PDFEncoding for a more complex scheme, or
121      * a PDFStream containing a CMap in a Type0 font.
122      * If <code>null</code> then not written out in the PDF.
123      */

124     protected Object JavaDoc encoding;
125
126     /**
127      * the Unicode mapping mechanism
128      */

129     // protected PDFToUnicode mapping;
130

131     /**
132      * create the /Font object
133      *
134      * @param number the object's number
135      * @param fontname the internal name for the font
136      * @param subtype the font's subtype
137      * @param basefont the base font name
138      * @param encoding the character encoding schema used by the font
139      * @param mapping the Unicode mapping mechanism
140      */

141     public PDFFont(int number, String JavaDoc fontname, byte subtype,
142                    String JavaDoc basefont,
143                    Object JavaDoc encoding /* , PDFToUnicode mapping */) {
144
145         /* generic creation of PDF object */
146         super(number);
147
148         /* set fields using paramaters */
149         this.fontname = fontname;
150         this.subtype = subtype;
151         this.basefont = basefont;
152         this.encoding = encoding;
153         // this.mapping = mapping;
154
}
155
156     /**
157      * factory method with the basic parameters
158      *
159      * @param number the object's number
160      * @param fontname the internal name for the font
161      * @param subtype the font's subtype
162      * @param basefont the base font name
163      * @param encoding the character encoding schema used by the font
164      */

165     public static PDFFont createFont(int number, String JavaDoc fontname,
166                                      byte subtype, String JavaDoc basefont,
167                                      Object JavaDoc encoding) {
168         switch (subtype) {
169         case TYPE0:
170             return new PDFFontType0(number, fontname, subtype, basefont,
171                                     encoding);
172         case TYPE1:
173         case MMTYPE1:
174             return new PDFFontType1(number, fontname, subtype, basefont,
175                                     encoding);
176         /*
177          * case TYPE3 :
178          * return new PDFFontType3(number, fontname, subtype, basefont, encoding);
179          */

180         case TRUETYPE:
181             return new PDFFontTrueType(number, fontname, subtype, basefont,
182                                        encoding);
183         }
184         return null; // should not happend
185
}
186
187     /**
188      * factory method with the extended parameters
189      * for Type1, MMType1 and TrueType
190      *
191      * @param number the object's number
192      * @param fontname the internal name for the font
193      * @param subtype the font's subtype
194      * @param basefont the base font name
195      * @param encoding the character encoding schema used by the font
196      * @param firstChar the first character code in the font
197      * @param lastChar the last character code in the font
198      * @param widths an array of size (lastChar - firstChar +1)
199      * @param descriptor the descriptor for other font's metrics
200      */

201     public static PDFFont createFont(int number, String JavaDoc fontname,
202                                      byte subtype, String JavaDoc basefont,
203                                      Object JavaDoc encoding, int firstChar,
204                                      int lastChar, PDFArray widths,
205                                      PDFFontDescriptor descriptor) {
206
207         PDFFontNonBase14 font;
208         switch (subtype) {
209         case TYPE0:
210             font = new PDFFontType0(number, fontname, subtype, basefont,
211                                     encoding);
212             font.setDescriptor(descriptor);
213             return font;
214         case TYPE1:
215         case MMTYPE1:
216             font = new PDFFontType1(number, fontname, subtype, basefont,
217                                     encoding);
218             font.setWidthMetrics(firstChar, lastChar, widths);
219             font.setDescriptor(descriptor);
220             return font;
221         case TYPE3:
222             return null; // should not happend
223

224         case TRUETYPE:
225             font = new PDFFontTrueType(number, fontname, subtype, basefont,
226                                        encoding);
227             font.setWidthMetrics(firstChar, lastChar, widths);
228             font.setDescriptor(descriptor);
229             return font;
230
231         }
232         return null; // should not happend
233
}
234
235     /**
236      * get the internal name used for this font
237      *
238      * @return the internal name
239      */

240     public String JavaDoc getName() {
241         return this.fontname;
242     }
243
244     /**
245      * produce the PDF representation for the object
246      *
247      * @return the PDF
248      */

249     public byte[] toPDF() {
250         StringBuffer JavaDoc p = new StringBuffer JavaDoc();
251         p.append(this.number + " " + this.generation
252                  + " obj\n<< /Type /Font\n/Subtype /"
253                  + TYPE_NAMES[this.subtype] + "\n/Name /" + this.fontname
254                  + "\n/BaseFont /" + this.basefont);
255         if (encoding != null) {
256             p.append("\n/Encoding ");
257             if (encoding instanceof PDFEncoding) {
258                 p.append(((PDFEncoding)this.encoding).referencePDF());
259             } else if (encoding instanceof PDFStream) {
260                 p.append(((PDFStream)this.encoding).referencePDF());
261             } else {
262                 p.append("/").append((String JavaDoc)encoding);
263             }
264         }
265         fillInPDF(p);
266         p.append(" >>\nendobj\n");
267
268         try {
269             return p.toString().getBytes(PDFDocument.ENCODING);
270         } catch (UnsupportedEncodingException JavaDoc ue) {
271             return p.toString().getBytes();
272         }
273     }
274
275     /**
276      * fill in the specifics for the font's subtype.
277      *
278      * the given buffer already contains the fields common to all font types.
279      *
280      * @param begin the buffer to be completed with the type specific fields
281      */

282     protected void fillInPDF(StringBuffer JavaDoc begin) {}
283
284 }
285
Popular Tags