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;
031:
032: import java.io.File;
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: /**
037: * DOCUMENT ME!
038: *
039: * @author MIT TODO To change the template for this generated type comment go to
040: * Window - Preferences - Java - Code Style - Code Templates
041: */
042: public class FontMapper {
043: private static FontMapper active = null;
044:
045: private Map fontMaps = new HashMap();
046:
047: private FontMap defaultMap = null;
048:
049: public static synchronized FontMapper getActive() {
050: if (active == null) {
051: active = new FontMapper();
052: }
053: return active;
054: }
055:
056: protected FontMapper() {
057: super ();
058: }
059:
060: public void setDefaultMap(FontMap defaultMap) {
061: this .defaultMap = defaultMap;
062: }
063:
064: public FontMap getDefaultMap() {
065: return defaultMap;
066: }
067:
068: public FontMap lookupFontMap(String name) {
069: return (FontMap) fontMaps.get(normalize(name));
070: }
071:
072: public void registerAlias(String name, String alias) {
073: FontMap map = new FontAliasMap(this , normalize(name), alias);
074: if (name == null) {
075: setDefaultMap(map);
076: } else {
077: fontMaps.put(map.getKey(), map);
078: }
079: }
080:
081: public void registerFile(String name, File file) {
082: FontMap map = new FontFileMap(this , normalize(name), file);
083: if (name == null) {
084: setDefaultMap(map);
085: } else {
086: fontMaps.put(map.getKey(), map);
087: }
088: }
089:
090: public void registerResource(String name, Class owner,
091: String resource) {
092: FontMap map = new FontResourceMap(this , normalize(name), owner,
093: resource);
094: if (name == null) {
095: setDefaultMap(map);
096: } else {
097: fontMaps.put(map.getKey(), map);
098: }
099: }
100:
101: protected String normalize(String name) {
102: if (name == null) {
103: return null;
104: }
105: StringBuilder result = new StringBuilder();
106: for (int i = 0; i < name.length(); i++) {
107: char c = name.charAt(i);
108: if ((c == ' ') || (c == '_') || (c == '-')) {
109: continue;
110: }
111: result.append(Character.toLowerCase(c));
112: }
113: return result.toString();
114: }
115: }
|