KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: LazyFont.java 474387 2006-11-13 16:28:13Z jeremias $ */
19
20 package org.apache.fop.fonts;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Map JavaDoc;
25 import javax.xml.transform.Source JavaDoc;
26 import javax.xml.transform.stream.StreamSource JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.fop.apps.FOPException;
31 import org.xml.sax.InputSource JavaDoc;
32
33 /**
34  * This class is used to defer the loading of a font until it is really used.
35  */

36 public class LazyFont extends Typeface implements FontDescriptor {
37
38     private static Log log = LogFactory.getLog(LazyFont.class);
39     
40     private String JavaDoc metricsFileName = null;
41     private String JavaDoc fontEmbedPath = null;
42     private boolean useKerning = false;
43
44     private boolean isMetricsLoaded = false;
45     private Typeface realFont = null;
46     private FontDescriptor realFontDescriptor = null;
47
48     private FontResolver resolver = null;
49     
50     /**
51      * Main constructor
52      * @param fontEmbedPath path to embeddable file (may be null)
53      * @param metricsFileName path to the metrics XML file
54      * @param useKerning True, if kerning should be enabled
55      * @param resolver the font resolver to handle font URIs
56      */

57     public LazyFont(String JavaDoc fontEmbedPath, String JavaDoc metricsFileName
58                     , boolean useKerning, FontResolver resolver) {
59         this.metricsFileName = metricsFileName;
60         this.fontEmbedPath = fontEmbedPath;
61         this.useKerning = useKerning;
62         this.resolver = resolver;
63     }
64
65     private void load(boolean fail) {
66         if (!isMetricsLoaded) {
67             try {
68                 if (metricsFileName != null) {
69                     /**@todo Possible thread problem here */
70                     FontReader reader = null;
71                     if (resolver != null) {
72                         Source JavaDoc source = resolver.resolve(metricsFileName);
73                         if (source == null) {
74                             String JavaDoc err = "Cannot load font: failed to create Source from metrics file "
75                                 + metricsFileName;
76                             if (fail) {
77                                 throw new RuntimeException JavaDoc(err);
78                             } else {
79                                 log.error(err);
80                             }
81                             return;
82                         }
83                         InputStream JavaDoc in = null;
84                         if (source instanceof StreamSource JavaDoc) {
85                             in = ((StreamSource JavaDoc) source).getInputStream();
86                         }
87                         if (in == null && source.getSystemId() != null) {
88                             in = new java.net.URL JavaDoc(source.getSystemId()).openStream();
89                         }
90                         if (in == null) {
91                             String JavaDoc err = "Cannot load font: failed to create InputStream from"
92                                 + " Source for metrics file " + metricsFileName;
93                             if (fail) {
94                                 throw new RuntimeException JavaDoc(err);
95                             } else {
96                                 log.error(err);
97                             }
98                             return;
99                         }
100                         InputSource JavaDoc src = new InputSource JavaDoc(in);
101                         src.setSystemId(source.getSystemId());
102                         reader = new FontReader(src);
103                     } else {
104                         reader
105                             = new FontReader(new InputSource JavaDoc(new URL JavaDoc(metricsFileName).openStream()));
106                     }
107                     reader.setKerningEnabled(useKerning);
108                     reader.setFontEmbedPath(fontEmbedPath);
109                     reader.setResolver(resolver);
110                     realFont = reader.getFont();
111                 } else {
112                     if (fontEmbedPath == null) {
113                         throw new RuntimeException JavaDoc("Cannot load font. No font URIs available.");
114                     }
115                     realFont = FontLoader.loadFont(fontEmbedPath, resolver);
116                 }
117                 if (realFont instanceof FontDescriptor) {
118                     realFontDescriptor = (FontDescriptor) realFont;
119                 }
120             } catch (FOPException fopex) {
121                 log.error("Failed to read font metrics file " + metricsFileName, fopex);
122                 if (fail) {
123                     throw new RuntimeException JavaDoc(fopex.getMessage());
124                 }
125             } catch (IOException JavaDoc ioex) {
126                 log.error("Failed to read font metrics file " + metricsFileName, ioex);
127                 if (fail) {
128                     throw new RuntimeException JavaDoc(ioex.getMessage());
129                 }
130             }
131             isMetricsLoaded = true;
132         }
133     }
134
135     /**
136      * Gets the real font.
137      * @return the real font
138      */

139     public Typeface getRealFont() {
140         load(false);
141         return realFont;
142     }
143
144     // ---- Font ----
145
/**
146      * @see org.apache.fop.fonts.Typeface#getEncoding()
147      */

148     public String JavaDoc getEncoding() {
149         load(true);
150         return realFont.getEncoding();
151     }
152
153     /**
154      * @see org.apache.fop.fonts.Typeface#mapChar(char)
155      */

156     public char mapChar(char c) {
157         load(true);
158         return realFont.mapChar(c);
159     }
160
161     /**
162      * @see org.apache.fop.fonts.Typeface#hasChar(char)
163      */

164     public boolean hasChar(char c) {
165         load(true);
166         return realFont.hasChar(c);
167     }
168
169     /**
170      * @see org.apache.fop.fonts.Typeface#isMultiByte()
171      */

172     public boolean isMultiByte() {
173         load(true);
174         return realFont.isMultiByte();
175     }
176
177     // ---- FontMetrics interface ----
178
/**
179      * @see org.apache.fop.fonts.FontMetrics#getFontName()
180      */

181     public String JavaDoc getFontName() {
182         load(true);
183         return realFont.getFontName();
184     }
185
186     /**
187      * @see org.apache.fop.fonts.FontMetrics#getMaxAscent(int)
188      */

189     public int getMaxAscent(int size) {
190         load(true);
191         return realFont.getMaxAscent(size);
192     }
193
194     /**
195      * @see org.apache.fop.fonts.FontMetrics#getAscender(int)
196      */

197     public int getAscender(int size) {
198         load(true);
199         return realFont.getAscender(size);
200     }
201
202     /**
203      * @see org.apache.fop.fonts.FontMetrics#getCapHeight(int)
204      */

205     public int getCapHeight(int size) {
206         load(true);
207         return realFont.getCapHeight(size);
208     }
209
210     /**
211      * @see org.apache.fop.fonts.FontMetrics#getDescender(int)
212      */

213     public int getDescender(int size) {
214         load(true);
215         return realFont.getDescender(size);
216     }
217
218     /**
219      * @see org.apache.fop.fonts.FontMetrics#getXHeight(int)
220      */

221     public int getXHeight(int size) {
222         load(true);
223         return realFont.getXHeight(size);
224     }
225
226     /**
227      * @see org.apache.fop.fonts.FontMetrics#getWidth(int, int)
228      */

229     public int getWidth(int i, int size) {
230         load(true);
231         return realFont.getWidth(i, size);
232     }
233
234     /**
235      * @see org.apache.fop.fonts.FontMetrics#getWidths()
236      */

237     public int[] getWidths() {
238         load(true);
239         return realFont.getWidths();
240     }
241
242     /**
243      * @see org.apache.fop.fonts.FontMetrics#hasKerningInfo()
244      */

245     public boolean hasKerningInfo() {
246         load(true);
247         return realFont.hasKerningInfo();
248     }
249
250     /**
251      * @see org.apache.fop.fonts.FontMetrics#getKerningInfo()
252      */

253     public Map JavaDoc getKerningInfo() {
254         load(true);
255         return realFont.getKerningInfo();
256     }
257
258     // ---- FontDescriptor interface ----
259
/**
260      * @see org.apache.fop.fonts.FontDescriptor#getCapHeight()
261      */

262     public int getCapHeight() {
263         load(true);
264         return realFontDescriptor.getCapHeight();
265     }
266
267     /**
268      * @see org.apache.fop.fonts.FontDescriptor#getDescender()
269      */

270     public int getDescender() {
271         load(true);
272         return realFontDescriptor.getDescender();
273     }
274
275     /**
276      * @see org.apache.fop.fonts.FontDescriptor#getAscender()
277      */

278     public int getAscender() {
279         load(true);
280         return realFontDescriptor.getAscender();
281     }
282
283     /**
284      * @see org.apache.fop.fonts.FontDescriptor#getFlags()
285      */

286     public int getFlags() {
287         load(true);
288         return realFontDescriptor.getFlags();
289     }
290
291     /**
292      * @see org.apache.fop.fonts.FontDescriptor#getFontBBox()
293      */

294     public int[] getFontBBox() {
295         load(true);
296         return realFontDescriptor.getFontBBox();
297     }
298
299     /**
300      * @see org.apache.fop.fonts.FontDescriptor#getItalicAngle()
301      */

302     public int getItalicAngle() {
303         load(true);
304         return realFontDescriptor.getItalicAngle();
305     }
306
307     /**
308      * @see org.apache.fop.fonts.FontDescriptor#getStemV()
309      */

310     public int getStemV() {
311         load(true);
312         return realFontDescriptor.getStemV();
313     }
314
315     /**
316      * @see org.apache.fop.fonts.FontDescriptor#getFontType()
317      */

318     public FontType getFontType() {
319         load(true);
320         return realFontDescriptor.getFontType();
321     }
322
323     /**
324      * @see org.apache.fop.fonts.FontDescriptor#isEmbeddable()
325      */

326     public boolean isEmbeddable() {
327         load(true);
328         return realFontDescriptor.isEmbeddable();
329     }
330
331 }
332
333
Popular Tags