KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > ps > PSFontUtils


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: PSFontUtils.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.ps;
21
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.stream.StreamSource JavaDoc;
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 /**
43  * Utility code for font handling in PostScript.
44  */

45 public class PSFontUtils extends org.apache.xmlgraphics.ps.PSFontUtils {
46
47     /**
48      * Generates the PostScript code for the font dictionary.
49      * @param gen PostScript generator to use for output
50      * @param fontInfo available fonts
51      * @return a Map of PSResource instances representing all defined fonts (key: font key)
52      * @throws IOException in case of an I/O problem
53      */

54     public static Map JavaDoc writeFontDict(PSGenerator gen, FontInfo fontInfo)
55                 throws IOException JavaDoc {
56         gen.commentln("%FOPBeginFontDict");
57         gen.writeln("/FOPFonts 100 dict dup begin");
58
59         // write("/gfF1{/Helvetica findfont} bd");
60
// write("/gfF3{/Helvetica-Bold findfont} bd");
61
Map JavaDoc fonts = fontInfo.getFonts();
62         Map JavaDoc fontResources = new java.util.HashMap JavaDoc();
63         Iterator JavaDoc iter = fonts.keySet().iterator();
64         while (iter.hasNext()) {
65             String JavaDoc key = (String JavaDoc)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                 //This is to avoid an NPE if a malconfigured font is in the configuration but not
72
//used in the document. If it were used, we wouldn't get this far.
73
String JavaDoc 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 JavaDoc 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                 //Resource usage shall be handled by renderer
96
//gen.notifyResourceUsage(fontRes, true);
97
}
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         //Rewrite font encodings
108
iter = fonts.keySet().iterator();
109         while (iter.hasNext()) {
110             String JavaDoc key = (String JavaDoc)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                 //ignore (ZapfDingbats and Symbol run through here
116
//TODO: ZapfDingbats and Symbol should get getEncoding() fixed!
117
} 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 JavaDoc getInputStreamOnFont(PSGenerator gen, CustomFont font)
135                 throws IOException JavaDoc {
136         if (font.isEmbeddable()) {
137             Source JavaDoc source = font.getEmbedFileSource();
138             if (source == null && font.getEmbedResourceName() != null) {
139                 source = new StreamSource JavaDoc(PSFontUtils.class
140                         .getResourceAsStream(font.getEmbedResourceName()));
141             }
142             if (source == null) {
143                 return null;
144             }
145             InputStream JavaDoc in = null;
146             if (source instanceof StreamSource JavaDoc) {
147                 in = ((StreamSource JavaDoc) source).getInputStream();
148             }
149             if (in == null && source.getSystemId() != null) {
150                 try {
151                     in = new java.net.URL JavaDoc(source.getSystemId()).openStream();
152                 } catch (MalformedURLException JavaDoc e) {
153                     new FileNotFoundException JavaDoc(
154                             "File not found. URL could not be resolved: "
155                                     + e.getMessage());
156                 }
157             }
158             if (in == null) {
159                 return null;
160             }
161             //Make sure the InputStream is decorated with a BufferedInputStream
162
if (!(in instanceof java.io.BufferedInputStream JavaDoc)) {
163                 in = new java.io.BufferedInputStream JavaDoc(in);
164             }
165             return in;
166         } else {
167             return null;
168         }
169     }
170
171 }
172
Popular Tags