KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > FontListLoader


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * FontListLoader.java
28  *
29  * Created on 22 maggio 2003, 18.45
30  *
31  */

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 /**
38  *
39  * @author Administrator
40  */

41 public class FontListLoader {
42
43     /** Creates a new instance of FontListLoader */
44     public FontListLoader() {
45     }
46
47     public static java.awt.Font JavaDoc loadFont(String JavaDoc file)
48     {
49         java.awt.Font JavaDoc f = null;
50         try {
51             f = java.awt.Font.createFont(f.TRUETYPE_FONT, new java.io.FileInputStream JavaDoc(file));
52         }
53         catch (IllegalArgumentException JavaDoc ett)
54         {
55             System.out.println(ett.getMessage() +" No TrueType font");
56         }
57         catch (java.awt.FontFormatException JavaDoc ef)
58         {
59             System.out.println(ef.getMessage() +" FontFormatException");
60         }
61          catch (java.io.IOException JavaDoc ioex)
62         {
63             System.out.println(ioex.getMessage() +" IOException");
64         }
65         catch (Exception JavaDoc 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 JavaDoc path = System.getProperty("java.class.path");
96             //path += System.getProperty("path.separator") + "C:\\winnt\\fonts";
97
if (path != null && path.length()>0)
98             {
99                //if (monitor != null)
100
//{
101
// monitor.fontsLoadingStatusUpdated( I18n.getString("fontsLoader.loadingFontsFromClasspath", "Loading fonts from classpath"));
102
//}
103

104                StringTokenizer st = new StringTokenizer(path, System.getProperty("path.separator") );
105
106                 while (st.hasMoreTokens())
107                 {
108                     String JavaDoc path_str = (String JavaDoc)st.nextToken();
109                             
110                     try { addFont(fonts, path_str, monitor); } catch (Exception JavaDoc ex) {
111                  
112                         MainFrame.getMainInstance().logOnConsole(
113                                 I18n.getFormattedString("fontListLoader.invalidFont", "Invalid font found in: {0}", new Object JavaDoc[]{path_str}));
114                     }
115                 }
116             }
117         }
118         else
119         {
120             Enumeration e_fonts = fontsPaths.elements();
121             //if (monitor != null)
122
//{
123
// monitor.fontsLoadingStatusUpdated( I18n.getString("fontsLoader.loadingFontsFromFontpath", "Loading fonts from font path"));
124
//}
125

126             while (e_fonts.hasMoreElements())
127                 {
128                     String JavaDoc path_str = (String JavaDoc)e_fonts.nextElement();
129
130                     try { addFont(fonts, path_str, monitor); } catch (Exception JavaDoc ex) {
131                  
132                         MainFrame.getMainInstance().logOnConsole(
133                                 I18n.getFormattedString("fontListLoader.invalidFont", "Invalid font found in: {0}", new Object JavaDoc[]{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 JavaDoc path_str, FontsLoaderMonitor monitor)
144     {
145                 java.text.MessageFormat JavaDoc formatter = new java.text.MessageFormat JavaDoc(
146                         I18n.getString("fontsLoader.loadedFont", "Loaded font {0}"),
147                         I18n.getCurrentLocale());
148         
149                 java.io.File JavaDoc file = new java.io.File JavaDoc(path_str);
150                 if (!file.exists()) {
151
152                     return;
153                 }
154                 if (file.isDirectory())
155                 {
156                     String JavaDoc[] files = file.list( new java.io.FilenameFilter JavaDoc(){
157                         public boolean accept(java.io.File JavaDoc dir, String JavaDoc filename)
158                         {
159 // modified
160
// return filename.toUpperCase().endsWith(".TTF") ;
161
return filename.toUpperCase().endsWith(".TTF") || filename.toUpperCase().endsWith(".TTC");
162                         }
163                     });
164
165                     //added begin
166
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 JavaDoc[]{""+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 JavaDoc[] 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 JavaDoc 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 JavaDoc[]{""+file.getPath() + file.separator +files[i]}));
191                                                 }
192                                     
193                                                         
194                                                 if (monitor != null)
195                                                 {
196                                                     monitor.fontsLoadingStatusUpdated(
197                                                             formatter.format( new Object JavaDoc[]{ 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 JavaDoc ioe) {
203                         System.out.println(ioe);
204                                 }
205             } else {
206             //added end
207

208                             
209                             java.awt.Font JavaDoc f = loadFont( file.getPath() + file.separator + files[i]);
210                             if (f != null)
211                             {
212                                 fonts.addElement(new IRFont(f, files[i]));
213                                 //System.out.println(""+ f.getFamily() + " " + f.getFontName() +" ("+file.getPath() + file.separator +files[i]+")");
214
}
215                             else
216                             {
217                                 MainFrame.getMainInstance().logOnConsole(
218                                                         I18n.getFormattedString("fontListLoader.failedLoadingFont", "Failed to load font {0}", new Object JavaDoc[]{""+file.getPath() + file.separator +files[i]}));
219                             }
220                             
221                                 
222                             if (monitor != null)
223                             {
224                                 monitor.fontsLoadingStatusUpdated(
225                                         formatter.format( new Object JavaDoc[]{ file.getPath() + file.separator + files[i] }));
226                             }
227                         }
228                         
229                         
230                     }
231                 } else if (path_str.toUpperCase().endsWith(".TTF"))
232                 {
233                     // Try to load this file...
234
System.out.println("m"+ path_str);
235                 }
236                 else if (path_str.toUpperCase().endsWith(".TTC"))
237                 {
238                     // Try to load this file...
239
System.out.println("TTC: "+ path_str);
240                 }
241     }
242
243 }
244
Popular Tags