001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.font.afm;
031:
032: import java.io.ByteArrayInputStream;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.util.HashMap;
036: import java.util.Iterator;
037: import java.util.Map;
038: import java.util.Set;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041: import de.intarsys.font.AbstractFontRegistry;
042: import de.intarsys.font.FontFamily;
043: import de.intarsys.font.FontStyle;
044: import de.intarsys.tools.stream.StreamTools;
045:
046: /**
047: * DOCUMENT ME!
048: *
049: * @author wad To change the template for this generated type comment go to
050: * Window>Preferences>Java>Code Generation>Code and Comments
051: */
052: public class AFMRegistry extends AbstractFontRegistry {
053: // the list of the 14 adobe standard fonts available in every pdf viewer.
054: private static Map BUILTIN = new HashMap();
055:
056: private static AFMRegistry instance = null;
057:
058: static {
059: BUILTIN.put(AFM.FN_TIMES, "Times-Roman.afm"); //$NON-NLS-1$
060: BUILTIN.put(AFM.FN_TIMES_BOLD, "Times-Bold.afm"); //$NON-NLS-1$
061: BUILTIN.put(AFM.FN_TIMES_ITALIC, "Times-Italic.afm"); //$NON-NLS-1$
062: BUILTIN.put(AFM.FN_TIMES_BOLDITALIC, "Times-BoldItalic.afm"); //$NON-NLS-1$
063: BUILTIN.put(AFM.FN_HELVETICA, "Helvetica.afm"); //$NON-NLS-1$
064: BUILTIN.put(AFM.FN_HELVETICA_BOLD, "Helvetica-Bold.afm"); //$NON-NLS-1$
065: BUILTIN.put(AFM.FN_HELVETICA_OBLIQUE, "Helvetica-Oblique.afm"); //$NON-NLS-1$
066: BUILTIN.put(AFM.FN_HELVETICA_BOLDOBLIQUE,
067: "Helvetica-BoldOblique.afm"); //$NON-NLS-1$
068: BUILTIN.put(AFM.FN_COURIER, "Courier.afm"); //$NON-NLS-1$
069: BUILTIN.put(AFM.FN_COURIER_BOLD, "Courier-Bold.afm"); //$NON-NLS-1$
070: BUILTIN.put(AFM.FN_COURIER_OBLIQUE, "Courier-Oblique.afm"); //$NON-NLS-1$
071: BUILTIN.put(AFM.FN_COURIER_BOLDOBLIQUE,
072: "Courier-BoldOblique.afm"); //$NON-NLS-1$
073: BUILTIN.put(AFM.FN_SYMBOL, "Symbol.afm"); //$NON-NLS-1$
074: BUILTIN.put(AFM.FN_ZAPFDINGBATS, "ZapfDingbats.afm"); //$NON-NLS-1$
075: }
076:
077: /**
078: * The collection of built-in font names.
079: *
080: * @return The collection of built-in font names.
081: */
082: public static Set builtInFonts() {
083: return BUILTIN.keySet();
084: }
085:
086: public static synchronized AFMRegistry instance() {
087: if (instance == null) {
088: instance = new AFMRegistry();
089: instance.loadBuiltInFonts();
090: }
091: return instance;
092: }
093:
094: /**
095: * Answer <code>true</code> if <code>name</code> designates a built-in
096: * font for PDF.
097: *
098: * @param name
099: * THe name of the font to lookup.
100: *
101: * @return Answer <code>true</code> if <code>name</code> designates a
102: * built-in font for PDF.
103: */
104: public static boolean isBuiltIn(String name) {
105: return BUILTIN.containsKey(name);
106: }
107:
108: protected AFMRegistry() {
109: super (20);
110: }
111:
112: public AFM getFont(String name) {
113: String familyName = AFM.getFontFamilyName(name);
114: FontStyle style = AFM.getFontStyle(name);
115: FontFamily fonts = getFontFamily(familyName);
116: if (fonts != null) {
117: return (AFM) fonts.getFont(style);
118: }
119: return null;
120: }
121:
122: private static Logger logger = Logger.getLogger("AFM"); //$NON-NLS-1$
123:
124: /**
125: * Must be called before usage
126: */
127: protected void loadBuiltInFonts() {
128: String key = null;
129: String resourceName = null;
130: Iterator i = BUILTIN.keySet().iterator();
131:
132: // TODO load fonts as needed
133: while (i.hasNext()) {
134: byte[] bytes;
135:
136: key = (String) i.next();
137: resourceName = (String) BUILTIN.get(key);
138:
139: AFM result = null;
140:
141: InputStream is = AFMRegistry.class
142: .getResourceAsStream(resourceName);
143:
144: if (is == null) {
145: logger.log(Level.WARNING, "AFMRegistry: resource \"" //$NON-NLS-1$
146: + resourceName + "\" not found"); //$NON-NLS-1$
147: }
148:
149: try {
150: // font files are small enough to be read into memory
151: bytes = StreamTools.toByteArray(is);
152: result = new AFMParser()
153: .parse(new ByteArrayInputStream(bytes));
154: } catch (IOException e) {
155: logger.log(Level.WARNING, "AFMRegistry: resource \"" //$NON-NLS-1$
156: + resourceName + "\" can not be parsed (" //$NON-NLS-1$
157: + e.getMessage() + ")"); //$NON-NLS-1$
158: } finally {
159: StreamTools.close(is);
160: }
161:
162: addFont(result);
163: }
164: }
165: }
|