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.util.StringTokenizer;
033:
034: import de.intarsys.tools.string.StringTools;
035:
036: /**
037: * A simple implementation for an AFM char metric object
038: *
039: * <p>
040: * This implementation will hold the information needed for implementing a
041: * simple pdf field layout mechanism. All attributes but name, code, and with
042: * are ignored.
043: * </p>
044: *
045: * <p>
046: * See the "Adobe Font Metrics File Format Specification"
047: * </p>
048: */
049: public class AFMChar {
050: /*
051: * todo under construction
052: */
053:
054: //
055: public static String AFM_TOKEN_WX = "WX"; //$NON-NLS-1$
056:
057: public static String AFM_TOKEN_W = "W"; //$NON-NLS-1$
058:
059: public static String AFM_TOKEN_W0X = "W0X"; //$NON-NLS-1$
060:
061: public static String AFM_TOKEN_W0 = "W0"; //$NON-NLS-1$
062:
063: public static String AFM_TOKEN_N = "N"; //$NON-NLS-1$
064:
065: public static String AFM_TOKEN_CH = "CH"; //$NON-NLS-1$
066:
067: public static String AFM_TOKEN_C = "C"; //$NON-NLS-1$
068:
069: // the char name
070: private String name;
071:
072: // the char code or -1
073: private int code;
074:
075: // the char width or 0
076: private int width;
077:
078: /**
079: * AFMChar constructor comment.
080: *
081: * @param code
082: * docme
083: * @param width
084: * docme
085: * @param name
086: * docme
087: */
088: protected AFMChar(int code, int width, String name) {
089: super ();
090: setCode(code);
091: setWidth(width);
092: setName(name);
093: }
094:
095: /**
096: * Create a new glyph representation from the definition parsed from
097: * <code>glyph</code>.
098: *
099: * @param glyph
100: * A string containing a definition for the glyph.
101: *
102: * @return The new glyph representation.
103: */
104: public static AFMChar create(String glyph) {
105: int code = -1;
106: int width = -1;
107: String name = StringTools.EMPTY;
108: StringTokenizer to = new StringTokenizer(glyph, ";"); //$NON-NLS-1$
109: while (to.hasMoreTokens()) {
110: StringTokenizer tf = new StringTokenizer(to.nextToken());
111: if (!tf.hasMoreTokens()) {
112: continue;
113: }
114:
115: String field = tf.nextToken();
116: if (field.equals(AFM_TOKEN_C)) {
117: code = Integer.parseInt(tf.nextToken());
118: } else if (field.equals(AFM_TOKEN_CH)) {
119: String value = tf.nextToken();
120: code = Integer.parseInt(value.substring(1, value
121: .length() - 1), 16);
122: } else if (field.equals(AFM_TOKEN_WX)) {
123: width = Integer.parseInt(tf.nextToken());
124: } else if (field.equals(AFM_TOKEN_W0X)) {
125: width = Integer.parseInt(tf.nextToken());
126: } else if (field.equals(AFM_TOKEN_W)) {
127: width = Integer.parseInt(tf.nextToken());
128: } else if (field.equals(AFM_TOKEN_W0)) {
129: width = Integer.parseInt(tf.nextToken());
130: } else if (field.equals(AFM_TOKEN_N)) {
131: name = tf.nextToken();
132: }
133: }
134: return new AFMChar(code, width, name);
135: }
136:
137: /**
138: * The glyphs code.
139: *
140: * @return The glyphs code.
141: */
142: public int getCode() {
143: return code;
144: }
145:
146: /**
147: * The glyphs name.
148: *
149: * @return The glyphs name.
150: */
151: public java.lang.String getName() {
152: return name;
153: }
154:
155: /**
156: * The glyphs width.
157: *
158: * @return The glyphs width.
159: */
160: public int getWidth() {
161: return width;
162: }
163:
164: /*
165: * (non-Javadoc)
166: *
167: * @see java.lang.Object#toString()
168: */
169: public String toString() {
170: return "<" + getCode() + ", " + getWidth() + ", " + getName() + ">"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
171: }
172:
173: private void setCode(int newCode) {
174: code = newCode;
175: }
176:
177: private void setName(java.lang.String newName) {
178: name = newName;
179: }
180:
181: private void setWidth(int newWidth) {
182: width = newWidth;
183: }
184: }
|