KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > encoding > Encoding


1 /**
2  * Copyright (c) 2003-2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.encoding;
32
33 import java.io.BufferedReader JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.InputStreamReader JavaDoc;
36 import java.io.IOException JavaDoc;
37
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.Map JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42
43 import org.pdfbox.cos.COSName;
44
45 import org.pdfbox.util.ResourceLoader;
46
47 import org.pdfbox.pdmodel.common.COSObjectable;
48
49 /**
50  * This is an interface to a text encoder.
51  *
52  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
53  * @version $Revision: 1.15 $
54  */

55 public abstract class Encoding implements COSObjectable
56 {
57
58
59     /**
60      * This is a mapping from a character code to a character name.
61      */

62     protected Map JavaDoc codeToName = new HashMap JavaDoc();
63     /**
64      * This is a mapping from a character name to a character code.
65      */

66     protected Map JavaDoc nameToCode = new HashMap JavaDoc();
67
68     private static final Map JavaDoc NAME_TO_CHARACTER = new HashMap JavaDoc();
69     private static final Map JavaDoc CHARACTER_TO_NAME = new HashMap JavaDoc();
70
71     static
72     {
73         BufferedReader JavaDoc glyphStream = null;
74         try
75         {
76             InputStream JavaDoc resource = ResourceLoader.loadResource( "Resources/glyphlist.txt" );
77             glyphStream = new BufferedReader JavaDoc( new InputStreamReader JavaDoc( resource ) );
78             String JavaDoc line = null;
79             while( (line = glyphStream.readLine()) != null )
80             {
81                 line = line.trim();
82                 //lines starting with # are comments which we can ignore.
83
if( !line.startsWith("#" ) )
84                 {
85                     int semicolonIndex = line.indexOf( ';' );
86                     if( semicolonIndex >= 0 )
87                     {
88                         try
89                         {
90                             String JavaDoc characterName = line.substring( 0, semicolonIndex );
91                             String JavaDoc unicodeValue = line.substring( semicolonIndex+1, line.length() );
92                             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc( unicodeValue, " ", false );
93                             String JavaDoc value = "";
94                             while(tokenizer.hasMoreTokens())
95                             {
96                                 int characterCode = Integer.parseInt( tokenizer.nextToken(), 16 );
97                                 value += (char)characterCode;
98                             }
99
100                             NAME_TO_CHARACTER.put( COSName.getPDFName( characterName ), value );
101                         }
102                         catch( NumberFormatException JavaDoc nfe )
103                         {
104                             nfe.printStackTrace();
105                         }
106                     }
107                 }
108             }
109         }
110         catch( IOException JavaDoc io )
111         {
112             io.printStackTrace();
113         }
114         finally
115         {
116             if( glyphStream != null )
117             {
118                 try
119                 {
120                     glyphStream.close();
121                 }
122                 catch( IOException JavaDoc e )
123                 {
124                     e.printStackTrace();
125                 }
126
127             }
128         }
129
130
131         NAME_TO_CHARACTER.put( COSName.getPDFName( ".notdef" ), "" );
132         NAME_TO_CHARACTER.put( COSName.getPDFName( "fi" ), "fi" );
133         NAME_TO_CHARACTER.put( COSName.getPDFName( "fl" ), "fl" );
134         NAME_TO_CHARACTER.put( COSName.getPDFName( "ffi" ), "ffi" );
135         NAME_TO_CHARACTER.put( COSName.getPDFName( "ff" ), "ff" );
136         NAME_TO_CHARACTER.put( COSName.getPDFName( "pi" ), "pi" );
137
138         Iterator JavaDoc keys = NAME_TO_CHARACTER.keySet().iterator();
139         while( keys.hasNext() )
140         {
141             Object JavaDoc key = keys.next();
142             Object JavaDoc value = NAME_TO_CHARACTER.get( key );
143             CHARACTER_TO_NAME.put( value, key );
144         }
145     }
146
147
148     /**
149      * This will add a character encoding.
150      *
151      * @param code The character code that matches the character.
152      * @param name The name of the character.
153      */

154     protected void addCharacterEncoding( int code, COSName name )
155     {
156         Integer JavaDoc intCode = new Integer JavaDoc( code );
157         codeToName.put( intCode, name );
158         nameToCode.put( name, intCode );
159     }
160
161     /**
162      * This will get the character code for the name.
163      *
164      * @param name The name of the character.
165      *
166      * @return The code for the character.
167      *
168      * @throws IOException If there is no character code for the name.
169      */

170     public int getCode( COSName name ) throws IOException JavaDoc
171     {
172         Integer JavaDoc code = (Integer JavaDoc)nameToCode.get( name );
173         if( code == null )
174         {
175             throw new IOException JavaDoc( "No character code for character name '" + name.getName() + "'" );
176         }
177         return code.intValue();
178     }
179
180     /**
181      * This will take a character code and get the name from the code.
182      *
183      * @param code The character code.
184      *
185      * @return The name of the character.
186      *
187      * @throws IOException If there is no name for the code.
188      */

189     public COSName getName( int code ) throws IOException JavaDoc
190     {
191         COSName name = (COSName)codeToName.get( new Integer JavaDoc( code ) );
192         if( name == null )
193         {
194             //lets be forgiving for now
195
name = COSName.getPDFName( "space" );
196             //throw new IOException( getClass().getName() +
197
// ": No name for character code '" + code + "'" );
198
}
199         return name;
200     }
201
202     /**
203      * This will take a character code and get the name from the code.
204      *
205      * @param c The character.
206      *
207      * @return The name of the character.
208      *
209      * @throws IOException If there is no name for the character.
210      */

211     public COSName getNameFromCharacter( char c ) throws IOException JavaDoc
212     {
213         COSName name = (COSName)CHARACTER_TO_NAME.get( "" + c );
214         if( name == null )
215         {
216             throw new IOException JavaDoc( "No name for character '" + c + "'" );
217         }
218         return name;
219     }
220
221     /**
222      * This will get the character from the code.
223      *
224      * @param code The character code.
225      *
226      * @return The printable character for the code.
227      *
228      * @throws IOException If there is not name for the character.
229      */

230     public String JavaDoc getCharacter( int code ) throws IOException JavaDoc
231     {
232         String JavaDoc character = getCharacter( getName( code ) );
233         return character;
234     }
235
236     /**
237      * This will get the character from the name.
238      *
239      * @param name The name of the character.
240      *
241      * @return The printable character for the code.
242      */

243     public static String JavaDoc getCharacter( COSName name )
244     {
245         String JavaDoc character = (String JavaDoc)NAME_TO_CHARACTER.get( name );
246         if( character == null )
247         {
248             character = name.getName();
249         }
250         return character;
251     }
252 }
Popular Tags