KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: PDFFontDescriptor.java,v 1.5.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 descriptor.
58  *
59  * Font descriptors are specified on page 222 and onwards of the PDF 1.3 spec.
60  */

61 public class PDFFontDescriptor extends PDFObject {
62
63     // Required fields
64
protected int ascent;
65     protected int capHeight;
66     protected int descent;
67     protected int flags;
68     protected PDFRectangle fontBBox;
69     protected String JavaDoc basefont; // PDF-spec: FontName
70
protected int italicAngle;
71     protected int stemV;
72     // Optional fields
73
protected int stemH = 0;
74     protected int xHeight = 0;
75     protected int leading = 0;
76     protected int avgWidth = 0;
77     protected int maxWidth = 0;
78     protected int missingWidth = 0;
79     protected PDFStream fontfile;
80     // protected String charSet = null;
81

82     protected byte subtype;
83
84     /**
85      * create the /FontDescriptor object
86      *
87      * @param number the object's number
88      * @param ascent the maximum height above the baseline
89      * @param descent the maximum depth below the baseline
90      * @param capHeight height of the capital letters
91      * @param flags various characteristics of the font
92      * @param fontBBox the bounding box for the described font
93      * @param basefont the base font name
94      * @param italicAngle the angle of the vertical dominant strokes
95      * @param stemV the width of the dominant vertical stems of glyphs
96      */

97     public PDFFontDescriptor(int number, String JavaDoc basefont, int ascent,
98                              int descent, int capHeight, int flags,
99                              PDFRectangle fontBBox, int italicAngle,
100                              int stemV) {
101
102         /* generic creation of PDF object */
103         super(number);
104
105         /* set fields using paramaters */
106         this.basefont = basefont;
107         this.ascent = ascent;
108         this.descent = descent;
109         this.capHeight = capHeight;
110         this.flags = flags;
111         this.fontBBox = fontBBox;
112         this.italicAngle = italicAngle;
113         this.stemV = stemV;
114     }
115
116     /**
117      * set the optional metrics
118      */

119     public void setMetrics(int avgWidth, int maxWidth, int missingWidth,
120                            int leading, int stemH, int xHeight) {
121         this.avgWidth = avgWidth;
122         this.maxWidth = maxWidth;
123         this.missingWidth = missingWidth;
124         this.leading = leading;
125         this.stemH = stemH;
126         this.xHeight = xHeight;
127     }
128
129     /**
130      * set the optional font file stream
131      *
132      * @param subtype the font type defined in the font stream
133      * @param fontfile the stream containing an embedded font
134      */

135     public void setFontFile(byte subtype, PDFStream fontfile) {
136         this.subtype = subtype;
137         this.fontfile = fontfile;
138     }
139
140     // public void setCharSet(){}//for subset fonts
141

142     /**
143      * produce the PDF representation for the object
144      *
145      * @return the PDF
146      */

147     public byte[] toPDF() {
148         StringBuffer JavaDoc p = new StringBuffer JavaDoc(this.number + " " + this.generation
149                                           + " obj\n<< /Type /FontDescriptor"
150                                           + "\n/FontName /" + this.basefont);
151
152         p.append("\n/FontBBox ");
153         p.append(fontBBox.toPDFString());
154         p.append("\n/Flags ");
155         p.append(flags);
156         p.append("\n/CapHeight ");
157         p.append(capHeight);
158         p.append("\n/Ascent ");
159         p.append(ascent);
160         p.append("\n/Descent ");
161         p.append(descent);
162         p.append("\n/ItalicAngle ");
163         p.append(italicAngle);
164         p.append("\n/StemV ");
165         p.append(stemV);
166         // optional fields
167
if (stemH != 0) {
168             p.append("\n/StemH ");
169             p.append(stemH);
170         }
171         if (xHeight != 0) {
172             p.append("\n/XHeight ");
173             p.append(xHeight);
174         }
175         if (avgWidth != 0) {
176             p.append("\n/AvgWidth ");
177             p.append(avgWidth);
178         }
179         if (maxWidth != 0) {
180             p.append("\n/MaxWidth ");
181             p.append(maxWidth);
182         }
183         if (missingWidth != 0) {
184             p.append("\n/MissingWidth ");
185             p.append(missingWidth);
186         }
187         if (leading != 0) {
188             p.append("\n/Leading ");
189             p.append(leading);
190         }
191         if (fontfile != null) {
192             switch (subtype) {
193             case PDFFont.TYPE1:
194                 p.append("\n/FontFile ");
195                 break;
196             case PDFFont.TRUETYPE:
197                 p.append("\n/FontFile2 ");
198                 break;
199             case PDFFont.TYPE0:
200                 p.append("\n/FontFile2 ");
201                 break;
202             default:
203                 p.append("\n/FontFile2 ");
204             }
205             p.append(fontfile.referencePDF());
206         }
207         // charSet for subset fonts // not yet implemented
208
// CID optional field
209
fillInPDF(p);
210         p.append("\n >>\nendobj\n");
211
212         try {
213             return p.toString().getBytes(PDFDocument.ENCODING);
214         } catch (UnsupportedEncodingException JavaDoc ue) {
215             return p.toString().getBytes();
216         }
217     }
218
219     /**
220      * fill in the specifics for the font's descriptor.
221      *
222      * the given buffer already contains the fields common to all descriptors.
223      *
224      * @param begin the buffer to be completed with the specific fields
225      */

226     protected void fillInPDF(StringBuffer JavaDoc begin) {}
227
228 }
229
Popular Tags