001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * FontListLoader.java
028: *
029: * Created on 22 maggio 2003, 18.45
030: *
031: */
032:
033: package it.businesslogic.ireport;
034:
035: import it.businesslogic.ireport.gui.MainFrame;
036: import it.businesslogic.ireport.util.I18n;
037: import java.util.*;
038:
039: /**
040: *
041: * @author Administrator
042: */
043: public class FontListLoader {
044:
045: /** Creates a new instance of FontListLoader */
046: public FontListLoader() {
047: }
048:
049: public static java.awt.Font loadFont(String file) {
050: java.awt.Font f = null;
051: try {
052: f = java.awt.Font.createFont(f.TRUETYPE_FONT,
053: new java.io.FileInputStream(file));
054: } catch (IllegalArgumentException ett) {
055: System.out.println(ett.getMessage() + " No TrueType font");
056: } catch (java.awt.FontFormatException ef) {
057: System.out
058: .println(ef.getMessage() + " FontFormatException");
059: } catch (java.io.IOException ioex) {
060: System.out.println(ioex.getMessage() + " IOException");
061: } catch (Exception ex) {
062: System.out.println(ex.getMessage() + " General Exception");
063: }
064:
065: return f;
066: }
067:
068: public static Vector loadTTFFonts() {
069: return loadTTFFonts(null);
070: }
071:
072: public static Vector loadTTFFonts(FontsLoaderMonitor monitor) {
073: if (monitor != null)
074: monitor.fontsLoadingStarted();
075:
076: if (monitor != null) {
077: monitor.fontsLoadingStatusUpdated(I18n.getString(
078: "fontsLoader.loadingFontsInit",
079: "Initializing font loader"));
080: }
081:
082: Vector fonts = new Vector();
083:
084: Vector fontsPaths = MainFrame.getMainInstance().getFontspath();
085:
086: System.out.flush();
087: if (fontsPaths == null || fontsPaths.size() == 0) {
088: String path = System.getProperty("java.class.path");
089: //path += System.getProperty("path.separator") + "C:\\winnt\\fonts";
090: if (path != null && path.length() > 0) {
091: //if (monitor != null)
092: //{
093: // monitor.fontsLoadingStatusUpdated( I18n.getString("fontsLoader.loadingFontsFromClasspath", "Loading fonts from classpath"));
094: //}
095:
096: StringTokenizer st = new StringTokenizer(path, System
097: .getProperty("path.separator"));
098:
099: while (st.hasMoreTokens()) {
100: String path_str = (String) st.nextToken();
101:
102: try {
103: addFont(fonts, path_str, monitor);
104: } catch (Exception ex) {
105:
106: MainFrame.getMainInstance().logOnConsole(
107: I18n.getFormattedString(
108: "fontListLoader.invalidFont",
109: "Invalid font found in: {0}",
110: new Object[] { path_str }));
111: }
112: }
113: }
114: } else {
115: Enumeration e_fonts = fontsPaths.elements();
116: //if (monitor != null)
117: //{
118: // monitor.fontsLoadingStatusUpdated( I18n.getString("fontsLoader.loadingFontsFromFontpath", "Loading fonts from font path"));
119: //}
120:
121: while (e_fonts.hasMoreElements()) {
122: String path_str = (String) e_fonts.nextElement();
123:
124: try {
125: addFont(fonts, path_str, monitor);
126: } catch (Exception ex) {
127:
128: MainFrame.getMainInstance().logOnConsole(
129: I18n.getFormattedString(
130: "fontListLoader.invalidFont",
131: "Invalid font found in: {0}",
132: new Object[] { path_str }));
133: }
134: }
135: }
136:
137: if (monitor != null)
138: monitor.fontsLoadingFinished();
139: return fonts;
140: }
141:
142: public static void addFont(Vector fonts, String path_str,
143: FontsLoaderMonitor monitor) {
144: java.text.MessageFormat formatter = new java.text.MessageFormat(
145: I18n.getString("fontsLoader.loadedFont",
146: "Loaded font {0}"), I18n.getCurrentLocale());
147:
148: java.io.File file = new java.io.File(path_str);
149: if (!file.exists()) {
150:
151: return;
152: }
153: if (file.isDirectory()) {
154: String[] files = file.list(new java.io.FilenameFilter() {
155: public boolean accept(java.io.File dir, String filename) {
156: // modified
157: // return filename.toUpperCase().endsWith(".TTF") ;
158: return filename.toUpperCase().endsWith(".TTF")
159: || filename.toUpperCase().endsWith(".TTC");
160: }
161: });
162:
163: //added begin
164: com.lowagie.text.pdf.FontMapper fontMapper = new com.lowagie.text.pdf.DefaultFontMapper();
165:
166: if (files == null) {
167: MainFrame.getMainInstance().logOnConsole(
168: I18n.getFormattedString(
169: "fontListLoader.unableToListFiles",
170: "Unable to list files in: {0}",
171: new Object[] { "" + file }));
172: return;
173: }
174: for (int i = 0; i < files.length; ++i) {
175: if (files[i].toUpperCase().endsWith(".TTC")) {
176: try {
177: String[] names = com.lowagie.text.pdf.BaseFont
178: .enumerateTTCNames(file.getPath()
179: + file.separator + files[i]);
180: for (int a = 0; a < names.length; a++) {
181:
182: java.awt.Font f = fontMapper
183: .pdfToAwt(
184: com.lowagie.text.FontFactory
185: .getFont(
186: file
187: .getPath()
188: + file.separator
189: + files[i])
190: .getBaseFont(), 10);
191: if (f != null) {
192: fonts
193: .addElement(new IRFont(f,
194: files[i]));
195: } else {
196: MainFrame
197: .getMainInstance()
198: .logOnConsole(
199: I18n
200: .getFormattedString(
201: "fontListLoader.failedLoadingFont",
202: "Failed to load font {0}",
203: new Object[] { ""
204: + file
205: .getPath()
206: + file.separator
207: + files[i] }));
208: }
209:
210: if (monitor != null) {
211: monitor
212: .fontsLoadingStatusUpdated(formatter
213: .format(new Object[] { file
214: .getPath()
215: + file.separator
216: + files[i] }));
217: }
218: }
219: } catch (com.lowagie.text.DocumentException de) {
220: System.out.println(de);
221: } catch (java.io.IOException ioe) {
222: System.out.println(ioe);
223: }
224: } else {
225: //added end
226:
227: java.awt.Font f = loadFont(file.getPath()
228: + file.separator + files[i]);
229: if (f != null) {
230: fonts.addElement(new IRFont(f, files[i]));
231: //System.out.println(""+ f.getFamily() + " " + f.getFontName() +" ("+file.getPath() + file.separator +files[i]+")");
232: } else {
233: MainFrame
234: .getMainInstance()
235: .logOnConsole(
236: I18n
237: .getFormattedString(
238: "fontListLoader.failedLoadingFont",
239: "Failed to load font {0}",
240: new Object[] { ""
241: + file
242: .getPath()
243: + file.separator
244: + files[i] }));
245: }
246:
247: if (monitor != null) {
248: monitor.fontsLoadingStatusUpdated(formatter
249: .format(new Object[] { file.getPath()
250: + file.separator + files[i] }));
251: }
252: }
253:
254: }
255: } else if (path_str.toUpperCase().endsWith(".TTF")) {
256: // Try to load this file...
257: System.out.println("m" + path_str);
258: } else if (path_str.toUpperCase().endsWith(".TTC")) {
259: // Try to load this file...
260: System.out.println("TTC: " + path_str);
261: }
262: }
263:
264: }
|