1 17 18 19 20 package org.apache.fop.render.ps; 21 22 import java.io.FileNotFoundException ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.net.MalformedURLException ; 26 import java.util.Iterator ; 27 import java.util.Map ; 28 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.stream.StreamSource ; 31 32 import org.apache.fop.fonts.CustomFont; 33 import org.apache.fop.fonts.Font; 34 import org.apache.fop.fonts.FontInfo; 35 import org.apache.fop.fonts.FontType; 36 import org.apache.fop.fonts.LazyFont; 37 import org.apache.fop.fonts.Typeface; 38 import org.apache.xmlgraphics.ps.DSCConstants; 39 import org.apache.xmlgraphics.ps.PSGenerator; 40 import org.apache.xmlgraphics.ps.PSResource; 41 42 45 public class PSFontUtils extends org.apache.xmlgraphics.ps.PSFontUtils { 46 47 54 public static Map writeFontDict(PSGenerator gen, FontInfo fontInfo) 55 throws IOException { 56 gen.commentln("%FOPBeginFontDict"); 57 gen.writeln("/FOPFonts 100 dict dup begin"); 58 59 Map fonts = fontInfo.getFonts(); 62 Map fontResources = new java.util.HashMap (); 63 Iterator iter = fonts.keySet().iterator(); 64 while (iter.hasNext()) { 65 String key = (String )iter.next(); 66 Typeface tf = (Typeface)fonts.get(key); 67 if (tf instanceof LazyFont) { 68 tf = ((LazyFont)tf).getRealFont(); 69 } 70 if (tf == null) { 71 String fallbackKey = fontInfo.getInternalFontKey(Font.DEFAULT_FONT); 74 tf = (Typeface)fonts.get(fallbackKey); 75 } 76 PSResource fontRes = new PSResource("font", tf.getFontName()); 77 fontResources.put(key, fontRes); 78 boolean embeddedFont = false; 79 if (FontType.TYPE1 == tf.getFontType()) { 80 if (tf instanceof CustomFont) { 81 CustomFont cf = (CustomFont)tf; 82 InputStream in = getInputStreamOnFont(gen, cf); 83 if (in != null) { 84 gen.writeDSCComment(DSCConstants.BEGIN_RESOURCE, 85 fontRes); 86 embedType1Font(gen, in); 87 gen.writeDSCComment(DSCConstants.END_RESOURCE); 88 gen.notifyResourceUsage(fontRes, false); 89 embeddedFont = true; 90 } 91 } 92 } 93 if (!embeddedFont) { 94 gen.writeDSCComment(DSCConstants.INCLUDE_RESOURCE, fontRes); 95 } 98 gen.commentln("%FOPBeginFontKey: " + key); 99 gen.writeln("/" + key + " /" + tf.getFontName() + " def"); 100 gen.commentln("%FOPEndFontKey"); 101 } 102 gen.writeln("end def"); 103 gen.commentln("%FOPEndFontDict"); 104 gen.commentln("%FOPBeginFontReencode"); 105 defineWinAnsiEncoding(gen); 106 107 iter = fonts.keySet().iterator(); 109 while (iter.hasNext()) { 110 String key = (String )iter.next(); 111 Typeface fm = (Typeface)fonts.get(key); 112 if (fm instanceof LazyFont && ((LazyFont)fm).getRealFont() == null) { 113 continue; 114 } else if (null == fm.getEncoding()) { 115 } else if ("WinAnsiEncoding".equals(fm.getEncoding())) { 118 gen.writeln("/" + fm.getFontName() + " findfont"); 119 gen.writeln("dup length dict begin"); 120 gen.writeln(" {1 index /FID ne {def} {pop pop} ifelse} forall"); 121 gen.writeln(" /Encoding " + fm.getEncoding() + " def"); 122 gen.writeln(" currentdict"); 123 gen.writeln("end"); 124 gen.writeln("/" + fm.getFontName() + " exch definefont pop"); 125 } else { 126 gen.commentln("%WARNING: Only WinAnsiEncoding is supported. Font '" 127 + fm.getFontName() + "' asks for: " + fm.getEncoding()); 128 } 129 } 130 gen.commentln("%FOPEndFontReencode"); 131 return fontResources; 132 } 133 134 private static InputStream getInputStreamOnFont(PSGenerator gen, CustomFont font) 135 throws IOException { 136 if (font.isEmbeddable()) { 137 Source source = font.getEmbedFileSource(); 138 if (source == null && font.getEmbedResourceName() != null) { 139 source = new StreamSource (PSFontUtils.class 140 .getResourceAsStream(font.getEmbedResourceName())); 141 } 142 if (source == null) { 143 return null; 144 } 145 InputStream in = null; 146 if (source instanceof StreamSource ) { 147 in = ((StreamSource ) source).getInputStream(); 148 } 149 if (in == null && source.getSystemId() != null) { 150 try { 151 in = new java.net.URL (source.getSystemId()).openStream(); 152 } catch (MalformedURLException e) { 153 new FileNotFoundException ( 154 "File not found. URL could not be resolved: " 155 + e.getMessage()); 156 } 157 } 158 if (in == null) { 159 return null; 160 } 161 if (!(in instanceof java.io.BufferedInputStream )) { 163 in = new java.io.BufferedInputStream (in); 164 } 165 return in; 166 } else { 167 return null; 168 } 169 } 170 171 } 172 | Popular Tags |