Source Code Cross Referenced for DefaultFontMapper.java in  » PDF » pdf-itext » com » lowagie » text » pdf » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » PDF » pdf itext » com.lowagie.text.pdf 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004 by Paulo Soares.
003:         *
004:         * The contents of this file are subject to the Mozilla Public License Version 1.1
005:         * (the "License"); you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007:         *
008:         * Software distributed under the License is distributed on an "AS IS" basis,
009:         * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010:         * for the specific language governing rights and limitations under the License.
011:         *
012:         * The Original Code is 'iText, a free JAVA-PDF library'.
013:         *
014:         * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015:         * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
016:         * All Rights Reserved.
017:         * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018:         * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
019:         *
020:         * Contributor(s): all the names of the contributors are added in the source code
021:         * where applicable.
022:         *
023:         * Alternatively, the contents of this file may be used under the terms of the
024:         * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025:         * provisions of LGPL are applicable instead of those above.  If you wish to
026:         * allow use of your version of this file only under the terms of the LGPL
027:         * License and not to allow others to use your version of this file under
028:         * the MPL, indicate your decision by deleting the provisions above and
029:         * replace them with the notice and other provisions required by the LGPL.
030:         * If you do not delete the provisions above, a recipient may use your version
031:         * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032:         *
033:         * This library is free software; you can redistribute it and/or modify it
034:         * under the terms of the MPL as stated above or under the terms of the GNU
035:         * Library General Public License as published by the Free Software Foundation;
036:         * either version 2 of the License, or any later version.
037:         *
038:         * This library is distributed in the hope that it will be useful, but WITHOUT
039:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040:         * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041:         * details.
042:         *
043:         * If you didn't download this code from the following link, you should check if
044:         * you aren't using an obsolete version:
045:         * http://www.lowagie.com/iText/
046:         */
047:        package com.lowagie.text.pdf;
048:
049:        import java.awt.Font;
050:        import java.io.File;
051:        import java.util.HashMap;
052:
053:        import com.lowagie.text.ExceptionConverter;
054:
055:        /** Default class to map awt fonts to BaseFont.
056:         * @author Paulo Soares (psoares@consiste.pt)
057:         */
058:
059:        public class DefaultFontMapper implements  FontMapper {
060:
061:            /** A representation of BaseFont parameters.
062:             */
063:            public static class BaseFontParameters {
064:                /** The font name.
065:                 */
066:                public String fontName;
067:                /** The encoding for that font.
068:                 */
069:                public String encoding;
070:                /** The embedding for that font.
071:                 */
072:                public boolean embedded;
073:                /** Whether the font is cached of not.
074:                 */
075:                public boolean cached;
076:                /** The font bytes for ttf and afm.
077:                 */
078:                public byte ttfAfm[];
079:                /** The font bytes for pfb.
080:                 */
081:                public byte pfb[];
082:
083:                /** Constructs default BaseFont parameters.
084:                 * @param fontName the font name or location
085:                 */
086:                public BaseFontParameters(String fontName) {
087:                    this .fontName = fontName;
088:                    encoding = BaseFont.CP1252;
089:                    embedded = BaseFont.EMBEDDED;
090:                    cached = BaseFont.CACHED;
091:                }
092:            }
093:
094:            /** Maps aliases to names.
095:             */
096:            private HashMap aliases = new HashMap();
097:            /** Maps names to BaseFont parameters.
098:             */
099:            private HashMap mapper = new HashMap();
100:
101:            /**
102:             * Returns a BaseFont which can be used to represent the given AWT Font
103:             *
104:             * @param	font		the font to be converted
105:             * @return	a BaseFont which has similar properties to the provided Font
106:             */
107:
108:            public BaseFont awtToPdf(Font font) {
109:                try {
110:                    BaseFontParameters p = getBaseFontParameters(font
111:                            .getFontName());
112:                    if (p != null)
113:                        return BaseFont.createFont(p.fontName, p.encoding,
114:                                p.embedded, p.cached, p.ttfAfm, p.pfb);
115:                    String fontKey = null;
116:                    String logicalName = font.getName();
117:
118:                    if (logicalName.equalsIgnoreCase("DialogInput")
119:                            || logicalName.equalsIgnoreCase("Monospaced")
120:                            || logicalName.equalsIgnoreCase("Courier")) {
121:
122:                        if (font.isItalic()) {
123:                            if (font.isBold()) {
124:                                fontKey = BaseFont.COURIER_BOLDOBLIQUE;
125:
126:                            } else {
127:                                fontKey = BaseFont.COURIER_OBLIQUE;
128:                            }
129:
130:                        } else {
131:                            if (font.isBold()) {
132:                                fontKey = BaseFont.COURIER_BOLD;
133:
134:                            } else {
135:                                fontKey = BaseFont.COURIER;
136:                            }
137:                        }
138:
139:                    } else if (logicalName.equalsIgnoreCase("Serif")
140:                            || logicalName.equalsIgnoreCase("TimesRoman")) {
141:
142:                        if (font.isItalic()) {
143:                            if (font.isBold()) {
144:                                fontKey = BaseFont.TIMES_BOLDITALIC;
145:
146:                            } else {
147:                                fontKey = BaseFont.TIMES_ITALIC;
148:                            }
149:
150:                        } else {
151:                            if (font.isBold()) {
152:                                fontKey = BaseFont.TIMES_BOLD;
153:
154:                            } else {
155:                                fontKey = BaseFont.TIMES_ROMAN;
156:                            }
157:                        }
158:
159:                    } else { // default, this catches Dialog and SansSerif
160:
161:                        if (font.isItalic()) {
162:                            if (font.isBold()) {
163:                                fontKey = BaseFont.HELVETICA_BOLDOBLIQUE;
164:
165:                            } else {
166:                                fontKey = BaseFont.HELVETICA_OBLIQUE;
167:                            }
168:
169:                        } else {
170:                            if (font.isBold()) {
171:                                fontKey = BaseFont.HELVETICA_BOLD;
172:                            } else {
173:                                fontKey = BaseFont.HELVETICA;
174:                            }
175:                        }
176:                    }
177:                    return BaseFont.createFont(fontKey, BaseFont.CP1252, false);
178:                } catch (Exception e) {
179:                    throw new ExceptionConverter(e);
180:                }
181:            }
182:
183:            /**
184:             * Returns an AWT Font which can be used to represent the given BaseFont
185:             *
186:             * @param	font		the font to be converted
187:             * @param	size		the desired point size of the resulting font
188:             * @return	a Font which has similar properties to the provided BaseFont
189:             */
190:
191:            public Font pdfToAwt(BaseFont font, int size) {
192:                String names[][] = font.getFullFontName();
193:                if (names.length == 1)
194:                    return new Font(names[0][3], 0, size);
195:                String name10 = null;
196:                String name3x = null;
197:                for (int k = 0; k < names.length; ++k) {
198:                    String name[] = names[k];
199:                    if (name[0].equals("1") && name[1].equals("0"))
200:                        name10 = name[3];
201:                    else if (name[2].equals("1033")) {
202:                        name3x = name[3];
203:                        break;
204:                    }
205:                }
206:                String finalName = name3x;
207:                if (finalName == null)
208:                    finalName = name10;
209:                if (finalName == null)
210:                    finalName = names[0][3];
211:                return new Font(finalName, 0, size);
212:            }
213:
214:            /** Maps a name to a BaseFont parameter.
215:             * @param awtName the name
216:             * @param parameters the BaseFont parameter
217:             */
218:            public void putName(String awtName, BaseFontParameters parameters) {
219:                mapper.put(awtName, parameters);
220:            }
221:
222:            /** Maps an alias to a name.
223:             * @param alias the alias
224:             * @param awtName the name
225:             */
226:            public void putAlias(String alias, String awtName) {
227:                aliases.put(alias, awtName);
228:            }
229:
230:            /** Looks for a BaseFont parameter associated with a name.
231:             * @param name the name
232:             * @return the BaseFont parameter or <CODE>null</CODE> if not found.
233:             */
234:            public BaseFontParameters getBaseFontParameters(String name) {
235:                String alias = (String) aliases.get(name);
236:                if (alias == null)
237:                    return (BaseFontParameters) mapper.get(name);
238:                BaseFontParameters p = (BaseFontParameters) mapper.get(alias);
239:                if (p == null)
240:                    return (BaseFontParameters) mapper.get(name);
241:                else
242:                    return p;
243:            }
244:
245:            /**
246:             * Inserts the names in this map.
247:             * @param allNames the returned value of calling {@link BaseFont#getAllFontNames(String, String, byte[])}
248:             * @param path the full path to the font
249:             */
250:            public void insertNames(Object allNames[], String path) {
251:                String names[][] = (String[][]) allNames[2];
252:                String main = null;
253:                for (int k = 0; k < names.length; ++k) {
254:                    String name[] = names[k];
255:                    if (name[2].equals("1033")) {
256:                        main = name[3];
257:                        break;
258:                    }
259:                }
260:                if (main == null)
261:                    main = names[0][3];
262:                BaseFontParameters p = new BaseFontParameters(path);
263:                mapper.put(main, p);
264:                for (int k = 0; k < names.length; ++k) {
265:                    aliases.put(names[k][3], main);
266:                }
267:                aliases.put(allNames[0], main);
268:            }
269:
270:            /** Inserts all the fonts recognized by iText in the
271:             * <CODE>directory</CODE> into the map. The encoding
272:             * will be <CODE>BaseFont.CP1252</CODE> but can be
273:             * changed later.
274:             * @param dir the directory to scan
275:             * @return the number of files processed
276:             */
277:            public int insertDirectory(String dir) {
278:                File file = new File(dir);
279:                if (!file.exists() || !file.isDirectory())
280:                    return 0;
281:                File files[] = file.listFiles();
282:                if (files == null)
283:                    return 0;
284:                int count = 0;
285:                for (int k = 0; k < files.length; ++k) {
286:                    file = files[k];
287:                    String name = file.getPath().toLowerCase();
288:                    try {
289:                        if (name.endsWith(".ttf") || name.endsWith(".otf")
290:                                || name.endsWith(".afm")) {
291:                            Object allNames[] = BaseFont.getAllFontNames(file
292:                                    .getPath(), BaseFont.CP1252, null);
293:                            insertNames(allNames, file.getPath());
294:                            ++count;
295:                        } else if (name.endsWith(".ttc")) {
296:                            String ttcs[] = BaseFont.enumerateTTCNames(file
297:                                    .getPath());
298:                            for (int j = 0; j < ttcs.length; ++j) {
299:                                String nt = file.getPath() + "," + j;
300:                                Object allNames[] = BaseFont.getAllFontNames(
301:                                        nt, BaseFont.CP1252, null);
302:                                insertNames(allNames, nt);
303:                            }
304:                            ++count;
305:                        }
306:                    } catch (Exception e) {
307:                    }
308:                }
309:                return count;
310:            }
311:
312:            public HashMap getMapper() {
313:                return mapper;
314:            }
315:
316:            public HashMap getAliases() {
317:                return aliases;
318:            }
319:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.