package org.apache.fop.fonts;
import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.xml.sax.InputSource;
this.userAgent = userAgent;
}
- private void load() {
+ private void load(boolean fail) {
if (!isMetricsLoaded) {
- isMetricsLoaded = true;
try {
/**@todo Possible thread problem here */
FontReader reader = null;
Source source = userAgent.resolveURI(metricsFileName
, userAgent.getFontBaseURL());
if (source == null) {
- log.error("Failed to create Source from metrics file " + metricsFileName);
+ String err = "Cannot load font: failed to create Source from metrics file "
+ + metricsFileName;
+ if (fail) {
+ throw new RuntimeException(err);
+ } else {
+ log.error(err);
+ }
return;
}
InputStream in = null;
in = new java.net.URL(source.getSystemId()).openStream();
}
if (in == null) {
- log.error("Failed to create InputStream from Source for metrics file "
- + metricsFileName);
+ String err = "Cannot load font: failed to create InputStream from"
+ + " Source for metrics file " + metricsFileName;
+ if (fail) {
+ throw new RuntimeException(err);
+ } else {
+ log.error(err);
+ }
return;
}
reader = new FontReader(new InputSource(in));
realFontDescriptor = (FontDescriptor) realFont;
}
// log.debug("Metrics " + metricsFileName + " loaded.");
- } catch (Exception ex) {
- log.error("Failed to read font metrics file " + metricsFileName, ex);
+ } catch (FOPException fopex) {
+ log.error("Failed to read font metrics file " + metricsFileName, fopex);
+ if (fail) {
+ throw new RuntimeException(fopex.getMessage());
+ }
+ } catch (IOException ioex) {
+ log.error("Failed to read font metrics file " + metricsFileName, ioex);
+ if (fail) {
+ throw new RuntimeException(ioex.getMessage());
+ }
}
+ isMetricsLoaded = true;
}
}
* @return the real font
*/
public Typeface getRealFont() {
- load();
+ load(false);
return realFont;
}
* @see org.apache.fop.fonts.Typeface#getEncoding()
*/
public String getEncoding() {
- load();
+ load(true);
return realFont.getEncoding();
}
* @see org.apache.fop.fonts.Typeface#mapChar(char)
*/
public char mapChar(char c) {
- load();
+ load(true);
return realFont.mapChar(c);
}
* @see org.apache.fop.fonts.Typeface#hasChar(char)
*/
public boolean hasChar(char c) {
- load();
+ load(true);
return realFont.hasChar(c);
}
* @see org.apache.fop.fonts.FontMetrics#getFontName()
*/
public String getFontName() {
- load();
+ load(true);
return realFont.getFontName();
}
* @see org.apache.fop.fonts.FontMetrics#getAscender(int)
*/
public int getAscender(int size) {
- load();
+ load(true);
return realFont.getAscender(size);
}
* @see org.apache.fop.fonts.FontMetrics#getCapHeight(int)
*/
public int getCapHeight(int size) {
- load();
+ load(true);
return realFont.getCapHeight(size);
}
* @see org.apache.fop.fonts.FontMetrics#getDescender(int)
*/
public int getDescender(int size) {
- load();
+ load(true);
return realFont.getDescender(size);
}
* @see org.apache.fop.fonts.FontMetrics#getXHeight(int)
*/
public int getXHeight(int size) {
- load();
+ load(true);
return realFont.getXHeight(size);
}
* @see org.apache.fop.fonts.FontMetrics#getWidth(int, int)
*/
public int getWidth(int i, int size) {
- load();
+ load(true);
return realFont.getWidth(i, size);
}
* @see org.apache.fop.fonts.FontMetrics#getWidths()
*/
public int[] getWidths() {
- load();
+ load(true);
return realFont.getWidths();
}
* @see org.apache.fop.fonts.FontMetrics#hasKerningInfo()
*/
public boolean hasKerningInfo() {
- load();
+ load(true);
return realFont.hasKerningInfo();
}
* @see org.apache.fop.fonts.FontMetrics#getKerningInfo()
*/
public Map getKerningInfo() {
- load();
+ load(true);
return realFont.getKerningInfo();
}
* @see org.apache.fop.fonts.FontDescriptor#getCapHeight()
*/
public int getCapHeight() {
- load();
+ load(true);
return realFontDescriptor.getCapHeight();
}
* @see org.apache.fop.fonts.FontDescriptor#getDescender()
*/
public int getDescender() {
- load();
+ load(true);
return realFontDescriptor.getDescender();
}
* @see org.apache.fop.fonts.FontDescriptor#getAscender()
*/
public int getAscender() {
- load();
+ load(true);
return realFontDescriptor.getAscender();
}
* @see org.apache.fop.fonts.FontDescriptor#getFlags()
*/
public int getFlags() {
- load();
+ load(true);
return realFontDescriptor.getFlags();
}
* @see org.apache.fop.fonts.FontDescriptor#getFontBBox()
*/
public int[] getFontBBox() {
- load();
+ load(true);
return realFontDescriptor.getFontBBox();
}
* @see org.apache.fop.fonts.FontDescriptor#getItalicAngle()
*/
public int getItalicAngle() {
- load();
+ load(true);
return realFontDescriptor.getItalicAngle();
}
* @see org.apache.fop.fonts.FontDescriptor#getStemV()
*/
public int getStemV() {
- load();
+ load(true);
return realFontDescriptor.getStemV();
}
* @see org.apache.fop.fonts.FontDescriptor#getFontType()
*/
public FontType getFontType() {
- load();
+ load(true);
return realFontDescriptor.getFontType();
}
* @see org.apache.fop.fonts.FontDescriptor#isEmbeddable()
*/
public boolean isEmbeddable() {
- load();
+ load(true);
return realFontDescriptor.isEmbeddable();
}
import org.apache.commons.io.EndianUtils;
import org.apache.commons.io.IOUtils;
import org.apache.fop.fonts.CustomFont;
+import org.apache.fop.fonts.Font;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.fonts.FontType;
import org.apache.fop.fonts.Glyphs;
if (tf instanceof LazyFont) {
tf = ((LazyFont)tf).getRealFont();
}
+ if (tf == null) {
+ //This is to avoid an NPE if a malconfigured font is in the configuration but not
+ //used in the document. If it were used, we wouldn't get this far.
+ String fallbackKey = fontInfo.getInternalFontKey(Font.DEFAULT_FONT);
+ tf = (Typeface)fonts.get(fallbackKey);
+ }
PSResource fontRes = new PSResource("font", tf.getFontName());
fontResources.put(key, fontRes);
boolean embeddedFont = false;
while (iter.hasNext()) {
String key = (String)iter.next();
Typeface fm = (Typeface)fonts.get(key);
- if (null == fm.getEncoding()) {
+ if (fm instanceof LazyFont && ((LazyFont)fm).getRealFont() == null) {
+ continue;
+ } else if (null == fm.getEncoding()) {
//ignore (ZapfDingbats and Symbol run through here
//TODO: ZapfDingbats and Symbol should get getEncoding() fixed!
} else if ("WinAnsiEncoding".equals(fm.getEncoding())) {