1 17 18 19 20 package org.apache.fop.fonts; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.URL ; 24 import java.util.Map ; 25 import javax.xml.transform.Source ; 26 import javax.xml.transform.stream.StreamSource ; 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 ; 32 33 36 public class LazyFont extends Typeface implements FontDescriptor { 37 38 private static Log log = LogFactory.getLog(LazyFont.class); 39 40 private String metricsFileName = null; 41 private String 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 57 public LazyFont(String fontEmbedPath, String 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 70 FontReader reader = null; 71 if (resolver != null) { 72 Source source = resolver.resolve(metricsFileName); 73 if (source == null) { 74 String err = "Cannot load font: failed to create Source from metrics file " 75 + metricsFileName; 76 if (fail) { 77 throw new RuntimeException (err); 78 } else { 79 log.error(err); 80 } 81 return; 82 } 83 InputStream in = null; 84 if (source instanceof StreamSource ) { 85 in = ((StreamSource ) source).getInputStream(); 86 } 87 if (in == null && source.getSystemId() != null) { 88 in = new java.net.URL (source.getSystemId()).openStream(); 89 } 90 if (in == null) { 91 String err = "Cannot load font: failed to create InputStream from" 92 + " Source for metrics file " + metricsFileName; 93 if (fail) { 94 throw new RuntimeException (err); 95 } else { 96 log.error(err); 97 } 98 return; 99 } 100 InputSource src = new InputSource (in); 101 src.setSystemId(source.getSystemId()); 102 reader = new FontReader(src); 103 } else { 104 reader 105 = new FontReader(new InputSource (new URL (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 ("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 (fopex.getMessage()); 124 } 125 } catch (IOException ioex) { 126 log.error("Failed to read font metrics file " + metricsFileName, ioex); 127 if (fail) { 128 throw new RuntimeException (ioex.getMessage()); 129 } 130 } 131 isMetricsLoaded = true; 132 } 133 } 134 135 139 public Typeface getRealFont() { 140 load(false); 141 return realFont; 142 } 143 144 148 public String getEncoding() { 149 load(true); 150 return realFont.getEncoding(); 151 } 152 153 156 public char mapChar(char c) { 157 load(true); 158 return realFont.mapChar(c); 159 } 160 161 164 public boolean hasChar(char c) { 165 load(true); 166 return realFont.hasChar(c); 167 } 168 169 172 public boolean isMultiByte() { 173 load(true); 174 return realFont.isMultiByte(); 175 } 176 177 181 public String getFontName() { 182 load(true); 183 return realFont.getFontName(); 184 } 185 186 189 public int getMaxAscent(int size) { 190 load(true); 191 return realFont.getMaxAscent(size); 192 } 193 194 197 public int getAscender(int size) { 198 load(true); 199 return realFont.getAscender(size); 200 } 201 202 205 public int getCapHeight(int size) { 206 load(true); 207 return realFont.getCapHeight(size); 208 } 209 210 213 public int getDescender(int size) { 214 load(true); 215 return realFont.getDescender(size); 216 } 217 218 221 public int getXHeight(int size) { 222 load(true); 223 return realFont.getXHeight(size); 224 } 225 226 229 public int getWidth(int i, int size) { 230 load(true); 231 return realFont.getWidth(i, size); 232 } 233 234 237 public int[] getWidths() { 238 load(true); 239 return realFont.getWidths(); 240 } 241 242 245 public boolean hasKerningInfo() { 246 load(true); 247 return realFont.hasKerningInfo(); 248 } 249 250 253 public Map getKerningInfo() { 254 load(true); 255 return realFont.getKerningInfo(); 256 } 257 258 262 public int getCapHeight() { 263 load(true); 264 return realFontDescriptor.getCapHeight(); 265 } 266 267 270 public int getDescender() { 271 load(true); 272 return realFontDescriptor.getDescender(); 273 } 274 275 278 public int getAscender() { 279 load(true); 280 return realFontDescriptor.getAscender(); 281 } 282 283 286 public int getFlags() { 287 load(true); 288 return realFontDescriptor.getFlags(); 289 } 290 291 294 public int[] getFontBBox() { 295 load(true); 296 return realFontDescriptor.getFontBBox(); 297 } 298 299 302 public int getItalicAngle() { 303 load(true); 304 return realFontDescriptor.getItalicAngle(); 305 } 306 307 310 public int getStemV() { 311 load(true); 312 return realFontDescriptor.getStemV(); 313 } 314 315 318 public FontType getFontType() { 319 load(true); 320 return realFontDescriptor.getFontType(); 321 } 322 323 326 public boolean isEmbeddable() { 327 load(true); 328 return realFontDescriptor.isEmbeddable(); 329 } 330 331 } 332 333 | Popular Tags |