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 de.intarsys.font.FontStyle;
033: import de.intarsys.font.IFont;
034: import java.util.HashMap;
035: import java.util.Map;
036:
037: /**
038: * A simple implementation for an adobe font metric object.
039: *
040: * <p>
041: * This implementation will hold all afm attributes in a generic collection. The
042: * char metrics are read explicitly and held in a list of AFMChar's. No other
043: * information (like kerning) is extracted.
044: * </p>
045: *
046: * <p>
047: * See the "Adobe Font Metrics File Format Specification"
048: * </p>
049: */
050: public class AFM implements IFont {
051: /*
052: * todo create exceptions when font unavailable todo under construction
053: */
054: public static final String FN_TIMES = "Times-Roman"; //$NON-NLS-1$
055:
056: public static final String FN_TIMES_BOLD = "Times-Bold"; //$NON-NLS-1$
057:
058: public static final String FN_TIMES_ITALIC = "Times-Italic"; //$NON-NLS-1$
059:
060: public static final String FN_TIMES_BOLDITALIC = "Times-BoldItalic"; //$NON-NLS-1$
061:
062: public static final String FN_HELVETICA = "Helvetica"; //$NON-NLS-1$
063:
064: public static final String FN_HELVETICA_BOLD = "Helvetica-Bold"; //$NON-NLS-1$
065:
066: public static final String FN_HELVETICA_OBLIQUE = "Helvetica-Oblique"; //$NON-NLS-1$
067:
068: public static final String FN_HELVETICA_BOLDOBLIQUE = "Helvetica-BoldOblique"; //$NON-NLS-1$
069:
070: public static final String FN_COURIER = "Courier"; //$NON-NLS-1$
071:
072: public static final String FN_COURIER_BOLD = "Courier-Bold"; //$NON-NLS-1$
073:
074: public static final String FN_COURIER_OBLIQUE = "Courier-Oblique"; //$NON-NLS-1$
075:
076: public static final String FN_COURIER_BOLDOBLIQUE = "Courier-BoldOblique"; //$NON-NLS-1$
077:
078: public static final String FN_SYMBOL = "Symbol"; //$NON-NLS-1$
079:
080: public static final String FN_ZAPFDINGBATS = "ZapfDingbats"; //$NON-NLS-1$
081:
082: // the chars described in the font metric file
083: // ... organized by codePoint
084: private HashMap charsByCode = new HashMap();
085:
086: // ... organized by name
087: private HashMap charsByName = new HashMap();
088:
089: // attribute name/value pairs parsed form the font metric file
090: private Map attributes = new HashMap();
091:
092: /**
093: * BuiltInAFM constructor comment.
094: */
095: public AFM() {
096: super ();
097: }
098:
099: /**
100: * The string value for a generic attribute designated by <code>name</code>.
101: *
102: * @param name
103: * The name of the attribute to lookup.
104: *
105: * @return The string value for a generic attribute designated by
106: * <code>name</code>.
107: */
108: public String getAttribute(String name) {
109: return (String) attributes.get(name);
110: }
111:
112: /**
113: * The character representation for a byte code point given in
114: * <code>codePoint</code>.
115: *
116: * @param codePoint
117: * The byte code point.
118: *
119: * @return The character representation for a byte code point given in
120: * <code>codePoint</code>.
121: */
122: public AFMChar getCharByCode(int codePoint) {
123: return (AFMChar) getCharsByCode().get(new Integer(codePoint));
124: }
125:
126: /**
127: * The character representation for an adobe glyph name given in
128: * <code>name</code>.
129: *
130: * @param name
131: * The adobe glyph name.
132: *
133: * @return The character representation for an adobe glyph name given in
134: * <code>name</code>.
135: */
136: public AFMChar getCharByName(String name) {
137: return (AFMChar) getCharsByName().get(name);
138: }
139:
140: public static String getFontFamilyName(String name) {
141: if (name == null) {
142: return null;
143: }
144: int posMinus = name.indexOf('-');
145: if (posMinus > 0) {
146: name = name.substring(0, posMinus);
147: }
148: return name;
149: }
150:
151: public String getFontName() {
152: return getAttribute(AFMParser.TOKEN_FontName);
153: }
154:
155: public static FontStyle getFontStyle(String name) {
156: if (name == null) {
157: return FontStyle.REGULAR;
158: }
159: int posMinus = name.indexOf('-');
160: if (posMinus > 0) {
161: name = name.substring(posMinus + 1);
162: }
163: return FontStyle.getFontStyle(name);
164: }
165:
166: /*
167: * (non-Javadoc)
168: *
169: * @see de.intarsys.font.IFont#getFontFamilyName()
170: */
171: public String getFontFamilyName() {
172: return getFontFamilyName(getFontName());
173: }
174:
175: /*
176: * (non-Javadoc)
177: *
178: * @see de.intarsys.font.IFont#getFontStyle()
179: */
180: public FontStyle getFontStyle() {
181: return getFontStyle(getFontName());
182: }
183:
184: /**
185: * Set a generic attribute value. The attributes value is given in <code>
186: * value</code>,
187: * the name of the attribute is <code>name</code>.
188: *
189: * @param name
190: * The name of the attribute.
191: * @param value
192: * The value of the attribute.
193: */
194: protected void setAttribute(String name, String value) {
195: getAttributes().put(name, value);
196: }
197:
198: protected void setAttributes(java.util.Map newAttributes) {
199: attributes = newAttributes;
200: }
201:
202: protected java.util.Map getAttributes() {
203: return attributes;
204: }
205:
206: protected java.util.HashMap getCharsByCode() {
207: return charsByCode;
208: }
209:
210: protected java.util.HashMap getCharsByName() {
211: return charsByName;
212: }
213:
214: protected void addChar(AFMChar c) {
215: if (c.getCode() != -1) {
216: getCharsByCode().put(new Integer(c.getCode()), c);
217: }
218:
219: if (c.getName() != null) {
220: getCharsByName().put(c.getName(), c);
221: }
222: }
223: }
|