1 17 18 19 20 package org.apache.fop.fonts; 21 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 27 import javax.xml.transform.Source ; 28 import javax.xml.transform.stream.StreamSource ; 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 37 public abstract class FontLoader { 38 39 46 public static CustomFont loadFont(String fontFileURI, FontResolver resolver) 47 throws IOException { 48 FontLoader loader; 49 fontFileURI = fontFileURI.trim(); 50 String name = fontFileURI.toLowerCase(); 51 String 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 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 openFontFile(FontResolver resolver, String uri) 74 throws IOException , MalformedURLException { 75 InputStream in = null; 76 if (resolver != null) { 77 Source source = resolver.resolve(uri); 78 if (source == null) { 79 String err = "Cannot load font: failed to create Source for font file " 80 + uri; 81 throw new IOException (err); 82 } 83 if (source instanceof StreamSource ) { 84 in = ((StreamSource ) source).getInputStream(); 85 } 86 if (in == null && source.getSystemId() != null) { 87 in = new java.net.URL (source.getSystemId()).openStream(); 88 } 89 if (in == null) { 90 String err = "Cannot load font: failed to create InputStream from" 91 + " Source for font file " + uri; 92 throw new IOException (err); 93 } 94 } else { 95 in = new URL (uri).openStream(); 96 } 97 return in; 98 } 99 100 103 public abstract CustomFont getFont(); 104 105 106 } 107 | Popular Tags |