KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > pdf > fonts > SingleByteFont


1 /*
2  * $Id: SingleByteFont.java,v 1.3.2.5 2003/02/25 14:58:09 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.render.pdf.fonts;
52
53 //Java
54
import java.io.InputStream JavaDoc;
55 import java.io.BufferedInputStream JavaDoc;
56 import java.util.Map JavaDoc;
57 import java.net.URL JavaDoc;
58
59 //FOP
60
import org.apache.fop.messaging.MessageHandler;
61 import org.apache.fop.layout.FontDescriptor;
62 import org.apache.fop.pdf.PDFStream;
63 import org.apache.fop.pdf.PDFT1Stream;
64 import org.apache.fop.pdf.PDFTTFStream;
65 import org.apache.fop.render.pdf.CodePointMapping;
66 import org.apache.fop.render.pdf.Font;
67 import org.apache.fop.fonts.type1.PFBParser;
68 import org.apache.fop.fonts.type1.PFBData;
69 import org.apache.fop.tools.IOUtil;
70
71
72 /**
73  * Generic SingleByte font
74  */

75 public class SingleByteFont extends Font implements FontDescriptor {
76     public String JavaDoc fontName = null;
77     public String JavaDoc encoding = "WinAnsiEncoding";
78     private final CodePointMapping mapping
79         = CodePointMapping.getMapping("WinAnsiEncoding");
80
81     public int capHeight = 0;
82     public int xHeight = 0;
83     public int ascender = 0;
84     public int descender = 0;
85     public int[] fontBBox = {0, 0, 0, 0};
86
87     public URL JavaDoc embedFileName = null;
88     public String JavaDoc embedResourceName = null;
89     public PDFStream embeddedFont = null;
90
91     public int firstChar = 0;
92     public int lastChar = 255;
93     public int flags = 4;
94     public int stemV = 0;
95     public int italicAngle = 0;
96     public int missingWidth = 0;
97
98     public Map JavaDoc kerning = new java.util.HashMap JavaDoc();
99     public boolean useKerning = true;
100
101     public int width[] = null;
102     public byte subType = 0;
103
104     public final boolean hasKerningInfo() {
105         return (useKerning & kerning.isEmpty());
106     }
107
108     public final Map JavaDoc getKerningInfo() {
109         if (useKerning) {
110             return kerning;
111         } else {
112             return new java.util.HashMap JavaDoc();
113         }
114     }
115
116     public byte getSubType() {
117         return subType;
118     }
119
120     public int getAvgWidth() {
121         return -1;
122     }
123
124     public int getMinWidth() {
125         return -1;
126     }
127
128     public int getMaxWidth() {
129         return -1;
130     }
131
132     public int getleading() {
133         return -1;
134     }
135
136     public int getStemH() {
137         return 0;
138     }
139
140     public int getMissingWidth() {
141         return missingWidth;
142     }
143
144     public String JavaDoc getCharEncoding() {
145         return encoding;
146     }
147
148     public boolean isEmbeddable() {
149         return (embedFileName == null && embedResourceName == null) ? false
150                : true;
151     }
152
153
154     public PDFStream getFontFile(int i) {
155         InputStream JavaDoc instream = null;
156
157         // Get file first
158
if (embedFileName != null) try {
159             instream = embedFileName.openStream();
160         } catch (Exception JavaDoc e) {
161             MessageHandler.error("Failed to embed fontfile: "
162                                + embedFileName);
163         }
164
165             // Get resource
166
if (instream == null && embedResourceName != null) try {
167             instream =
168                 new BufferedInputStream JavaDoc(this.getClass().getResourceAsStream(embedResourceName));
169         } catch (Exception JavaDoc e) {
170             MessageHandler.error("Failed to embed fontresource: "
171                                + embedResourceName);
172         }
173
174         if (instream == null) {
175             return (PDFStream)null;
176         }
177
178         // Read fontdata
179
try {
180             if (subType == org.apache.fop.pdf.PDFFont.TYPE1) {
181                 PFBParser parser = new PFBParser();
182                 PFBData pfb = parser.parsePFB(instream);
183                 embeddedFont = new PDFT1Stream(i);
184                 ((PDFT1Stream)embeddedFont).setData(pfb);
185             } else {
186                 byte[] file = IOUtil.toByteArray(instream, 128000);
187                 embeddedFont = new PDFTTFStream(i, file.length);
188                 ((PDFTTFStream)embeddedFont).setData(file, file.length);
189             }
190
191             embeddedFont.addFilter("flate");
192             embeddedFont.addFilter("ascii-85");
193             instream.close();
194         } catch (Exception JavaDoc e) {
195             MessageHandler.error("Failed to read font data for embedded font: "+e.getMessage());
196         }
197
198         return (PDFStream)embeddedFont;
199     }
200
201     public String JavaDoc encoding() {
202         return encoding;
203     }
204
205     public String JavaDoc fontName() {
206         return fontName;
207     }
208
209     public int getAscender() {
210         return ascender;
211     }
212
213     public int getDescender() {
214         return descender;
215     }
216
217     public int getCapHeight() {
218         return capHeight;
219     }
220
221     public int getAscender(int size) {
222         return size * ascender;
223     }
224
225     public int getCapHeight(int size) {
226         return size * capHeight;
227     }
228
229     public int getDescender(int size) {
230         return size * descender;
231     }
232
233     public int getXHeight(int size) {
234         return size * xHeight;
235     }
236
237     public int getFlags() {
238         return flags;
239     }
240
241     public int[] getFontBBox() {
242         return fontBBox;
243     }
244
245     public int getItalicAngle() {
246         return italicAngle;
247     }
248
249     public int getStemV() {
250         return stemV;
251     }
252
253     public int getFirstChar() {
254         return 0;
255         // return firstChar;
256
}
257
258     public int getLastChar() {
259         return lastChar;
260     }
261
262     public int width(int i, int size) {
263         return size * width[i];
264     }
265
266     public int[] getWidths(int size) {
267         int[] arr = new int[width.length];
268         System.arraycopy(width, 0, arr, 0, width.length - 1);
269         for (int i = 0; i < arr.length; i++) {
270             arr[i] *= size;
271         }
272         return arr;
273     }
274
275     public char mapChar(char c) {
276         char d = mapping.mapChar(c);
277         if(d != 0) {
278             return d;
279         } else {
280             return '#';
281         }
282     }
283
284 }
285
286
Popular Tags