1 51 package org.apache.fop.render.pdf; 52 53 import org.apache.fop.render.pdf.fonts.*; 55 import org.apache.fop.messaging.MessageHandler; 56 import org.apache.fop.layout.FontInfo; 57 import org.apache.fop.layout.FontDescriptor; 58 import org.apache.fop.pdf.PDFDocument; 59 import org.apache.fop.pdf.PDFResources; 60 import org.apache.fop.configuration.Configuration; 61 import org.apache.fop.configuration.FontTriplet; 62 import org.apache.fop.apps.FOPException; 63 64 import java.util.Iterator ; 66 import java.util.Map ; 67 import java.util.List ; 68 import java.net.URL ; 69 70 76 public class FontSetup { 77 78 86 public static void setup(FontInfo fontInfo) throws FOPException { 87 MessageHandler.logln("setting up fonts"); 88 89 fontInfo.addMetrics("F1", new Helvetica()); 90 fontInfo.addMetrics("F2", new HelveticaOblique()); 91 fontInfo.addMetrics("F3", new HelveticaBold()); 92 fontInfo.addMetrics("F4", new HelveticaBoldOblique()); 93 fontInfo.addMetrics("F5", new TimesRoman()); 94 fontInfo.addMetrics("F6", new TimesItalic()); 95 fontInfo.addMetrics("F7", new TimesBold()); 96 fontInfo.addMetrics("F8", new TimesBoldItalic()); 97 fontInfo.addMetrics("F9", new Courier()); 98 fontInfo.addMetrics("F10", new CourierOblique()); 99 fontInfo.addMetrics("F11", new CourierBold()); 100 fontInfo.addMetrics("F12", new CourierBoldOblique()); 101 fontInfo.addMetrics("F13", new Symbol()); 102 fontInfo.addMetrics("F14", new ZapfDingbats()); 103 104 109 110 fontInfo.addFontProperties("F5", "any", "normal", "normal"); 111 fontInfo.addFontProperties("F6", "any", "italic", "normal"); 112 fontInfo.addFontProperties("F6", "any", "oblique", "normal"); 113 fontInfo.addFontProperties("F7", "any", "normal", "bold"); 114 fontInfo.addFontProperties("F8", "any", "italic", "bold"); 115 fontInfo.addFontProperties("F8", "any", "oblique", "bold"); 116 117 fontInfo.addFontProperties("F1", "sans-serif", "normal", "normal"); 118 fontInfo.addFontProperties("F2", "sans-serif", "oblique", "normal"); 119 fontInfo.addFontProperties("F2", "sans-serif", "italic", "normal"); 120 fontInfo.addFontProperties("F3", "sans-serif", "normal", "bold"); 121 fontInfo.addFontProperties("F4", "sans-serif", "oblique", "bold"); 122 fontInfo.addFontProperties("F4", "sans-serif", "italic", "bold"); 123 fontInfo.addFontProperties("F5", "serif", "normal", "normal"); 124 fontInfo.addFontProperties("F6", "serif", "oblique", "normal"); 125 fontInfo.addFontProperties("F6", "serif", "italic", "normal"); 126 fontInfo.addFontProperties("F7", "serif", "normal", "bold"); 127 fontInfo.addFontProperties("F8", "serif", "oblique", "bold"); 128 fontInfo.addFontProperties("F8", "serif", "italic", "bold"); 129 fontInfo.addFontProperties("F9", "monospace", "normal", "normal"); 130 fontInfo.addFontProperties("F10", "monospace", "oblique", "normal"); 131 fontInfo.addFontProperties("F10", "monospace", "italic", "normal"); 132 fontInfo.addFontProperties("F11", "monospace", "normal", "bold"); 133 fontInfo.addFontProperties("F12", "monospace", "oblique", "bold"); 134 fontInfo.addFontProperties("F12", "monospace", "italic", "bold"); 135 136 fontInfo.addFontProperties("F1", "Helvetica", "normal", "normal"); 137 fontInfo.addFontProperties("F2", "Helvetica", "oblique", "normal"); 138 fontInfo.addFontProperties("F2", "Helvetica", "italic", "normal"); 139 fontInfo.addFontProperties("F3", "Helvetica", "normal", "bold"); 140 fontInfo.addFontProperties("F4", "Helvetica", "oblique", "bold"); 141 fontInfo.addFontProperties("F4", "Helvetica", "italic", "bold"); 142 fontInfo.addFontProperties("F5", "Times", "normal", "normal"); 143 fontInfo.addFontProperties("F6", "Times", "oblique", "normal"); 144 fontInfo.addFontProperties("F6", "Times", "italic", "normal"); 145 fontInfo.addFontProperties("F7", "Times", "normal", "bold"); 146 fontInfo.addFontProperties("F8", "Times", "oblique", "bold"); 147 fontInfo.addFontProperties("F8", "Times", "italic", "bold"); 148 fontInfo.addFontProperties("F9", "Courier", "normal", "normal"); 149 fontInfo.addFontProperties("F10", "Courier", "oblique", "normal"); 150 fontInfo.addFontProperties("F10", "Courier", "italic", "normal"); 151 fontInfo.addFontProperties("F11", "Courier", "normal", "bold"); 152 fontInfo.addFontProperties("F12", "Courier", "oblique", "bold"); 153 fontInfo.addFontProperties("F12", "Courier", "italic", "bold"); 154 fontInfo.addFontProperties("F13", "Symbol", "normal", "normal"); 155 fontInfo.addFontProperties("F14", "ZapfDingbats", "normal", "normal"); 156 157 162 163 fontInfo.addFontProperties("F5", "Times-Roman", "normal", "normal"); 164 fontInfo.addFontProperties("F6", "Times-Roman", "oblique", "normal"); 165 fontInfo.addFontProperties("F6", "Times-Roman", "italic", "normal"); 166 fontInfo.addFontProperties("F7", "Times-Roman", "normal", "bold"); 167 fontInfo.addFontProperties("F8", "Times-Roman", "oblique", "bold"); 168 fontInfo.addFontProperties("F8", "Times-Roman", "italic", "bold"); 169 fontInfo.addFontProperties("F5", "Times Roman", "normal", "normal"); 170 fontInfo.addFontProperties("F6", "Times Roman", "oblique", "normal"); 171 fontInfo.addFontProperties("F6", "Times Roman", "italic", "normal"); 172 fontInfo.addFontProperties("F7", "Times Roman", "normal", "bold"); 173 fontInfo.addFontProperties("F8", "Times Roman", "oblique", "bold"); 174 fontInfo.addFontProperties("F8", "Times Roman", "italic", "bold"); 175 fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter", 176 "normal", "normal"); 177 178 179 addConfiguredFonts(fontInfo, 15); 180 } 181 182 186 public static void addConfiguredFonts(FontInfo fontInfo, int num) 187 throws FOPException { 188 189 String internalName = null; 190 FontReader reader = null; 191 192 List fontInfos = Configuration.getFonts(); 193 if (fontInfos == null) 194 return; 195 196 for (int i = 0; i < fontInfos.size(); i++) { 197 org.apache.fop.configuration.FontInfo configFontInfo = 198 (org.apache.fop.configuration.FontInfo)fontInfos.get(i); 199 200 try { 201 URL metricsFile = configFontInfo.getMetricsFile(); 202 if (metricsFile != null) { 203 internalName = "F" + num; 204 num++; 205 211 LazyFont font = new LazyFont(configFontInfo.getEmbedFile(), 212 metricsFile, 213 configFontInfo.getKerning()); 214 fontInfo.addMetrics(internalName, font); 215 216 List triplets = configFontInfo.getFontTriplets(); 217 for (int j = 0; j < triplets.size(); j++) { 218 FontTriplet triplet = (FontTriplet)triplets.get(j); 219 220 fontInfo.addFontProperties(internalName, 221 triplet.getName(), 222 triplet.getStyle(), 223 triplet.getWeight()); 224 } 225 } 226 } catch (Exception ex) { 227 MessageHandler.error("Failed to read a font metrics file: " 228 + ex.getMessage()); 229 } 230 } 231 } 232 233 239 public static void addToResources(PDFDocument doc, FontInfo fontInfo) { 240 Map fonts = fontInfo.getUsedFonts(); 241 Iterator e = fonts.keySet().iterator(); 242 PDFResources resources = doc.getResources(); 243 while (e.hasNext()) { 244 String f = (String )e.next(); 245 Font font = (Font)fonts.get(f); 246 FontDescriptor desc = null; 247 if (font instanceof FontDescriptor) { 248 desc = (FontDescriptor)font; 249 } 250 resources.addFont(doc.makeFont(f, font.fontName(), 251 font.encoding(), font, desc)); 252 } 253 } 254 255 } 256 | Popular Tags |