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.pdf.font;
031:
032: import de.intarsys.font.afm.AFM;
033: import de.intarsys.font.afm.AFMChar;
034: import de.intarsys.pdf.cos.COSObject;
035: import de.intarsys.pdf.encoding.Encoding;
036: import de.intarsys.pdf.encoding.GlyphNameMap;
037:
038: /**
039: * The builtin encoding of AFM defined fonts.
040: */
041: public class AFMEncoding extends Encoding {
042: //
043: private AFM afm;
044:
045: /**
046: * AFMEncoding constructor comment.
047: *
048: * @param afm
049: * The {@link AFM} object defining the encoding.
050: */
051: public AFMEncoding(AFM afm) {
052: super ();
053: setAfm(afm);
054: }
055:
056: /**
057: * Return the underlying Adobe font metrics.
058: *
059: * @return Return the wrapped Adobe font metrics.
060: */
061: public AFM getAfm() {
062: return afm;
063: }
064:
065: /*
066: * (non-Javadoc)
067: *
068: * @see de.intarsys.pdf.font.FontEncoding#getCode(java.lang.String)
069: */
070: public int getByteCode(String name) {
071: AFMChar c = getAfm().getCharByName(name);
072: if (c != null) {
073: return c.getCode();
074: }
075: return -1;
076: }
077:
078: /*
079: * (non-Javadoc)
080: *
081: * @see de.intarsys.pdf.encoding.Encoding#getByteCode(int)
082: */
083: public int getByteCode(int unicode) {
084: String glyphName = GlyphNameMap.Standard.getGlyphName(unicode);
085: return getByteCode(glyphName);
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see de.intarsys.pdf.encoding.Encoding#getCosObject(de.intarsys.pdf.cos.COSDocument)
092: */
093: public COSObject getCosObject() {
094: return null;
095: }
096:
097: /*
098: * (non-Javadoc)
099: *
100: * @see de.intarsys.pdf.font.FontEncoding#getName(int)
101: */
102: public String getGlyphName(int codePoint) {
103: AFMChar c = getAfm().getCharByCode(codePoint);
104: if (c != null) {
105: return c.getName();
106: }
107: return ".notdef";
108: }
109:
110: /*
111: * (non-Javadoc)
112: *
113: * @see de.intarsys.pdf.encoding.Encoding#getName()
114: */
115: public String getName() {
116: return "AFMEncoding";
117: }
118:
119: /*
120: * (non-Javadoc)
121: *
122: * @see de.intarsys.pdf.encoding.Encoding#getUnicode(int)
123: */
124: public int getUnicode(int byteCode) {
125: String glyphName = getGlyphName(byteCode);
126: if (glyphName == null) {
127: return -1;
128: } else {
129: return GlyphNameMap.Standard.getUnicode(glyphName);
130: }
131: }
132:
133: /*
134: * (non-Javadoc)
135: *
136: * @see de.intarsys.pdf.encoding.Encoding#getValidByteCode(java.lang.String)
137: */
138: public int getValidByteCode(String name) {
139: AFMChar c = getAfm().getCharByName(name);
140: if (c != null) {
141: return c.getCode();
142: }
143: return ' ';
144: }
145:
146: /*
147: * (non-Javadoc)
148: *
149: * @see de.intarsys.pdf.encoding.Encoding#getValidByteCode(int)
150: */
151: public int getValidByteCode(int unicode) {
152: String glyphName = GlyphNameMap.Standard.getGlyphName(unicode);
153: return getValidByteCode(glyphName);
154: }
155:
156: /**
157: * Set the underlying Adobe Font metrics.
158: *
159: * @param newAfm
160: * The underlying Adobe Font metrics.
161: */
162: private void setAfm(AFM newAfm) {
163: afm = newAfm;
164: }
165: }
|