1 32 33 package it.businesslogic.ireport; 34 import it.businesslogic.ireport.gui.MainFrame; 35 import it.businesslogic.ireport.util.I18n; 36 import java.util.*; 37 41 public class FontListLoader { 42 43 44 public FontListLoader() { 45 } 46 47 public static java.awt.Font loadFont(String file) 48 { 49 java.awt.Font f = null; 50 try { 51 f = java.awt.Font.createFont(f.TRUETYPE_FONT, new java.io.FileInputStream (file)); 52 } 53 catch (IllegalArgumentException ett) 54 { 55 System.out.println(ett.getMessage() +" No TrueType font"); 56 } 57 catch (java.awt.FontFormatException ef) 58 { 59 System.out.println(ef.getMessage() +" FontFormatException"); 60 } 61 catch (java.io.IOException ioex) 62 { 63 System.out.println(ioex.getMessage() +" IOException"); 64 } 65 catch (Exception ex) 66 { 67 System.out.println(ex.getMessage() +" General Exception"); 68 } 69 70 return f; 71 } 72 73 public static Vector loadTTFFonts() 74 { 75 return loadTTFFonts(null); 76 } 77 78 79 public static Vector loadTTFFonts(FontsLoaderMonitor monitor) 80 { 81 if (monitor != null) monitor.fontsLoadingStarted(); 82 83 if (monitor != null) 84 { 85 monitor.fontsLoadingStatusUpdated( I18n.getString("fontsLoader.loadingFontsInit", "Initializing font loader")); 86 } 87 88 Vector fonts = new Vector(); 89 90 Vector fontsPaths = MainFrame.getMainInstance().getFontspath(); 91 92 System.out.flush(); 93 if (fontsPaths == null || fontsPaths.size() == 0) 94 { 95 String path = System.getProperty("java.class.path"); 96 if (path != null && path.length()>0) 98 { 99 104 StringTokenizer st = new StringTokenizer(path, System.getProperty("path.separator") ); 105 106 while (st.hasMoreTokens()) 107 { 108 String path_str = (String )st.nextToken(); 109 110 try { addFont(fonts, path_str, monitor); } catch (Exception ex) { 111 112 MainFrame.getMainInstance().logOnConsole( 113 I18n.getFormattedString("fontListLoader.invalidFont", "Invalid font found in: {0}", new Object []{path_str})); 114 } 115 } 116 } 117 } 118 else 119 { 120 Enumeration e_fonts = fontsPaths.elements(); 121 126 while (e_fonts.hasMoreElements()) 127 { 128 String path_str = (String )e_fonts.nextElement(); 129 130 try { addFont(fonts, path_str, monitor); } catch (Exception ex) { 131 132 MainFrame.getMainInstance().logOnConsole( 133 I18n.getFormattedString("fontListLoader.invalidFont", "Invalid font found in: {0}", new Object []{path_str})); 134 } 135 } 136 } 137 138 if (monitor != null) monitor.fontsLoadingFinished(); 139 return fonts; 140 } 141 142 143 public static void addFont(Vector fonts, String path_str, FontsLoaderMonitor monitor) 144 { 145 java.text.MessageFormat formatter = new java.text.MessageFormat ( 146 I18n.getString("fontsLoader.loadedFont", "Loaded font {0}"), 147 I18n.getCurrentLocale()); 148 149 java.io.File file = new java.io.File (path_str); 150 if (!file.exists()) { 151 152 return; 153 } 154 if (file.isDirectory()) 155 { 156 String [] files = file.list( new java.io.FilenameFilter (){ 157 public boolean accept(java.io.File dir, String filename) 158 { 159 return filename.toUpperCase().endsWith(".TTF") || filename.toUpperCase().endsWith(".TTC"); 162 } 163 }); 164 165 com.lowagie.text.pdf.FontMapper fontMapper = new com.lowagie.text.pdf.DefaultFontMapper(); 167 168 if (files == null) 169 { 170 MainFrame.getMainInstance().logOnConsole( 171 I18n.getFormattedString("fontListLoader.unableToListFiles", "Unable to list files in: {0}", new Object []{""+file})); 172 return; 173 } 174 for (int i=0; i<files.length; ++i) 175 { 176 if (files[i].toUpperCase().endsWith(".TTC")) { 177 try { 178 String [] names = com.lowagie.text.pdf.BaseFont.enumerateTTCNames(file.getPath() + file.separator + files[i]); 179 for (int a = 0; a < names.length; a++) { 180 181 182 183 java.awt.Font f = fontMapper.pdfToAwt( com.lowagie.text.FontFactory.getFont( file.getPath() + file.separator + files[i]).getBaseFont(), 10 ); 184 if (f != null) { 185 fonts.addElement(new IRFont(f, files[i])); 186 } 187 else 188 { 189 MainFrame.getMainInstance().logOnConsole( 190 I18n.getFormattedString("fontListLoader.failedLoadingFont", "Failed to load font {0}", new Object []{""+file.getPath() + file.separator +files[i]})); 191 } 192 193 194 if (monitor != null) 195 { 196 monitor.fontsLoadingStatusUpdated( 197 formatter.format( new Object []{ file.getPath() + file.separator + files[i] })); 198 } 199 } 200 } catch(com.lowagie.text.DocumentException de) { 201 System.out.println(de); 202 } catch(java.io.IOException ioe) { 203 System.out.println(ioe); 204 } 205 } else { 206 208 209 java.awt.Font f = loadFont( file.getPath() + file.separator + files[i]); 210 if (f != null) 211 { 212 fonts.addElement(new IRFont(f, files[i])); 213 } 215 else 216 { 217 MainFrame.getMainInstance().logOnConsole( 218 I18n.getFormattedString("fontListLoader.failedLoadingFont", "Failed to load font {0}", new Object []{""+file.getPath() + file.separator +files[i]})); 219 } 220 221 222 if (monitor != null) 223 { 224 monitor.fontsLoadingStatusUpdated( 225 formatter.format( new Object []{ file.getPath() + file.separator + files[i] })); 226 } 227 } 228 229 230 } 231 } else if (path_str.toUpperCase().endsWith(".TTF")) 232 { 233 System.out.println("m"+ path_str); 235 } 236 else if (path_str.toUpperCase().endsWith(".TTC")) 237 { 238 System.out.println("TTC: "+ path_str); 240 } 241 } 242 243 } 244 | Popular Tags |