KickJava   Java API By Example, From Geeks To Geeks.

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


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$ */
19
20 package org.apache.fop.fonts;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26
27 import javax.xml.transform.Source JavaDoc;
28 import javax.xml.transform.stream.StreamSource JavaDoc;
29
30 import org.apache.commons.io.IOUtils;
31 import org.apache.fop.fonts.truetype.TTFFontLoader;
32 import org.apache.fop.fonts.type1.Type1FontLoader;
33
34 /**
35  * Base class for font loaders.
36  */

37 public abstract class FontLoader {
38
39     /**
40      * Loads a custom font from a URI. In the case of Type 1 fonts, the PFB file must be specified.
41      * @param fontFileURI the URI to the font
42      * @param resolver the font resolver to use when resolving URIs
43      * @return the newly loaded font
44      * @throws IOException In case of an I/O error
45      */

46     public static CustomFont loadFont(String JavaDoc fontFileURI, FontResolver resolver)
47                 throws IOException JavaDoc {
48         FontLoader loader;
49         fontFileURI = fontFileURI.trim();
50         String JavaDoc name = fontFileURI.toLowerCase();
51         String JavaDoc effURI;
52         boolean type1 = false;
53         if (name.endsWith(".pfb")) {
54             type1 = true;
55             effURI = name.substring(0, fontFileURI.length() - 4) + ".pfm";
56         } else {
57             effURI = fontFileURI;
58         }
59         
60         InputStream JavaDoc in = openFontFile(resolver, effURI);
61         try {
62             if (type1) {
63                 loader = new Type1FontLoader(fontFileURI, in, resolver);
64             } else {
65                 loader = new TTFFontLoader(fontFileURI, in, resolver);
66             }
67             return loader.getFont();
68         } finally {
69             IOUtils.closeQuietly(in);
70         }
71     }
72
73     private static InputStream JavaDoc openFontFile(FontResolver resolver, String JavaDoc uri)
74                     throws IOException JavaDoc, MalformedURLException JavaDoc {
75         InputStream JavaDoc in = null;
76         if (resolver != null) {
77             Source JavaDoc source = resolver.resolve(uri);
78             if (source == null) {
79                 String JavaDoc err = "Cannot load font: failed to create Source for font file "
80                     + uri;
81                 throw new IOException JavaDoc(err);
82             }
83             if (source instanceof StreamSource JavaDoc) {
84                 in = ((StreamSource JavaDoc) source).getInputStream();
85             }
86             if (in == null && source.getSystemId() != null) {
87                 in = new java.net.URL JavaDoc(source.getSystemId()).openStream();
88             }
89             if (in == null) {
90                 String JavaDoc err = "Cannot load font: failed to create InputStream from"
91                     + " Source for font file " + uri;
92                 throw new IOException JavaDoc(err);
93             }
94         } else {
95             in = new URL JavaDoc(uri).openStream();
96         }
97         return in;
98     }
99     
100     /**
101      * @return the font loaded by this loader
102      */

103     public abstract CustomFont getFont();
104
105     
106 }
107
Popular Tags