KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: LazyFont.java,v 1.2.2.3 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 import org.apache.fop.render.pdf.Font;
54 import org.apache.fop.layout.FontDescriptor;
55 import org.apache.fop.pdf.PDFStream;
56 import org.apache.fop.messaging.MessageHandler;
57
58 import java.util.Map JavaDoc;
59 import java.net.URL JavaDoc;
60
61 import org.apache.fop.render.pdf.FontReader;
62
63 public class LazyFont extends Font implements FontDescriptor {
64
65     private URL JavaDoc metricsFile = null;
66     private URL JavaDoc fontEmbedPath = null;
67     private boolean useKerning = false;
68
69     private boolean isMetricsLoaded = false;
70     private Font realFont = null;
71     private FontDescriptor realFontDescriptor = null;
72
73     public LazyFont(URL JavaDoc fontEmbedPath, URL JavaDoc metricsFile, boolean useKerning){
74         this.metricsFile = metricsFile;
75         this.fontEmbedPath = fontEmbedPath;
76         this.useKerning = useKerning;
77     }
78
79     private void load(){
80         if (!isMetricsLoaded) {
81             isMetricsLoaded = true;
82             try{
83                 FontReader reader = new FontReader(metricsFile);
84                 reader.useKerning(useKerning);
85                 reader.setFontEmbedPath(fontEmbedPath);
86                 realFont = reader.getFont();
87                 if(realFont instanceof FontDescriptor){
88                     realFontDescriptor = (FontDescriptor) realFont;
89                 }
90                 // System.out.println("Metrics " + metricsFileName + " loaded.");
91
} catch (Exception JavaDoc ex) {
92                 MessageHandler.error("Failed to read font metrics file "
93                                      + metricsFile.toExternalForm()
94                                      + ": " + ex.getMessage());
95             }
96         }
97     }
98
99     public Font getRealFont(){
100         return realFont;
101     }
102
103     // Font
104
public String JavaDoc encoding(){
105         load();
106         return realFont.encoding();
107     }
108
109     public String JavaDoc fontName(){
110         load();
111         return realFont.fontName();
112     }
113
114     public byte getSubType(){
115         load();
116         return realFont.getSubType();
117     }
118
119     public char mapChar(char c){
120         load();
121         return realFont.mapChar(c);
122     }
123
124     // FontMetrics
125
public int getAscender(int size){
126         load();
127         return realFont.getAscender(size);
128     }
129
130     public int getCapHeight(int size){
131         load();
132         return realFont.getCapHeight(size);
133     }
134
135     public int getDescender(int size){
136         load();
137         return realFont.getDescender(size);
138     }
139
140     public int getXHeight(int size){
141         load();
142         return realFont.getXHeight(size);
143     }
144
145     public int getFirstChar(){
146         load();
147         return realFont.getFirstChar();
148     }
149
150     public int getLastChar(){
151         load();
152         return realFont.getLastChar();
153     }
154
155     public int width(int i, int size){
156         load();
157         return realFont.width(i, size);
158     }
159
160     public int[] getWidths(int size){
161         load();
162         return realFont.getWidths(size);
163     }
164
165     // FontDescriptor
166
public int getCapHeight(){
167         load();
168         return realFontDescriptor.getCapHeight();
169     }
170
171     public int getDescender(){
172         load();
173         return realFontDescriptor.getDescender();
174     }
175
176     public int getAscender(){
177         load();
178         return realFontDescriptor.getAscender();
179     }
180
181     public int getFlags(){
182         load();
183         return realFontDescriptor.getFlags();
184     }
185
186     public int[] getFontBBox(){
187         load();
188         return realFontDescriptor.getFontBBox();
189     }
190
191     public int getItalicAngle(){
192         load();
193         return realFontDescriptor.getItalicAngle();
194     }
195
196     public int getStemV(){
197         load();
198         return realFontDescriptor.getStemV();
199     }
200
201     public boolean hasKerningInfo(){
202         load();
203         return realFontDescriptor.hasKerningInfo();
204     }
205
206     public Map JavaDoc getKerningInfo(){
207         load();
208         return realFontDescriptor.getKerningInfo();
209     }
210
211     public boolean isEmbeddable(){
212         load();
213         return realFontDescriptor.isEmbeddable();
214     }
215
216     public PDFStream getFontFile(int objNum){
217         load();
218         return realFontDescriptor.getFontFile(objNum);
219     }
220 }
221
222
Popular Tags