1 51 package org.apache.fop.render.awt; 52 53 import org.apache.fop.messaging.MessageHandler; 55 import org.apache.fop.layout.FontInfo; 56 import org.apache.fop.configuration.Configuration; 57 import org.apache.fop.configuration.FontTriplet; 58 import org.apache.fop.apps.FOPException; 59 60 import java.awt.Graphics2D ; 62 import java.util.List ; 63 import java.awt.RenderingHints ; 64 import java.net.URL ; 65 66 72 public class FontSetup { 73 74 75 private static int normal = java.awt.Font.PLAIN; 76 77 private static int bold = java.awt.Font.BOLD; 78 79 private static int italic = java.awt.Font.ITALIC; 80 81 private static int bolditalic = java.awt.Font.BOLD + java.awt.Font.ITALIC; 82 83 93 public static void setup(FontInfo fontInfo, Graphics2D graphics) 94 throws FOPException { 95 96 FontMetricsMapper metric; 97 98 MessageHandler.logln("setting up fonts"); 99 100 102 graphics.setRenderingHint (RenderingHints.KEY_FRACTIONALMETRICS, 103 RenderingHints.VALUE_FRACTIONALMETRICS_ON); 104 105 106 112 113 metric = new FontMetricsMapper("SansSerif", normal, graphics); 114 fontInfo.addMetrics("F1", metric); 116 metric = new FontMetricsMapper("SansSerif", italic, graphics); 117 fontInfo.addMetrics("F2", metric); 119 metric = new FontMetricsMapper("SansSerif", bold, graphics); 120 fontInfo.addMetrics("F3", metric); 122 metric = new FontMetricsMapper("SansSerif", bolditalic, graphics); 123 fontInfo.addMetrics("F4", metric); 125 126 127 metric = new FontMetricsMapper("Serif", normal, graphics); 128 fontInfo.addMetrics("F5", metric); 130 metric = new FontMetricsMapper("Serif", italic, graphics); 131 fontInfo.addMetrics("F6", metric); 133 metric = new FontMetricsMapper("Serif", bold, graphics); 134 fontInfo.addMetrics("F7", metric); 136 metric = new FontMetricsMapper("Serif", bolditalic, graphics); 137 fontInfo.addMetrics("F8", metric); 139 140 metric = new FontMetricsMapper("MonoSpaced", normal, graphics); 141 fontInfo.addMetrics("F9", metric); 143 metric = new FontMetricsMapper("MonoSpaced", italic, graphics); 144 fontInfo.addMetrics("F10", metric); 146 metric = new FontMetricsMapper("MonoSpaced", bold, graphics); 147 fontInfo.addMetrics("F11", metric); 149 metric = new FontMetricsMapper("MonoSpaced", bolditalic, graphics); 150 fontInfo.addMetrics("F12", metric); 152 153 metric = new FontMetricsMapper("Symbol", bolditalic, graphics); 154 fontInfo.addMetrics("F13", metric); 156 fontInfo.addMetrics("F14", metric); 157 158 163 164 fontInfo.addFontProperties("F5", "any", "normal", "normal"); 165 fontInfo.addFontProperties("F6", "any", "italic", "normal"); 166 fontInfo.addFontProperties("F6", "any", "oblique", "normal"); 167 fontInfo.addFontProperties("F7", "any", "normal", "bold"); 168 fontInfo.addFontProperties("F8", "any", "italic", "bold"); 169 fontInfo.addFontProperties("F8", "any", "oblique", "bold"); 170 171 fontInfo.addFontProperties("F1", "sans-serif", "normal", "normal"); 172 fontInfo.addFontProperties("F2", "sans-serif", "oblique", "normal"); 173 fontInfo.addFontProperties("F2", "sans-serif", "italic", "normal"); 174 fontInfo.addFontProperties("F3", "sans-serif", "normal", "bold"); 175 fontInfo.addFontProperties("F4", "sans-serif", "oblique", "bold"); 176 fontInfo.addFontProperties("F4", "sans-serif", "italic", "bold"); 177 fontInfo.addFontProperties("F5", "serif", "normal", "normal"); 178 fontInfo.addFontProperties("F6", "serif", "oblique", "normal"); 179 fontInfo.addFontProperties("F6", "serif", "italic", "normal"); 180 fontInfo.addFontProperties("F7", "serif", "normal", "bold"); 181 fontInfo.addFontProperties("F8", "serif", "oblique", "bold"); 182 fontInfo.addFontProperties("F8", "serif", "italic", "bold"); 183 fontInfo.addFontProperties("F9", "monospace", "normal", "normal"); 184 fontInfo.addFontProperties("F10", "monospace", "oblique", "normal"); 185 fontInfo.addFontProperties("F10", "monospace", "italic", "normal"); 186 fontInfo.addFontProperties("F11", "monospace", "normal", "bold"); 187 fontInfo.addFontProperties("F12", "monospace", "oblique", "bold"); 188 fontInfo.addFontProperties("F12", "monospace", "italic", "bold"); 189 190 fontInfo.addFontProperties("F1", "Helvetica", "normal", "normal"); 191 fontInfo.addFontProperties("F2", "Helvetica", "oblique", "normal"); 192 fontInfo.addFontProperties("F2", "Helvetica", "italic", "normal"); 193 fontInfo.addFontProperties("F3", "Helvetica", "normal", "bold"); 194 fontInfo.addFontProperties("F4", "Helvetica", "oblique", "bold"); 195 fontInfo.addFontProperties("F4", "Helvetica", "italic", "bold"); 196 fontInfo.addFontProperties("F5", "Times", "normal", "normal"); 197 fontInfo.addFontProperties("F6", "Times", "oblique", "normal"); 198 fontInfo.addFontProperties("F6", "Times", "italic", "normal"); 199 fontInfo.addFontProperties("F7", "Times", "normal", "bold"); 200 fontInfo.addFontProperties("F8", "Times", "oblique", "bold"); 201 fontInfo.addFontProperties("F8", "Times", "italic", "bold"); 202 fontInfo.addFontProperties("F9", "Courier", "normal", "normal"); 203 fontInfo.addFontProperties("F10", "Courier", "oblique", "normal"); 204 fontInfo.addFontProperties("F10", "Courier", "italic", "normal"); 205 fontInfo.addFontProperties("F11", "Courier", "normal", "bold"); 206 fontInfo.addFontProperties("F12", "Courier", "oblique", "bold"); 207 fontInfo.addFontProperties("F12", "Courier", "italic", "bold"); 208 fontInfo.addFontProperties("F13", "Symbol", "normal", "normal"); 209 fontInfo.addFontProperties("F14", "ZapfDingbats", "normal", "normal"); 210 211 216 217 fontInfo.addFontProperties("F5", "Times-Roman", "normal", "normal"); 218 fontInfo.addFontProperties("F6", "Times-Roman", "oblique", "normal"); 219 fontInfo.addFontProperties("F6", "Times-Roman", "italic", "normal"); 220 fontInfo.addFontProperties("F7", "Times-Roman", "normal", "bold"); 221 fontInfo.addFontProperties("F8", "Times-Roman", "oblique", "bold"); 222 fontInfo.addFontProperties("F8", "Times-Roman", "italic", "bold"); 223 fontInfo.addFontProperties("F5", "Times Roman", "normal", "normal"); 224 fontInfo.addFontProperties("F6", "Times Roman", "oblique", "normal"); 225 fontInfo.addFontProperties("F6", "Times Roman", "italic", "normal"); 226 fontInfo.addFontProperties("F7", "Times Roman", "normal", "bold"); 227 fontInfo.addFontProperties("F8", "Times Roman", "oblique", "bold"); 228 fontInfo.addFontProperties("F8", "Times Roman", "italic", "bold"); 229 fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter", 230 "normal", "normal"); 231 232 233 addConfiguredFonts(fontInfo, 15, graphics); 234 } 235 236 244 public static void addConfiguredFonts( 245 FontInfo fontInfo, int num, Graphics2D graphics) 246 throws FOPException { 247 FontMetricsMapper metric; 248 String internalName = null; 249 250 List fontInfos = Configuration.getFonts(); 251 if (fontInfos == null) 252 return; 253 254 for (int i = 0; i < fontInfos.size(); i++) { 255 org.apache.fop.configuration.FontInfo configFontInfo = 256 (org.apache.fop.configuration.FontInfo)fontInfos.get(i); 257 258 try { 259 URL metricsFile = configFontInfo.getMetricsFile(); 260 if (metricsFile != null) { 261 internalName = "F" + num; 262 num++; 263 264 List triplets = configFontInfo.getFontTriplets(); 265 for (int j = 0; j < triplets.size(); j++) { 266 FontTriplet triplet = (FontTriplet)triplets.get(j); 267 boolean embed = configFontInfo.getEmbedFile() != null; 268 String family = embed ? triplet.getName() : "Dialog"; 271 metric = new FontMetricsMapper(family, 272 getFontMetrics(triplet), 273 graphics); 274 if (embed) 275 metric.setEmbedFont(configFontInfo.getEmbedFile()); 276 fontInfo.addMetrics(internalName, metric); 277 fontInfo.addFontProperties(internalName, 278 triplet.getName(), 279 triplet.getStyle(), 280 triplet.getWeight()); 281 } 282 } 283 } catch (Exception ex) { 284 MessageHandler.error("Failed to read font metrics file: " 285 + ex.getMessage()); 286 } 287 } 288 } 289 290 296 private static int getFontMetrics(FontTriplet triplet) { 297 boolean isBold = ("bold".equalsIgnoreCase(triplet.getWeight())); 298 boolean isItalic = ("italic".equalsIgnoreCase(triplet.getStyle())); 299 if (isBold && isItalic) { 300 return bolditalic; 301 } else if (isBold) { 302 return bold; 303 } else if (isItalic) { 304 return italic; 305 } 306 return normal; 307 } 308 } 309 310 311 312 313 314 315 316 317 318 319 | Popular Tags |