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.truetype;
031:
032: import java.io.File;
033: import java.io.FilenameFilter;
034: import java.io.IOException;
035: import java.util.ArrayList;
036: import java.util.HashMap;
037: import java.util.List;
038: import java.util.Map;
039: import java.util.logging.Level;
040: import java.util.logging.Logger;
041:
042: import de.intarsys.font.AbstractFontRegistry;
043: import de.intarsys.font.FontStyle;
044: import de.intarsys.font.IFont;
045: import de.intarsys.tools.locator.FileLocator;
046: import de.intarsys.tools.locator.ILocator;
047:
048: /**
049: *
050: */
051: public class TTRegistry extends AbstractFontRegistry {
052: private static TTRegistry ACTIVE = null;
053:
054: private static Logger logger = Logger.getLogger("TrueType"); //$NON-NLS-1$
055:
056: public static synchronized TTRegistry get() {
057: if (ACTIVE == null) {
058: ACTIVE = new TTRegistry();
059: }
060: return ACTIVE;
061: }
062:
063: private Map osNamed = null; // Windows-like named
064:
065: private Map psNamed = null;
066:
067: protected TTRegistry() {
068: psNamed = new HashMap();
069: osNamed = new HashMap();
070: }
071:
072: public void addFontOsIndexed(TTFont font) {
073: String fontFamily = font.getFontFamilyName();
074: if (fontFamily == null) {
075: return;
076: }
077: getOsNamed()
078: .put(concatOsFontName(fontFamily, font.getFontStyle()),
079: font);
080: }
081:
082: public void addFontPsIndexed(TTFont font) {
083: String psName = font.getPsName();
084: getPsNamed().put(psName, font);
085: }
086:
087: protected String concatOsFontName(String fontFamily, FontStyle style) {
088: String result = removeSpaces(fontFamily);
089: if (style != FontStyle.REGULAR) {
090: result += style.getLabel();
091: }
092: return result;
093: }
094:
095: public IFont getFont(String psName) {
096: IFont result = null;
097: result = (IFont) getPsNamed().get(psName);
098: if (result == null) {
099: result = (IFont) getOsNamed().get(psName);
100: }
101: return result;
102: }
103:
104: public List getFonts() {
105: return new ArrayList(osNamed.values());
106: }
107:
108: protected Map getOsNamed() {
109: return osNamed;
110: }
111:
112: protected Map getPsNamed() {
113: return psNamed;
114: }
115:
116: /**
117: * Returns the TrueType fonts contained in the directory specified by
118: * <code>fontDirectory</code>. This method serves for font selection
119: * purposes.
120: *
121: * @param fontDirectory
122: * docme
123: */
124: public List listDirectory(File fontDirectory) {
125: File[] files = fontDirectory.listFiles(new FilenameFilter() {
126: public boolean accept(File dir, String name) {
127: return (name != null) ? name.toLowerCase().endsWith(
128: "ttf") //$NON-NLS-1$
129: : false;
130: }
131: });
132: List fonts = new ArrayList();
133: for (int i = 0; i < files.length; i++) {
134: try {
135: ILocator locator = new FileLocator(files[i]);
136: TTFont font = TTFont.createFromLocator(locator);
137: fonts.add(font);
138: } catch (Throwable t) {
139: // todo 2 warning
140: }
141: }
142: return fonts;
143: }
144:
145: public void registerDirectory(File fontDirectory) {
146: if ((fontDirectory == null) || !fontDirectory.isDirectory()) {
147: return;
148: }
149: File[] files = fontDirectory.listFiles(new FilenameFilter() {
150: public boolean accept(File dir, String name) {
151: return (name != null) ? name.toLowerCase().endsWith(
152: "ttf") //$NON-NLS-1$
153: : false;
154: }
155: });
156:
157: for (int i = 0; i < files.length; i++) {
158: boolean really;
159:
160: File file = files[i];
161: String filepath = file.getAbsolutePath();
162: really = false;
163: try {
164: /*
165: * the absolute path/canonical path check is used to find out if
166: * the file path contains a symbolic link. if so we can ignore
167: * the file because it will also show up in another directory as
168: * a "real" file.
169: */
170: if (file.isFile()
171: && filepath.equalsIgnoreCase(file
172: .getCanonicalPath())) {
173: really = true;
174: }
175: } catch (IOException ex) {
176: // ignore
177: }
178: if (really) {
179: try {
180: registerFile(files[i]);
181: } catch (TrueTypeException e) {
182: logger.log(Level.WARNING,
183: "TTRegistry: " + e.getMessage()); //$NON-NLS-1$
184: }
185: }
186: }
187: }
188:
189: public TTFont registerFile(File file) throws TrueTypeException {
190: try {
191: ILocator locator = new FileLocator(file);
192: TTFont font = TTFont.createFromLocator(locator);
193: addFont(font);
194: addFontPsIndexed(font);
195: addFontOsIndexed(font);
196: return font;
197: } catch (Throwable t) {
198: throw new TrueTypeException(
199: "Error loading font file:\"" + file //$NON-NLS-1$
200: + "\". Check if file is a valid."); //$NON-NLS-1$
201: }
202: }
203:
204: protected String removeSpaces(String name) {
205: int pos = 0;
206: while ((pos = name.indexOf(' ')) != -1) {
207: name = name.substring(0, pos) + name.substring(pos + 1);
208: }
209: return name;
210: }
211: }
|